Virtual environments and python versions
Contents:
Recipe
Create and activate a new virtualenv
pyenv versionsorpyenv install -l- list available versionspyenv virtualenv <python version> <environment name>pyenv local <environment name>- this creates a
.python-versionfile in the current directory - the environment will be automatically activated and deactivated because
eval "$(pyenv virtualenv-init -)"is in the.zshrcfile.
- this creates a
python -Vpython -m test
If you did not configure eval "$(pyenv virtualenv-init -)" to run in your shell then
pyenv activate <environment name>pyenv deactivate
Background
pyenvmanages multiple versions of Python itself.virtualenv/venvmanages virtual environments for a specific Python version.pyenv-virtualenvis a pyenv plugin that plays nicely withvirtualenv. It manages virtual environments across varying versions of Python.python -V- shows which python version is currently being used.which python- shows the path or location of the current python.
Python versions
Installing new python versions
pyenv install --list | grep " 3\.9\."- list versionspyevn install <name>- install a new version- pyenv python versions are installed at
~/.pyenv/versions/ - You can uninstall a pyenv python version by
rm -rf ..the subdir in~/.pyenv/versions/...pyenv uninstall <name>
Set a particular python version
pyenv versions- list available versionspyenv global <name>- set system default python versionpyenv global system- back to how things were before pyenvpyenv local <name>- set default version for this current dir- this creates a
.python-versionfile in the current directory. If pyenv is active in your environment then the version specified in this file will be activated
Other commands
pyenv uninstall <version>pyenv rehash- run after installing a new version, or install a package that provides binaries
Tests
python -m test- run some tests to be confident everything is ok
Source
Code editor integration using activate_this.py
So that code editors like vim can respond to the currently active virtual environment and update path, sys.path and sys.prefix (for example when running Django test suites) virtualenv uses activate_this.py to make these changes.
For some reason it isn't available on my setup, so here is a copy I found somewhere. It's different to the version hosted on the virtualenv repo and I haven't looked at why yet. Copy paste the version below into ~/.pyenv/versions/<version>/envs/<environment name>/bin/activate_this.py
activate_this.py
## -*- coding: utf-8 -*-
## https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/python/activate_this.py
"""Activate virtualenv for current interpreter:
Use exec(open(this_file).read(), {'__file__': this_file}).
This can be used when you must use an existing Python interpreter, not the virtualenv bin/python.
"""
import os
import site
import sys
try:
abs_file = os.path.abspath(__file__)
except NameError:
raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))")
bin_dir = os.path.dirname(abs_file)
base = bin_dir[: -len("__BIN_NAME__") - 1] # strip away the bin part from the __file__, plus the path separator
## prepend bin to PATH (this file is inside the bin directory)
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
os.environ["VIRTUAL_ENV"] = base # virtual env is right above bin directory
## add the virtual environments libraries to the host python import mechanism
prev_length = len(sys.path)
for lib in "__LIB_FOLDERS__".split(os.pathsep):
path = os.path.realpath(os.path.join(bin_dir, lib))
site.addsitedir(path.decode("utf-8") if "__DECODE_PATH__" else path)
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]
sys.real_prefix = sys.prefix
sys.prefix = base