Python

Utilities

Pyenv – Simple Python Version Management

Python versions

versionsList all Python versions available to pyenv
installInstall a Python version using python-build
updateUpdate pyenv, its plugins including the list of available versions
globalSet or show the global Python version(s)
localSet or show the local application-specific Python version(s)
Install versions
pyenv install --list
pyenv install 3.15-dev
Set local python version
pyenv local 3.14.2
Unset the local version
pyenv local --unset

Documentation: https://github.com/pyenv/pyenv#readme

Tutorial for Linux Mint: https://forums.linuxmint.com/viewtopic.php?f=42&p=2103213

Shell

shellSets a shell-specific Python version by setting the `PYENV_VERSION’ environment variable in your shell. This version overrides local application-specific versions and the global version.
Set the current shell to use a certain python version
pyenv shell 3.14.2
Unset the current shell
pyenv shell --unset

virtualenv

virtualenvs
virtualenv
virtualenv-delete
activateActivate a Python virtualenv environment in current shell. This acts almost as same as pyenv shell, but this invokes the activate script in your shell.
deactivate
Create venv
pyenv virtualenv 3.14.2 wow-sync

Created with the specified python version in ~/.pyenv/versions/3.14.2/envs/wow-sync/ with a wow-sync symlink in ~/.pyenv/versions/

Pip will install stuff in ~/.pyenv/versions/wow-sync/lib/python3.14/site-packages

Delete venv
pyenv virtualenv-delete qdrant
Set the local version in current directory

creates a .python-version in the current directory

pyenv local wow-sync
Activate a venv in the current shell
pyenv activate wow-sync

Or if a local version has been set up with pyenv local wow-sync:

pyenv activate
Deactivate a venv in the local shell
pyenv deactivate

pip

Commands

installInstall packages.
uninstallUninstall packages.
listList installed packages.
pip list
install
-U, --upgradeUpgrade all specified packages to the newest available version. The handling of dependencies depends on the upgrade-strategy used.
-i, --index-url <url>Base URL of the Python Package Index (default https://pypi.org/simple). This should point to a repository compliant with PEP 503 (the simple repository API) or a local directory laid out in the same format.
pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cpu
uninstall
pip3 uninstall torch torchvision
pip list --format=freeze | grep "nvidia-" | cut -d'=' -f1 | xargs -I{} pip uninstall -y {}

Environment variables

PIP_CACHE_DIRwhere cache files are stored

Libraries

Dotenv – local environment variables

Install:

pip install dotenv

In code (this will load the working directory .env file):

from dotenv import load_dotenv
load_dotenv()

Use the variables:

os.getenv("QDRANT_URL")

By default, load_dotenv() will:

  • Look for a .env file in the same directory as the Python script (or higher up the directory tree).
  • Read each key-value pair and add it to os.environ.
  • Not override an environment variable that is already set, unless you explicitly pass override=True.

Documentation: https://pypi.org/project/python-dotenv/

VScode

Clean sequence for python projects

For my wow-sync python project:

  • pyenv install 3.14.2 (if not already installed)
  • pyenv virtualenv 3.14.2 wow-sync
  • cd directory
  • pyenv local wow-sync (creates a .python-version)
  • code .

After that, VScode will automatically use pyenv shell.

Other stuff (for new pages)

Huggingface

Env variables

HF_HOMEPath to the cache folder (can be large)
HF_TOKENyour token

Jupyter

Roughly needed in VScode

pip install ipykernel ipywidgets

Cell

# %%