This article is for absolute begginers in using Virualenv and Virtualenvwrapper. If you know how to use them already, you will feel bored. When I started to use Python, I always wondered how I can use different versions of a package in my system. Later I came to know about Virtualenv which can be used for creating multiple development environments in python.
Versions Used
- Ubuntu - 20.04
- Python - 3.8.2
- Virtualenv - 20.0.31
- Virtualenvwrapper - 4.8.4
To learn how to install and use Pip and Pipenv read this guide.
Virtualenv
Virtualenv is the package with which you can create mutilple isolate python development environments. Every every will have their own site-packages and environment variables.
Install virtualenv
Install Virtualenv using the command
sudo pip3 install virtualenv
Create virtual environment
Create virtualenv using the command
virtualenv venv_name
Activate virtual environment
You can activate the virtualenv using the command
source venv_name/bin/activate
If you see (venv_name)
at the beginning of your bash prompt, it denotes the active virtual environment.
After activating a virtual environment, all the python packages you install will be installed in the virtual environment instead of your system's global python installation. You can create multiple virtual environments and install multiple versions of the same package in each virtual environment.
But we are not done yet. There are some limitations and rules in using this. You have to be in the virtual environment directory to create and activate virtual environments.
Virtualenvwrapper
Installing virtualenvwrapper will get you past the limitations mentioned above in using virtualenv. As the implies, it is a set of extensions for using virtualenv.
Install virtualenvwrapper
Install virtualenvwrapper using the command
sudo pip3 install virtualenvwrapper
Edit bash config
Open your bash config file and add the following lines in the file
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=~/venvs/
source /usr/local/bin/virtualenvwrapper.sh
Here WORKON_HOME is the location where all the virtualenvs you create with virtualenvwrapper will be stored. You can change it to a folder of your choice. Then run the following command to activate the changes made in bash config file.
source ~/.bashrc #In case of Ubuntu
source ~/.bash_profile #In case of OS X
Create virtual environment
You can create virtual environment using the command
mkvirtualenv venv_name
You don't need to worry about the directory from which you run this command. All the virtualenvs will be created in the directory you defined in WORKON_HOME
in the previous step.
Activate virtual environment
You can activate virtual environment using the command
workon venv_name
Remove virtual environment
You can remova an existing virtual environment using the command
rmvirtualenv venv_name
Virtualenvwrapper makes it easy for us to create and manage virtual environments in Python.
You can see a demo of all these commands in the YouTube video given above. Feel free to post your comments.