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].startis inclusive, andendis exclusive.- If you are just slicing from the beginning, you can leave it out, e.g.
l[:3]will slice from0to3(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 from2(inclusive) to the end.
- If you are just slicing from the beginning, you can leave it out, e.g.
- 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
-
Key points
Due to Python being an interpreted language, its dependency management is similar to Node.js.
-
pyproject.toml-
PEP 518 -- Specifying Minimum Build System Requirements for Python Projects (opens in a new tab)
Similar to
pom.xmlin Maven
-
venv
-
Key points
- Virtual environments should be considered similar to
Maven POMto project dependencies, therefore one project per environment. - One
virtual environmentshould be dedicated to one project for dependency management purposes.
- Virtual environments should be considered similar to
-
Workflow
-
Prepare pyenv-installed Python versions
pyenv install $python_version -
Switch to the desired Python version for the current directory
pyenv local $python_versionThis will create a
.python-versionfile in the current directory (can be version controlled withgit). -
Prepare Poetry project config (
pyproject.toml)Make sure to specify the
project nameandPython 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" -
Switch to the virtual environment using the specified Python version, must meet version constraint in
pyproject.toml.poetry env use $python_versionThis will create a virtual environment if it does not exist.
-
Resolve project dependencies
poetry lockThis will create a
poetry.lockfile in the current directory (can be version controlled withgit). -
Install project dependencies
poetry install -
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
venvis bound to local Python installation, so not recommended to version control it.- Build system declares the Python version to use, and
venvis responsible for sourcing the correct version of Python installation.
venv - activate a virtualenv
source ${virtualenv_dir}/bin/activateNote:
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.txtvenv - create a requirements.txt file with all the packages installed in the current environment
pip freeze > requirements.txtpyenv
-
Key points
- Similar to
RVMandNVM pyenvinstalls and manages Python versions itself.pyenvaddshimswith the same name toPATHto overridepythonexecutables already inPATH.Shimswill switch between Python versions.
- Similar to
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 -lpyenv - list all installed Python versions
pyenv versionspyenv - determine which Pyenv-installed Python executable will actually be used
pyenv which $python_executable
# e.g. pyenv which python3pyenv - select a Pyenv-installed Python for the current shell
pyenv shell $python_versionpyenv - select a Pyenv-installed Python for the current directory
pyenv local $python_versionpip
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.txtpoetry
-
Key points
- All virtual environments created by Poetry are stored in
virtualenvs.pathin Poetry config.
- All virtual environments created by Poetry are stored in
poetry - set up
Installation
-
Homebrew
brew install poetry
poetry - create a project file template in the current directory
poetry initpoetry - determine Python version of the current environment
poetry env infopoetry - list all environments of the current project
poetry env list --full-pathpoetry - 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_versionpoetry - 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_nameNote: you can only delete an environment of the current project.
poetry - display the current config
poetry config --listuv (opens in a new tab)
uv - create a bare project
# under project directory
uv init --bareOnly 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 builduv - add a new dependency
uv add $package_nameuv - run a tool without installing it
uv tool run $tool_nameor
uvx $tool_nameuv - install a tool globally
uv tool install $tool_nameuv - show the directory of global tools installation
uv tool diruv - list all globally installed tools
uv tool listuv - upgrade the specified globally installed tool
uv tool upgrade $tool_nameuv - list available python versions and installations
uv python listExample:
❯ 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-installedExample:
❯ 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.8uv - install a specific Python version
uv python install $python_versionExample:
❯ uv python install 3.12
Installed Python 3.12.12 in 5.27suv - show the uv Python installation directory
uv python dirExample:
❯ uv python dir
/home/takechiyo/.local/share/uv/pythonuv - 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 pythonuv - run a REPL with additional dependencies
uv run --python 3.12 --with numpy --with pandasModules 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.pathOutput:
>>> 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.