Jump to:
venv VS virtualenv
There are two commonly used libraries that can be used to create virtual environments in Python:
virtualenv
: a popular third-party package that provides a way to create isolated Python environments.venv
: a module in the Python standard library that provides similar functionality tovirtualenv
, but is included with Python 3.3 and later versions.
Both of these libraries can be used to create virtual environments for Python projects, and they provide similar functionality. The choice between them typically depends on your specific needs and preferences.
Feature | venv | virtualenv |
---|---|---|
Included with Python | Yes (3.3+) | No, requires separate installation |
Python version supported | Only Python 3 | Python 2 and Python 3 |
Customization options | Fewer options | More options |
Environment customization | Simple | More complex |
Compatibility with PyPI | Excellent (using pip) | Excellent (using pip) |
Size of virtual environments | Generally faster and more lightweight | Larger |
Upsides |
|
|
Downsides |
|
|
venv Step-by-Step Guide
- Open your terminal or command prompt.
- Create a new directory for your project:
mkdir my_project
- Navigate to the directory:
cd my_project
venv
is part of Python Standard Library, so if you have Python, you already have venv on your machine- Create a new virtual environment for your project by typing
python3 -m venv env
and pressing Enter. This will create a new directory called “env” in your project directory. - Activate the virtual environment:
- On Windows:
.\env\Scripts\activate
- On Linux/Mac:
source env/bin/activate
- On Windows:
- You should see the name of your virtual environment in your terminal prompt now.
- Install the necessary packages for your project using pip, for example:
pip install numpy
- When you’re done working on your project, deactivate the virtual environment by typing
deactivate
and pressing Enter.
The (myenv)
prefix should disappear from your terminal prompt, indicating that you have returned to the global Python environment.
virtualenv Step-by-step guide
Step-by-step guide to setting up a virtual environment for Python project:
- Open your terminal or command prompt.
- Create a new directory for your project:
mkdir my_project
- Navigate to the directory:
cd my_project
- You can check if virtualenv is installed on your machine by running:
virtualenv --version
- This will display the version of virtualenv if it is installed, or an error message
- If you see an error message, you can install
virtualenv
using pip:pip install virtualenv
- Create a new virtual environment for your project:
virtualenv env
and pressing Enter. This will create a new directory called “env” in your project directory. - Activate the virtual environment:
- On Windows:
.\env\Scripts\activate
- On Linux/Mac:
source env/bin/activate
- On Windows:
- You should see the name of your virtual environment in your terminal prompt now.
- Install the necessary packages for your project using pip, for example:
pip install numpy
- When you’re done working on your project, deactivate the virtual environment:
deactivate
That’s it! Your project dependencies will be isolated from the global Python environment, and you can easily switch between different virtual environments for different projects.