Install VSCode, Python, and the Python extention.
Use pyenv to manage multiple Python installations on your computer. You should never install packages into the your system's default Python installation. Instead, you can use pyenv to activate and deactivate different, isolated versions of Python.
Helpful commands:
pyenv install -v 3.8.5to install Python 3.8.5.pyenv global 3.8.5to activate that version globally.python --versionto check the current Python version.
Helpful link:
- Managing Multiple Python Versions With pyenv @Realpython
The basic functionality is provided by Python's native venv. The workflow for setting up a new project consists of:
- Setting up a
gitrepo. - Installing and activating the right python version, e.g. through
pyenv global 3.8.5. - Creating a virtual environment through
python -m venv /path/to/virtualenv. - Optional: Install requirements from
requirements.txt.
You can either store your virtual environments in a central place, e.g. ~/.virtualenvs/project-a-venv or within your project directory under ./venv. In the latter scenario, make sure that folder is not part of version control.
There are multiple popular helpers for managing virtual environments, e.g. virtualenvwrapper. Those tools are mainly helpful for browsing and quickly switching between virtual environments.
You can put a file called .env into your project's root directory and tell VSCode to set those variables everywhere by setting python.envFile to ${workspaceFolder}/.env in your user or project settings.
Most projects will make use of Python's packaging system, i.e. contain a setup.py or pyproject.toml at the root level. For a project in "src layout", you will usually install the package in editable mode through pip install --editable. Then, you can import from the package in any other python script or notebook (e.g. from src.utils import foo) and you won't need to re-install after editing the package (that's why it's called "editable" mode).
This repo contains a pre-written user settings file (settings.json that you can place into ~/Library/Application Support/Code/User/settings.json). All projects will inherit the user settings. Projects can extend/overwrite user settings by providing another file in the project directory under .vscode/settings.json. Pylance, for example, will automatically add an entry related to the location of the Python interpreter in the project settings.
The Python extension automatically installs Pylance, Jupyter, and isort extensions.
- Key Features:
- IntelliSense (code autocomplete)
- Linting (Pylint, Flake8)
- Code formatting (black, autopep)
- Debugging
- Testing (unittest, pytest)
- Jupyter Notebooks
- Environments (venv, pipenv, conda)
- Refactoring
Indent-rainbow extensions provide us with a multilevel colorized indentation for improved code readability. We get alternating colors on each step, and it helps us avoid common indentation errors.
Python Indent extension helps us with creating indentations. By pressing the Enter key, the extension will parse the Python file and determine how the next line should be indented. It is a time saver.
Jupyter Notebook Renderers is part of the Jupyter extension pack. It helps us render plotly, vega, gif, png, svg, and jpeg output.
The autoDocstring extension helps us quickly generate docstring for Python functions. By typing triple quotes """ or ''' within the function, we can generate and modify docstring. Learn more about doc strings by following our Python Docstrings tutorial.
Black Formatter formats any file with >Format Document. By default, VSCode will use black for formatting and prompt you to install it if you haven't done so already. You can also format files upon saving automatically by setting editor.formatOnSave to true.
pip install black
black {source_file_or_directory}
python -m black {source_file_or_directory} # if as a script doesn't workYou can select the linting method by selecting >Python: Select Linter command in the command palette (Ctrl+Shift+P). You can also manually enable the linting method in settings. You can also review the list of available linting methods.
- Enable/ Disable Linting: select >Python: Enable/Disable Linting in command palette.
pip install pylint
pylint {source_file_or_directory}You can automatically sort your imports with isort by through >Python Refactor: Sort Imports. For compatibility with black, you need to set python.sortImports.args to ["--profile=black"].
pip install isort
isort {source_file_or_directory}- Command Palette: Access entire all available commands by using the Keyboard shortcut: Ctrl+Shift+P.
- Keyboard shortcuts: We can modify keyboard shortcuts or memorize them by using keyboard reference sheets.
- Command line: Launch the VSCode editor through the command line interface by typing
code .. - Errors and warnings: Quick jump to errors and warnings in a project by using the keyboard shortcut: Ctrl+Shift+M. We can also cycle through the error with F8 or Shift+F8.
- Multi cursor selection: Add cursors at arbitrary positions by using Alt+Click. We can also use Ctrl+Shift+L to modify all occurrences of the current selection.
- Rename: We can rename the symbol by selecting the symbol and typing F2.
- Bracket Colorization: To add color to each set of opening and closing brackets, making it easier to identify each set of brackets. To configure this setting, open Settings in VSCode and then search for Bracket Pair Colorization.
- Sticky Scroll: You need to enable the
editor.experimental.StikyScroll.enabledor you can go to the settings of your VSCode and look for the Experimental>Sticky Scroll: Enabled option.
GitLens supercharges your Git experience in VS Code.
- How to Configure Visual Studio Code for Python Development @freeCodeCamp.
- Setting Up VSCode For Python: A Complete Guide @Datacamp.