Python

List

  • You can access values using negative indexes, which will go from right to left. The last element would be at index -1.
  • You can get multiple values with slicing, i.e. l[start:end]. start is inclusive, and end is exclusive.
    • If you are just slicing from the beginning, you can leave it out, e.g. l[:3] will slice from 0 to 3 (exclusive).
    • Similarly, if you are just slicing up to the end of the list, you can leave it out, e.g. l[2:] will slice from 2 (inclusive) to the end.
  • One correct way to copy a list is b = a[:], since slicing gives you a new list.
  • You can combine two lists using +.

Build System

venv

  • Key points

    • Virtual environments should be considered similar to Maven POM to project dependencies, therefore one project per environment.
    • One virtual environment should be dedicated to one project for dependency management purposes.
  • Workflow

    1. Prepare pyenv-installed Python versions

      pyenv install $python_version
    2. Switch to the desired Python version for the current directory

      pyenv local $python_version

      This will create a .python-version file in the current directory (can be version controlled with git).

    3. Prepare Poetry project config (pyproject.toml)

      Make sure to specify the project name and Python version, which will be used in the name of the virtual environment.

      # Create a new Poetry project file
      poetry init
      # Example: pyproject.toml
      [tool.poetry]
      name = "03-visualizing-financial-time-series"
       
      [tool.poetry.dependencies]
      python = ">=3.9,<3.10"
    4. Switch to the virtual environment using the specified Python version, must meet version constraint in pyproject.toml.

      poetry env use $python_version

      This will create a virtual environment if it does not exist.

    5. Resolve project dependencies

      poetry lock

      This will create a poetry.lock file in the current directory (can be version controlled with git).

    6. Install project dependencies

      poetry install
    7. Use the virtual environment in IDE if needed

      # Get the path of the activated virtual environment
      poetry env list --full-path | grep Activated | awk '{print $1}'

venv - Cheatsheet

venv - version control
  • venv is bound to local Python installation, so not recommended to version control it.
  • Build system declares the Python version to use, and venv is responsible for sourcing the correct version of Python installation.
venv - activate a virtualenv
source ${virtualenv_dir}/bin/activate

Note:

You can also work with your virtual environment without activating it. To do this, you provide the full path to its Python interpreter when executing a command. However, you’ll likely want to activate the virtual environment after you create it to save yourself the effort of having to repeatedly type long pathnames.
venv - install Packages from requirements.txt in the current environment
pip install -r requirements.txt
venv - create a requirements.txt file with all the packages installed in the current environment
pip freeze > requirements.txt

pyenv

pyenv - set up

  • Homebrew

    brew install pyenv
  • zsh

    # env
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    # init
    eval "$(pyenv init -)"

pyenv - list all available Python versions

pyenv install -l

pyenv - list all installed Python versions

pyenv versions

pyenv - determine which Pyenv-installed Python executable will actually be used

pyenv which $python_executable
# e.g. pyenv which python3

pyenv - select a Pyenv-installed Python for the current shell

pyenv shell $python_version

pyenv - select a Pyenv-installed Python for the current directory

pyenv local $python_version

pip

pip - Generate a requirements.txt file

Requirements.txt is a definitive list of all the packages installed in the current environment with exact versions, similar to uv.lock of uv.

pip freeze > requirements.txt

poetry

  • Key points

    • All virtual environments created by Poetry are stored in virtualenvs.path in Poetry config.

poetry - set up

Installation

  • Homebrew

    brew install poetry

poetry - create a project file template in the current directory

poetry init

poetry - determine Python version of the current environment

poetry env info

poetry - list all environments of the current project

poetry env list --full-path

poetry - switch to a different Python version

Will create a new environment with the specified Python version if it doesn't already exist.

poetry env use $python_version

poetry - delete an environment

# Get the name of the environment
env_name=$(poetry env list | grep $python_version | awk '{print $1}')
 
# Delete the environment
poetry env remove $env_name

Note: you can only delete an environment of the current project.

poetry - display the current config

poetry config --list

uv (opens in a new tab)

uv - create a bare project

# under project directory
uv init --bare

Only create pyproject.toml with the current directory name as the project name.

# Current directory name: test
[project]
name = "test"
version = "0.1.0"
requires-python = ">=3.9"
dependencies = []

uv - convert an existing project to uv

# pyproject.toml
[project]
name = "my_project"
version = "0.1.0"
requires-python = ">=3.9,<3.10"
dependencies = [
    "alpha-vantage>=3.0.0",
    "pandas>=2.0.0"
]
# Test the build
uv build

uv - add a new dependency

uv add $package_name

uv - run a tool without installing it

uv tool run $tool_name

or

uvx $tool_name

uv - install a tool globally

uv tool install $tool_name

uv - show the directory of global tools installation

uv tool dir

