Installing and Using Python Virtual Environments in Ubuntu - Venv tutorial
Last update: 11-09-2024
Understanding Python Virtual Environments
What are virtual environments?
Virtual environments in Python are isolated spaces that allow developers to manage project-specific dependencies without interfering with system-wide Python installations. They act as self-contained directories containing a Python installation for a particular version of Python, along with additional packages.
Benefits of using virtual environments
- Dependency isolation: Each project can have its own set of dependencies.
- Version control: Easily manage different versions of packages for different projects.
- Clean development: Prevents cluttering of the global Python installation.
- Reproducibility: Ensures consistent development environments across different machines.
How virtual environments work in Ubuntu
In Ubuntu, virtual environments operate by creating a directory structure that includes:
- A copy of the Python binary
- A site-packages directory for installing dependencies
- Scripts to activate and deactivate the environment
When activated, the virtual environment modifies the system path to prioritize its own Python interpreter and packages. This ensures that any Python commands or scripts run within the environment use the isolated resources.
To create a virtual environment in Ubuntu, you typically use the venv
module, which is included in Python 3.3 and later versions. The process involves:
- Creating the environment directory
- Activating the environment
- Installing project-specific packages
Setting Up Python on Ubuntu
A. Checking existing Python installation
Before installing Python, it's wise to check if it's already installed on your Ubuntu system. Many Ubuntu distributions come with Python pre-installed. To verify, open a terminal and run:
python3 --version
If Python is installed, you'll see the version number. If not, you'll receive an error message.
B. Installing Python if needed
If Python isn't installed, run these commands:
sudo apt update
sudo apt install python3
With Python successfully installed on your Ubuntu system, you're now ready to move on to the next step: installing virtual environment tools.
Installing Virtual Environment Tools
A. Installing venv module
The venv
module is Python's built-in tool for creating virtual environments. It's typically included with Python 3.3 and later versions. To ensure you have it installed, run the command:
sudo apt-get install python3-venv
This command will install or update the venv
module if needed.
Now that we have Python set up on Ubuntu, we can now install essential tools like pip
.
B. Installing pip
pip
is the package installer for Python. To install it, run:
sudo apt-get install python3-pip
This will install pip
for Python 3.
Creating a Virtual Environment
Now that we have the necessary tools installed, let's dive into creating a Python virtual environment on Ubuntu.
Using venv to Create a New Environment
To create a new virtual environment, use the following command:
python3 -m venv .venv
This command creates a new virtual environment in the .venv
folder.
Choosing a Location and Creating a Folder for Your Virtual Environment
Notice that the name we chose for our virtual environment starts with a dot. This is not by mistake. If a folder's name starts with a dot, it will be a hidden folder
, and using a hidden folder for a virtual environment is a widely used convention.
.venv
folder will contain the isolated environment for a specific project.
Activating the Virtual Environment
To begin working in your virtual environment, you need to activate it. Run the following command:
source .venv/bin/activate
Once activated, the command line prompt will change to show the name of the virtual environment, typically (.venv)
. This indicates that any Python commands or package installations will apply only within this environment, keeping your global system untouched.
Installing Packages with pip
With the virtual environment activated, you can install packages using pip
. For example:
pip install requests
This package will only be available within this virtual environment, ensuring project isolation.
Remember to activate your virtual environment each time you work on your project to ensure you're using the correct package versions.
Running the Virtual Environment
Now that we've created our virtual environment, let's put it to use by running a simple example script and learning how to deactivate it when we're done.
A. How to run a simple example script that uses the package we installed with pip
Once your virtual environment is activated, you can run Python scripts that utilize the packages you've installed. Here's an example program that makes use of the requests
package we installed as an example:
import requests
response = requests.get('https://api.github.com')
print(f"GitHub API Status Code: {response.status_code}")
Save the file as a Python file, for example: python_example.py
You can then run the Python script (make sure the virtual environment is still activated):
python3 python_example.py
This example script makes a request to the GitHub API and prints the HTTP status code of the response. It helps confirm that your virtual environment is correctly using the requests
package to handle HTTP requests.
B. How to deactivate the virtual environment
When you're finished working in your virtual environment, it's important to deactivate it. This ensures that you return to your system's global Python environment. Here's how:
- In the terminal where your virtual environment is active, simply type:
deactivate
- You'll notice that the virtual environment name disappears from your command prompt, indicating you're back in the global environment.