Python virtual environment is used to create an isolated environment for Python project which contains interpreter, libraries, and scripts. You can create any number of virtual environments for your projects with each having its own dependencies.
By using virtual environments you avoid installing packages globally which could break other projects.
Putting it in simple words, a virtual environment helps to properly install the specific versions of the packages required by a python project.
To keep your Ubuntu 22.04 with the latest packages, run the following two apt commands:
sudo apt update sudo apt upgrade
Install pip for python3
Before installing the virtual environment, let's install pip. Pip is a package manager which helps to install, uninstall and upgrade packages for your projects.
To install pip for python 3 type:
apt install python3-pip
Create virtual environment for python 3
Venv command is used in Python to create the virtual environment. The venv package is available in Ubuntu repository.
Let's first install venv package using the following command:
apt install python3-venv
Now, to create a virtual environment, type:
python3 -m venv my_env_project
The above command creates a directory named 'my_env_project' in the current directory, which contains pip, interpreter, scripts, and libraries.
$ ls my_env_project/ bin include lib lib64 pyvenv.cfg share
You can now activate the virtual environment, type:
Command prompt would change to your environment and will look as shown:
Verify Virtual Environment
Run python command inside virtual environment to open the interpreter:
(my_env_project) linuxopsys@ubuntu:~$ python
Python 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
To install a package inside the virtual environment, for example I am installing NumPy package:
(my_env_project) linuxopsys@ubuntu:~$ pip install numpy --user
If you are getting below error
"ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv."
Set include-system-site-packages to true in pyvenv.cfg file.
Every time you install a new package inside your virtual environment, you should be able to import it into your project.
(my_env_project) linuxopsys@ubuntu:~/my_env_project$ python >>> import numpy
Let's test a math function, type:
>>> import math
To exit from the interpreter, type:
We can create python script and run from inside python 3 virtual environment.
(my_env_project) linuxopsys@ubuntu:~$ sudo vi script.py
import math print(math.sqrt(16))
To execute the script, type:
(my_env_project) linuxopsys@ubuntu:~$ python script.py
The script is being executed inside the virtual environment called named my_env_project.
Delete Virtual environment
To exit from virtual environment use exit or Ctrl+d command. To delete a virtual environment run the following command:
(my_env_project) linuxopsys@ubuntu:~$ deactivate
The above command won't remove my_env_project directory, simply use rm command to delete it.
Through this article, you learned how to properly create a virtual environment for your Python 3 projects on Ubuntu 22.04. We have also gone through a practical example on how to install a package and run a script inside the virtual environment.