Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to activate virtualenv on Linux?
A virtual environment is a tool that helps keep dependencies required by different projects in separate places. In Python, we commonly use the term venv to refer to virtual environments. This isolation prevents conflicts between different project dependencies and keeps your system Python installation clean.
Python's venv module creates lightweight virtual environments with their own site directories, isolated from the system's site directories. Each virtual environment has its own Python binaries and can maintain its own set of installed packages independently.
Creating a Virtual Environment
To create a virtual environment, use the following command structure ?
python3 -m venv /path_to_new_virtual_environment
Example ? Creating a Virtual Environment
Here's a practical example creating a virtual environment on Linux ?
python3 -m venv /home/user/myproject-env
After running this command, navigate to the directory where you created the virtual environment. You should see a structure similar to this ?
user@linux:~/myproject-env$ ls -la total 20 drwxr-xr-x 5 user user 4096 Jul 5 20:52 . drwxr-xr-x 3 user user 4096 Jul 5 20:52 .. drwxr-xr-x 2 user user 4096 Jul 5 20:52 bin drwxr-xr-x 2 user user 4096 Jul 5 20:52 include drwxr-xr-x 3 user user 4096 Jul 5 20:52 lib -rw-r--r-- 1 user user 90 Jul 5 20:52 pyvenv.cfg
Activating the Virtual Environment
Once created, you need to activate the virtual environment to use it. This is the key step that switches your shell to use the virtual environment's Python interpreter and packages.
source /path_to_virtual_environment/bin/activate
Example ? Activating on Linux
source /home/user/myproject-env/bin/activate
After activation, your command prompt will change to show the virtual environment name ?
(myproject-env) user@linux:~$
Deactivating the Virtual Environment
To return to your system's default Python environment, simply run ?
deactivate
Available venv Options
The venv module provides several useful options. To see all available arguments ?
python3 -m venv --help
| Option | Description |
|---|---|
| --system-site-packages | Give access to system site-packages |
| --symlinks | Use symlinks instead of copies |
| --clear | Delete environment contents if it exists |
| --without-pip | Skip installing pip |
| --prompt PROMPT | Alternative prompt prefix |
Verifying Virtual Environment Status
To check if your virtual environment is active and working correctly ?
which python pip list
The first command should show the path to your virtual environment's Python binary, and the second will show only the packages installed in the virtual environment.
Conclusion
Virtual environments are essential for Python development on Linux, allowing you to isolate project dependencies and avoid conflicts. The key steps are creating with python3 -m venv, activating with source bin/activate, and deactivating when done. This workflow ensures clean, reproducible Python environments for your projects.
