CS5: Introduction to Computer Science at Harvey Mudd College
CS5 Web > PythonTips
Submissions: CS submission site

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:


    "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.


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):

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:

Have Anaconda but no ipython?     pip install ipython   has worked for some...


File Encodings!

Aargh! In Python, file types and character types can cause troubles...


DLL errors on Windows:   use Anaconda Prompt

If you're getting library not found or DLL errors on Windows, then


WinError 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...


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 run

echo export PATH=\"$PATH:/opt/anaconda3/bin\" >> ~/.zshrc

To add /opt/anaconda3/bin to the beginning of your PATH in .zshrc run

echo export PATH=\"/opt/anaconda3/bin:$PATH\" >> ~/.zshrc

After 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:

Here's what a Spring 2018 update version will look like:

winver.png


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:

  1. At the login screen, click on the "How do I sign in to another domain" text
  2. Into the login textfield on the login screen type
           .\guesthmc
    
  3. Don't type any password, just click on the arrow, and it will log you in.
  4. Where is Anaconda3? It's at C:\ProgramData\Anaconda3

Warning:


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 hw3pr1

This 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):

When you save these settings, new files should now be in Spaces: 4 mode, meaning that each time you type a tab, it's replaced by four spaces. Here's what that tab/space menu will look like:

pic3.png

If so, you should be set! Try it out...