Python tips
And a few others for which requests have been made: for VSCode, et al.VSCode autocomplete shortcuts
Wow! The default VSCode settings are not useful for learning Python or computer science.They seem to be optimized for learning patience, in fact! Happily, there are overrides:
- Go to the Code menu, then Preferences
- Click on the Open Settings: JSON button.
- Copy these options from below...
- and paste them between the curly braces in that settings.json tab:
"editor.quickSuggestions": false,
"editor.acceptSuggestionOnCommitCharacter": false,
"editor.suggest.filterGraceful": true,
"editor.suggestOnTriggerCharacters": false,
"editor.acceptSuggestionOnEnter": "on",
"editor.suggest.showIcons": false,
"editor.suggest.maxVisibleSuggestions": 7,
"editor.hover.enabled": false,
"editor.hover.sticky": false,
"editor.suggest.snippetsPreventQuickSuggestions": false,
"editor.parameterHints.enabled": false,
"editor.wordBasedSuggestions": true,
"editor.tabCompletion": "on",
"extensions.ignoreRecommendations": true,
"files.autoSave": "afterDelay",
These options suppress the many popups and hovering windows and menus. - They still allow you to use code and variable-name completion (with the tab key),
- but they turn off some of the more aggressive hinting (too aggressive for cs5's purposes, my take)
VSCode spacing
Mixing tabs and spaces is convenient for typing code—especially Python! However, it's vital that the indentation you see in your text editor is what Python "sees" in its file. To ensure this, there is a one-time setting that will tell Sublime to handle spaces and tabs consistently (for all files):- Go to the File or Code menu, then Preferences followed by Settings
- In the search box, type "tab" to find tab-related settings
- Uncheck Editor: Detect Indentation
- Check Editor: Insert Spaces
- Make sure that Editor: Tab Size is set to 4
- Check Editor: Use Tab Stops
- Now type "newline" in the search bar and then click "Editor" to show only the editor-related stuff
- Check Files: Insert Final Newline
- Check Files: Trim Final Newlines
- You may wish to check Editor: Render Final Newline. If you do, there will be an extra (blank) line number at the end of your file so that it's easy to add lines at the bottom.
VSCode Liveshare for collaboration ...
VSCode offers a shared-interface across a remote connection called Liveshare:Where is ipython and similar options...
The default paths for Anaconda's ipython:- Windows: C:\Users\<your-username>\Anaconda3\Scripts\ipython.exe or bin
- Mac: /opt/anaconda3/bin/ipython
- Want to avoid the auto-activate conda defaults to? conda config --set auto_activate_base false
- on the CIS loaner (Mac) laptops, try here: /opt/anaconda3/bin/ipython
- want to add it to a Mac's path? Edit .bash_profile and/or .zshrc and add
- export PATH="/opt/anaconda3/bin:$PATH" at the bottom of the file
File Encodings!
Aargh! In Python, file types and character types can cause troubles...- To open a non-ASCII file, use f = open( filename, encoding='latin1' ) or try encoding='utf-16' or 'utf-8'
- To include non-ASCII characters in a Python source file, include # coding: utf-8 at the top of the .py file (or the others above)
- Here is an example of how to read files with emojis (e.g., Tweets) and process with them: emojitest.zip: emojitest_zip_file
DLL errors on Windows: use Anaconda Prompt
If you're getting library not found or DLL errors on Windows, thenWinError 995 yikes!
If you're in Windows and get an error similar to 'Unhandled exception in event loop' (WinError 995), this means that the "prompt-toolkit" library, allowing ipython's command-line editing, is too up-to-date...- To fix this problem, at the iPython prompt, paste and run this command, which reverts to a previous version:
!pip install prompt-toolkit==2.0.10
- The exclamation point is only needed if you are already at the iPython prompt.
- If you're at the (non-python) command line, the same command works, but without the exclamation point...
- Either way, you should only need to do this once, and you should be set.
- Here's a link to a fairly complete discussion of this: https://github.com/ipython/ipython/issues/12049
Catalina (new MacOS) path-setting
It's all new: zsh instead of bash. To add /opt/anaconda3/bin to the end of your PATH in .zshrc runecho export PATH=\"$PATH:/opt/anaconda3/bin\" >> ~/.zshrcTo add /opt/anaconda3/bin to the beginning of your PATH in .zshrc run
echo export PATH=\"/opt/anaconda3/bin:$PATH\" >> ~/.zshrcAfter either, start a new terminal and try which ipython. Yay! (I hope...)
VSCode keyboard shortcuts
Requests are in... for a keyboard shortcut that can change focus from the editor to the terminal panel (and back)... VSCode seems infinitely plastic and even this is doable. My take: Go to Preferences - Keyboard Shortcuts and click on the keybindings.json link that will be under the search bar. From there, place these two keybindings (or variations) into that file:
// Place your key bindings in this file to overwrite the defaults
[
// Toggle between terminal and editor focus
{
"key": "alt+up",
"command": "workbench.action.terminal.focus"
},
{
"key": "alt+up",
"command": "workbench.action.focusActiveEditorGroup",
"when": "terminalFocus"
},
{
"key": "ctrl+p",
"command": "editor.action.triggerSuggest",
"when": "editorHasCompletionItemProvider && textInputFocus && !editorReadonly"
},
]
Then, alt-uparrow (option-uparrow on the Mac) will swap between the terminal and the editor.
It also assigns control-p (Windows and Mac) to reveal the set of completion-suggestions. Rarely used, but nice to have!
VSCode error with printing! What!?
Phew! This has been fixed: https://github.com/Microsoft/vscode/issues/36630 But, because it's such a fun story, we're keeping it here! If your Python/VSCode can't print in an infinite loop (on Windows), it should be fixed by updating windows to Spring 2018 or later. To check your version:- hit the windowskey + r
- type winver and hit enter
uninstalling Java on Mac OS X
In any terminal window:sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefpane
Logging into the LAC computers
There is a guest account, guesthmc, on the LAC computers. Here are the instructions for them:- At the login screen, click on the "How do I sign in to another domain" text
- Into the login textfield on the login screen type
.\guesthmc - Don't type any password, just click on the arrow, and it will log you in.
- Where is Anaconda3? It's at C:\ProgramData\Anaconda3
- Some years the login is guest Sometimes it's hmcguest. If 2018's guesthmc does not work, try one of those two (it might be an older image or unreimaged machine...).
Use run
It's a pain to leave IPython and re-run, especially because all of your previous commands are lost! You don't have to leave IPython at all! Instead, when you're in IPython, paste or type (only once!)In [1]: run hw3pr1This completely reloads the file named hw3pr1.py. Then, you can use up-arrow to return to previous commands. Also, when you need to change your file to fix a bug or add a function, you can simply up-arrow back to run hw3pr1 and hit enter! No need to leave Python at all—this will typically save lots of time (and frustration)!
If you use Sublime: Make Sublime replace tabs with spaces
Mixing tabs and spaces is convenient for typing code—especially Python! However, it's vital that the indentation you see in your text editor is what Python "sees" in its file. To ensure this, there is a one-time setting that will tell Sublime to handle spaces and tabs consistently (for all files):- First, go to the Preferences menu
- On the Mac, it may be under the main Sublime Text menu
- Then, choose Settings - User
- A file of user settings will appear in Sublime. Yours may be empty; it may have other contents. Mine looked like this:
// Settings in here override those in "Default/Preferences.sublime-settings", // and are overridden in turn by file type specific settings. { }
- Replace those contents with the following complete text (or simply add the new lines):
{ // "font_size": 13, "tab_size" : 4, "translate_tabs_to_spaces": true, }
- Be sure to save this file.
- To be safe you could restart Sublime...
- Now, you can use tabs and spaces together, and tabs will become four spaces in the file—just as it looks!
- As a result, the file's look (its "syntax") will match how Python interprets it (its "semantics")
- If you would like to make the font that Sublime uses bigger, uncomment the first line by deleting the first two forward slashes (//) and space such that everything lines up.
If so, you should be set! Try it out...