Managing Python Environments Like a Pro | by Pratik Choudhari | Towards Data Science

Python virtual environments help us manage dependencies easily and effortlessly. The most common environment creation tools are virtualenv and conda, the latter is used for environment management for multiple languages whereas the former is made especially for python.
Why not use global python packages, then we won’t need to get into this environment mess, right? Well, yes, it will save us time managing environments but at what cost, the pain of having a setup ready to go for a project will grow exponentially. I learned this fact the hard way, using global packages for everything and not having a dedicated environment for every project.
In this blog, I will be writing about virtualenvwrapper , a python library to manage and customize environments in python which runs on top of the good old virtualenv. As we progress, we will see how the VEW CLI commands are similar to Linux commands like mkdir, rmdir and cp.
Note: I will be referring to virtualenvwrapper as VEW throughout this article
It is important to note that pyenv is not related to virtualenv or VEW. pyenv is used to switch between multiple python versions and does not manage packages installed. Also, pip is a package manager in python and pip too can not help us in managing environment because it was not made to do this. For more information read this stackoverflow thread.
stackoverflow.com
The installation process is the same as any other library.
pip install virtualenvwrapper
In a Linux system, after installation, we need to edit the .bashrc file, this will allow the user to access VEW in any terminal and location.
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3export WORKON_HOME=~/my_env_folderexport VIRTUALENVWRAPPER_VIRTUALENV=/home/my_user/.local/bin/virtualenvsource ~/.local/bin/virtualenvwrapper.sh
In the first line, we set the VIRTUALENVWRAPPER_PYTHON variable which points to the python binary installation VEW must refer toNext, WORKON_HOME is the folder where VEW will store all environments and utility scriptsVIRTUALENVWRAPPER_VIRTUALENV is the path to the original virtualenv binary
In the first line, we set the VIRTUALENVWRAPPER_PYTHON variable which points to the python binary installation VEW must refer to
Next, WORKON_HOME is the folder where VEW will store all environments and utility scripts
VIRTUALENVWRAPPER_VIRTUALENV is the path to the original virtualenv binary
As I said earlier, the commands here are similar to Linux commands. To create a new environment execute the following line.
mkvirtualenv my-env
This environment will be stored in the path specified in WORKON_HOME variable. Three options are supported alongside virtualenv option, which are:
-a my_path: folder for the environment, it means that whenever the environment will be activated, no matter in what path users are in currently, they will be redirected to my_path. It does not mean the environment will be created inside my_path.-i package1 package2 ...: try installing mentioned packages after the environment is created.-r requirements.txt: Create an environment and install from the requirements.txt file.
-a my_path: folder for the environment, it means that whenever the environment will be activated, no matter in what path users are in currently, they will be redirected to my_path. It does not mean the environment will be created inside my_path.
-i package1 package2 ...: try installing mentioned packages after the environment is created.
-r requirements.txt: Create an environment and install from the requirements.txt file.
rmvirtualenv my_env
Deletes environment folder. Remember to deactivate the environment before deleting it.
showvirtualenv my-env
lsvirtualenv
List all virtual environments created via this tool. Use -b option to just list environments and ignore the details.
virtualenv uses the following command to activate an environment.
source my-env/bin/activate
source is a commonly used Linux command which is primarily used for changing the environment variables with the current shell. Read more about it here. VEW abstracts this source command and provides an easy-to-remember alternative called workon .
workon my-env
Under the hood, VEW executes the source command.
Deactivating an environment in VEW is the same as virtualenv. In an active environment shell, execute the following.
deactivate
wipeenv
This command is to be run inside an active environment. When this is executed VEW will identify all third-party libraries and uninstall them.
Although virtualenv works just fine to manage all our environments, virtualenvwrapper is a recommended add-on. Its resemblance to Linux commands makes the operations easier to remember. Follow for more such articles. Thanks for reading till the end :)