When working with Python projects on VPS, it’s essential to isolate dependencies for each one to avoid version conflicts and maintain clean environments. This is where virtualenv comes in — a tool that helps you create independent Python environments.
In this guide, you’ll learn how to install virtualenv, create a virtual environment, and use it with Python 3.
virtualenv
is a tool that allows you to create isolated environments for Python projects. Each environment can have its own Python interpreter and installed packages — without affecting the global Python installation.
✅ Benefits:
Avoid dependency conflicts
Keep global Python clean
Easily manage project-specific libraries
Make sure you have Python 3 and pip installed. Check by running:
If not installed, get Python 3 from https://python.org.
virtualenv
Install virtualenv
globally using pip
:
To verify:
Navigate to your project folder or create a new one:
Then create the virtual environment (you can name it anything, commonly it’s called venv or .env):
You can also specify Python version explicitly:
This creates a folder named venv/ that contains a local Python interpreter and a local pip.
After activation, your shell prompt will change (you’ll see the name of the environment), and any Python packages installed will only apply inside this environment.
Once activated, you can install packages like usual:
You can freeze the environment’s dependencies into a file:
Later, you can recreate the same environment on another system with:
When you’re done:
This returns you to the global Python environment.