A Python virtual environment is a tool to isolate specific Python environments on a single machine, allowing for separate dependencies and packages for different projects. This is useful in cases where you need to work on multiple projects with conflicting package requirements or where you want to test code in an environment isolated from your main installation.
There are several tools available for creating and managing Python virtual environments, including venv
, virtualenv
, pipenv
, and conda
. This blog will explore these tools and provide examples of how to use them on both Windows and Mac systems.
Creating a Python Virtual Environment with venv
venv
is a built-in module in Python 3.3 and above for virtual environments. It is the recommended tool for creating virtual environments in the Python documentation.
To create a virtual environment with venv
, open a terminal or command prompt and navigate to the desired directory where you want to create the environment. Then, use the python -m venv
command followed by the name you want to give to your environment. For example:
python -m venv myenv
This will create a directory called myenv
in the current directory, containing a copy of the Python executable and the necessary files for a basic Python installation.
To activate the virtual environment on Windows, navigate to the Scripts
directory within the environment and use the activate.bat
script:
cd myenv\Scripts
activate.bat
On Mac or Linux systems, use the activate
script:
cd myenv/bin
source activate
To deactivate the virtual environment, use the deactivate
command:
deactivate
Creating a Python Virtual Environment with virtualenv
virtualenv
is a third-party Python package that provides support for creating multiple isolated Python environments. It is compatible with Python 2.7 and above and can be installed using pip
.
To install virtualenv
, open a terminal or command prompt and use the pip install
command:
pip install virtualenv
To create a virtual environment with virtualenv
, navigate to the desired directory and use the virtualenv
command followed by the name you want to give to your environment. For example:
virtualenv myenv
This will create a directory called myenv
in the current directory, containing a copy of the Python executable and the necessary files for a basic Python installation.
To activate the virtual environment on Windows, navigate to the Scripts
directory within the environment and use the activate.bat
script:
cd myenv\Scripts
activate.bat
On Mac or Linux systems, use the activate
script:
cd myenv/bin
source activate
To deactivate the virtual environment, use the deactivate
command:
deactivate
Listing Packages in a Python Virtual Environment
To list the packages installed in a Python virtual environment, you can use the pip freeze
command. This will display a list of all the package names and their versions, as well as the version of pip
and the Python interpreter being used.
To use pip freeze
, activate the virtual environment, and then run the command:
pip freeze
Introduction to pipenv
pipenv
is a tool that combines pip
and virtualenv
into a single command-line tool. It is designed to manage Python packages and virtual environments in a more user-friendly way, making it easier to manage dependencies and keep track of which packages are installed in each environment.
To install pipenv
, open a terminal or command prompt and use the pip install
command:
pip install pipenv
To create a virtual environment and install packages using pipenv
, navigate to the desired directory and use the pipenv install
command followed by the package name. For example:
pipenv install numpy
This will create a virtual environment in the current directory and install the numpy
package. The virtual environment and installed packages are tracked in a Pipfile
and Pipfile.lock
file, which are used to manage dependencies and ensure that the same packages are installed in the environment every time it is created.
To activate the virtual environment, use the pipenv shell
command:
pipenv shell
To deactivate the virtual environment, use the exit
command:
exit
Introduction to conda
conda
is a package and environment manager for Python and other languages, such as R, C++, and Fortran. It is commonly used in scientific computing and data analysis and is included in the Anaconda distribution of Python.
To create a virtual environment with conda
, open a terminal or command prompt and use the conda create
command followed by the name you want to give to your environment and the packages you want to install. For example:
conda create -n myenv numpy pandas
This will create a virtual environment called myenv
and install the numpy
and pandas
packages.
To activate the virtual environment, use the conda activate
command:
conda activate myenv
To deactivate the virtual environment, use the conda deactivate
command:
conda deactivate
Comparison of venv, virtualenv, pipenv, and conda
Tool | Description | Compatibility | Package Management |
---|---|---|---|
venv | Built-in module for creating virtual environments in Python 3.3 and above. | Python 3.3 and above | pip |
virtualenv | Third-party package for creating isolated Python environments. | Python 2.7 and above | pip |
pipenv | A tool that combines pip and virtualenv into a single command-line tool. Designed to make package and environment management more user-friendly. | Python 2.7 and above | pip |
conda | Package and environment manager for Python and other languages, such as R, C++, and Fortran. Commonly used in scientific computing and data analysis. Included in the Anaconda distribution of Python. | Python and other languages | conda |
In summary, the main differences between these tools are as follows:
venv
is a built-in module in Python 3.3 and above for creating virtual environments. It is the recommended tool for creating virtual environments in the Python documentation.virtualenv
is a third-party Python package that provides support for creating multiple isolated Python environments. It- is compatible with Python 2.7 and above and can be installed using
pip
. pipenv
is a tool that combinespip
andvirtualenv
into a single command-line tool. It is designed to manage Python packages and virtual environments in a more user-friendly way, making it easier to manage dependencies and keep track of which packages are installed in each environment.conda
is a package and environment manager for Python and other languages, such as R, C++, and Fortran. It is commonly used in scientific computing and data analysis and is included in the Anaconda distribution of Python.
In terms of which tool to use, it ultimately depends on your needs and preferences. venv
is the recommended tool for creating virtual environments in Python documentation, and it is a good choice if you are working with Python 3.3 or above. virtualenv
is a good choice if you are working with an older version of Python or prefer a third-party package. pipenv
is a good choice if you want a more user-friendly tool for managing packages and virtual environments. conda
is a good choice if you are working with other languages besides Python or if you are working in a scientific computing or data analysis setting.
Activating a Virtual Environment on Windows
To activate a virtual environment on Windows, navigate to the Scripts
directory within the environment and use the activate.bat
script:
cd myenv\Scripts
activate.bat
This will change the prompt to indicate that the virtual environment is active and any packages installed in the environment will be available.
To deactivate the virtual environment, use the deactivate
command:
deactivate
Frequently asked questions
Do I need to use a virtual environment?
It is not required to use a virtual environment, but it is generally a good practice to do so. Virtual environments allow you to isolate specific Python environments on a single machine, which can be helpful in cases where you need to work on multiple projects with conflicting package requirements or where you want to test code in an environment isolated from your main installation.
Can I use multiple virtual environments at the same time?
Yes, you can use multiple virtual environments at the same time by activating and deactivating them as needed.
Can I install packages globally in a virtual environment?
By default, packages are installed locally within a virtual environment and are only available to that environment. However, you can use the --system
flag with the pip
or conda
install command to install a package globally. This is generally not recommended, as it can lead to conflicts with other environments or the main Python installation.
Can I share a virtual environment with others?
Yes, you can share a virtual environment with others by providing them with a copy of the environment directory and the necessary instructions for activating and using it. You can also use the pip freeze
command to create a list of the packages installed in the environment, which can be used to recreate the environment on another machine using the pip install -r
command.
Can I use a virtual environment with an IDE?
Many IDEs, such as PyCharm and Visual Studio Code, allow you to select a specific virtual environment as the interpreter for a project. This allows you to use the packages and dependencies specific to that environment within the IDE.
Very good article
Glad that you like it 馃檪
Thanks for the article. I have a question, How do you activate an environment in a server? I have created it using the instruction (python -m venv myenv), but I don’t know how activate. Best regards
Is it windows or Linux?
Windows:
venv\bin\activate.bat
Linux:
source venv/bin/activate
OR
. venv/bin/activate