Many facets of Python’s Virtual Environments
This XKCD artwork is quite well know now.
However if you set up things right, its not a mess at all rather a charm to work with. We will see how easy is it with setting up environments.
We look at the following methods.
Python Virtual Environments with venv
Installing a specific python version using venv
sudo apt install python3.8-venv
Create a virtual environment with venv
.
python3 -m venv <envname>
Activate and deactivate the environment.
source <venv>/bin/acivate
# to deactivate
deactivate
Conda
Download the version of conda you need as below and install.
wget https://repo.anaconda.com/miniconda/Miniconda3-py38_4.10.3-Linux-x86_64.sh
# install the above downloaded file
bash Miniconda3-<version>.sh
# remove the file once finished
rm Miniconda3-<version>.sh
Update conda
conda update conda
Creating new environments
conda create --name <env name>
# activate environment
conda activate <env name>
Adding packages to conda
conda install numpy
# Install env with different python version.
conda create --name condaenv python=3.8.10
Adding packages form environments yaml
.
A yaml
file needs to be created first as described here
conda env create -f environments.yml
Now update environments based on yaml
.
conda env update --file environments.yml
For all commands refer conda cheat sheet
Poetry
This one is my personal favourite just becasue of its simplicity and how effective it is.
Install poetry with curl
.
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
If python is not found, replace it with python3
. This could happen if you are using oh-my-zsh
shell.
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 -
Initialise an environment. This automatically creates a .toml
file in the directory with all packages.
poetry init
Once initialised, install or update if already installed.
poetry install
# in case of an update
poetry update
Adding packages to your project? Just do
poetry add <package name>
Build
Want to build the package as wheel?
poetry build
and you are good to go.
Pro Tip
- *Do not install from
pip
. Always use thecurl
method as above. - Poetry installs envs in your
.cache/
directory. This below command will install in your working directory if thats the way you work.
poetry config virtualenvs.in-project true