Choose a different version or distribution
Introduction
Before we begin talking about how to create Python Virtual Environments on Ubuntu 18.04, let's briefly understand – What is a Python Virtual Environment?
A Python virtual environment is a directory tree that contains a Python installation as well as a number of additional packages.
The primary objective of Python virtual environments is to establish an exclusive environment for various Python projects. In this manner, you can install a particular version of a module for each project without being concerned that it will have an impact on your other Python projects.
In this tutorial, you will create Python Virtual Environments on Ubuntu 18.04. We will also address a few FAQs on how to create Python Virtual Environments on Ubuntu 18.04.
Create Virtual Environment for Python 3
Python 3.6 is preinstalled in Ubuntu 18.04 by default. Run the following command to see if Python 3 is installed on your system:
python3 -V
The output should seem as follows:
Output
Python 3.6.5
Since Python 3.6, using the venv
module is the suggested method for building a virtual environment.
Let us begin by installing the python3-venv
package, which includes the venv
module.
sudo apt install python3-venv
As soon as the module is installed, Python 3 virtual environments can be created.
Navigate to the directory in which you want to store your Python 3 virtual environments. Run the following command within the directory to create your new virtual environment:
python3 -m venv my-project-env
The preceding command creates the directory my-project-env
, which contains a copy of the Python binary, the Pip package manager, the standard Python library, and other supporting files.
You must run the activate
script in order to activate this virtual environment to start using it:
source my-project-env/bin/activate
Once enabled, the virtual environment's bin directory will be added to the start of the$PATH
variable. The name of the virtual environment you are now using will also be displayed in a new prompt in your shell. In our example, that would be my-project-env
:
Output
$ source my-project-env/bin/activate
(my-project-env) $
Now that the virtual environment has been activated, we may use pip to install, upgrade, and remove packages.
Let us use the Requests module to create a basic Python script.
Installing the module using Python's package manager, pip, is the first step.
pip install requests
Try importing the module to confirm the installation:
python -c "import requests"
Importing the module successfully indicates there are no issues, hence the installation was successful.
In this example, we will use the httpbin.org website, which offers a simple HTTP Request & Response service, to print the header entries.
Make a new file in your text editor:
nano testing.py
Copy and paste the content given below into the file:
import requests
r = requests.get('http://httpbin.org/get')
print(r.headers)
Close the file and save it.
Now, type the following to run the script:
python testing.py
As seen below, the script will print a dictionary of every header entry:
Output
{'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Date': 'Tue, 18 Sep 2018 16:50:03 GMT', 'Content-Type': 'application/json', 'Content-Length': '266', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Via': '1.1 vegur'}
Once you have finished your work to deactivate the environment, just type deactivate
to go back to your regular shell.
deactivate
FAQs to Create Python Virtual Environments on Ubuntu 18.04
How do I create a new Python Virtual Environment?
To create a new virtual environment, use the command: virtualenv myenv
.
How do I activate a virtual environment?
To activate your virtual environment, run: source myenv/bin/activate
.
How do I install packages inside a virtual environment?
After activating the virtual environment, you can use pip
to install packages, such as: pip install package_name
.
How do I deactivate a virtual environment?
To deactivate the virtual environment, simply run: deactivate
.
How can I check the installed packages in my virtual environment?
You can use the command pip freeze
to list all installed packages in the current environment.
How can I use a specific Python version in the virtual environment?
You can specify a Python version when creating the virtual environment using the --python
flag, such as: virtualenv --python=python3 myenv
.
How do I delete a virtual environment?
To delete a virtual environment, you can simply delete the associated directory with the command: rm -r myenv
.
Conclusion
You now know how to create and use Python virtual environments. You can create more virtual environments for your Python projects by following the steps we discussed above.
If you have any queries, feel free to post a comment below, and we'll be happy to answer them.