I recently got myself a “new” laptop – a Lenovo x270 (yay)! And once again I needed to set up a Python virtual environment. So of course I Googled for a solution, just to find my previously written article on the same topic!
So in this article, I'll update the instructions based on my newly acquired knowledge.
And let me tell you, it’s easier than before because we are going to do only two things:
- Install virtualenvwrapper
- Edit the .bashrc file
In this article I will show you how to set up virtualenvwrapper with pip3 (pip for Python 3). We are not going to use Python 2 because it's no longer supported.
To complete this tutorial, you will need a computer with Ubuntu 20.04 installed and an internet connection. Also, some knowledge of the terminal and Vim editor would be useful.
Setting up a Virtual Environment
Now open your terminal in the home directory by right clicking and choosing the option “Open in Terminal”. You can also press the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.
You first need to create a special directory that will hold all of your virtual environments. So go ahead and create a new hidden directory called virtualenv:
Now you should install pip for Python3:
sudo apt install python3-pip
Confirm the pip3 installation:
virtualenvwrapper is a set of extensions for virtualenv. It provides commands like mkvirtualenv, lssitepackages, and especially workon for switching between different virtualenv environments.
Install virtualenvwrapper via pip3:
pip3 install virtualenvwrapper
We are going to modify your .bashrc file by adding a row that will adjust every new virtual environment to use Python 3. We will point virtual environments to the directory we created above (.virtualenv) and we will also point to the locations of the virtualenv and virtualenvwrapper.
Now open the .bashrc file using the Vim editor:
If you still haven’t used Vim before or you don’t have it installed on your computer, you should install it now. It is one of the most widely used Linux editors and for good reason.
sudo apt install vim
After you've installed Vim open the .bashrc file by typing in the
vim .bashrc command in your terminal. Navigate to the bottom of the .bashrc file, press the letter i to enter insert mode in Vim, and add these rows:
#Virtualenvwrapper settings: export WORKON_HOME=$HOME/.virtualenvs VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 . /usr/local/bin/virtualenvwrapper.sh
After you are done, press the esc key, then type :wq and press enter. This command will save the file and exit Vim.
Now you need to reload the bashrc script. There are two ways to do it – close and reopen your terminal, or execute this command in the terminal:
To create a virtual environment in Python3 and activate it immediately use this command in your terminal:
To deactivate the environment use the deactivate command.
To list all available virtual environments use the command workon or lsvirtualenv (lsvirtualenv will show the same result as workon but in a fancier way) in your terminal:
To activate one specific environment use workon + name of your environment:
There are several useful command you might need to use someday:
Rmvirtualenv will remove a specific virtual environment located in your .virtualenv directory.
Cpvirtualenv will copy the existing virtual environment to a new virtual environment and activate it.
cpvirtualenv old_virtual_env new_virtual_env
Well done! You have now created your first isolated Python 3 environment.
Thank you for reading!