Skip to content

Python

Conventions

Imports

Pip

Package installer.

Command Description
pip install <package> Install the latest version of a package
pip install <package>==<version> Install the specified version of a package
pip install -r requirements.txt Install the packages from a requirements.txt file
pip list --outdated View outdated packages
pip install --upgrade <package> Upgrade the desired packages
pip uninstall <package> Uninstall a package

If pip has not been added to the path, replace pip with python -m pip in the commands above.

Pyenv

Easily switch between multiple versions of Python.

Scripting

Virtual Environments

Bundle all the packages and their versions to run an application so that they do not conflict with the globally installed python packages.

  1. Create virtual environment directory (suggested name: .venv).

    py -m venv .venv
    
    python3 -m venv .venv
    
  2. Activate virtual environment. Use the active shell's corresponding activate script.

    .venv\Scripts\Activate.ps1
    
    source .venv/bin/activate
    
  3. Install the required packages: pip section

  4. Create/update the requirements.txt file (to record the versions of packages used in the project)

    pip freeze > requirements.txt
    
  5. Exit the virtual environment

    deactivate
    

VS Code integration

After selecting .venv/bin/python to be the Python interpreter, terminals and debuggers will run in the virtual environment by default.

Resources