uv - list all globally installed tools

uv tool list

uv - upgrade the specified globally installed tool

uv tool upgrade $tool_name

uv - list available python versions and installations

uv python list

Example:

 uv python list
cpython-3.14.0b3-linux-x86_64-gnu                 <download available>
cpython-3.14.0b3+freethreaded-linux-x86_64-gnu    <download available>
cpython-3.13.5-linux-x86_64-gnu                   /home/linuxbrew/.linuxbrew/bin/python3.13 -> ../Cellar/python@3.13/3.13.5/bin/python3.13
cpython-3.13.5-linux-x86_64-gnu                   /home/linuxbrew/.linuxbrew/bin/python3 -> ../Cellar/python@3.13/3.13.5/bin/python3
cpython-3.13.5-linux-x86_64-gnu                   <download available>
cpython-3.13.5+freethreaded-linux-x86_64-gnu      <download available>
cpython-3.12.11-linux-x86_64-gnu                  <download available>
cpython-3.11.13-linux-x86_64-gnu                  <download available>
cpython-3.11.11-linux-x86_64-gnu                  /usr/bin/python3.11
cpython-3.10.18-linux-x86_64-gnu                  <download available>
cpython-3.9.23-linux-x86_64-gnu                   <download available>
cpython-3.9.21-linux-x86_64-gnu                   /usr/bin/python3.9
cpython-3.9.21-linux-x86_64-gnu                   /usr/bin/python3 -> python3.9
cpython-3.8.20-linux-x86_64-gnu                   /home/takechiyo/.local/share/uv/python/cpython-3.8.20-linux-x86_64-gnu/bin/python3.8
pypy-3.11.11-linux-x86_64-gnu                     <download available>
pypy-3.10.16-linux-x86_64-gnu                     <download available>
pypy-3.9.19-linux-x86_64-gnu                      <download available>
pypy-3.8.16-linux-x86_64-gnu                      <download available>
graalpy-3.11.0-linux-x86_64-gnu                   <download available>
graalpy-3.10.0-linux-x86_64-gnu                   <download available>
graalpy-3.8.5-linux-x86_64-gnu                    <download available>

uv - list only installed Python versions

203949e ([Python,uv] run REPL)

uv python list --only-installed

Example:

 uv python list --only-installed
cpython-3.13.5-linux-x86_64-gnu     /home/linuxbrew/.linuxbrew/bin/python3.13 -> ../Cellar/python@3.13/3.13.5/bin/python3.13
cpython-3.13.5-linux-x86_64-gnu     /home/linuxbrew/.linuxbrew/bin/python3 -> ../Cellar/python@3.13/3.13.5/bin/python3
cpython-3.11.11-linux-x86_64-gnu    /usr/bin/python3.11
cpython-3.9.21-linux-x86_64-gnu     /usr/bin/python3.9
cpython-3.9.21-linux-x86_64-gnu     /usr/bin/python3 -> python3.9
cpython-3.8.20-linux-x86_64-gnu     /home/takechiyo/.local/share/uv/python/cpython-3.8.20-linux-x86_64-gnu/bin/python3.8

uv - install a specific Python version

uv python install $python_version

Example:

 uv python install 3.12
Installed Python 3.12.12 in 5.27s

uv - show the uv Python installation directory

uv python dir

Example:

 uv python dir
/home/takechiyo/.local/share/uv/python

uv - specify an explicit Python version

uv supports more formats than other tools that read .python-version files, i.e., pyenv. If compatibility with those tools is needed, only use version numbers instead of complex requests such as cpython@3.10.

If no request is provided, the currently pinned version will be shown.

uv python pin 

Example:

 uv python pin 3.12
Pinned `.python-version` to `3.12`

uv - run a REPL with a specific version of Python

# e.g.
uv run --python 3.12 python

uv - run a REPL with additional dependencies

uv run --python 3.12 --with numpy --with pandas

Modules and Packages

Module search path

How the module search path is truly configured on your machine, print the built-in `sys.path`` list.

import sys
 
sys.path

Output:

>>> import sys
>>> sys.path
['', 'C:\\Users\\Takechiyo\\scoop\\apps\\miniconda3\\current\\python313.zip', 'C:\\Users\\Takechiyo\\scoop\\apps\\miniconda3\\current\\DLLs', 'C:\\Users\\Takechiyo\\scoop\\apps\\miniconda3\\current\\Lib', 'C:\\Users\\Takechiyo\\scoop\\apps\\miniconda3\\25.7.0-2', 'C:\\Users\\Takechiyo\\scoop\\apps\\miniconda3\\current', 'C:\\Users\\Takechiyo\\scoop\\apps\\miniconda3\\current\\Lib\\site-packages']

Python searches each directory in this list from left to right and uses the first file match it finds. The empty string at the front means the current directory.