- How to Use virtualenv in Python
- Install virtualenv in Python
- How to Work With and Maintain virtualenv in Python
- In Mac or Unix
- In Windows
- Deleting Virtual Environments in Python
- What About pyenv and pyenv-virtualenv?
- Using virtualenv With Repositories
- Closing Thoughts on virtualenv in Python
- Installing packages using pip and virtual environments¶
- Installing pip¶
- Installing virtualenv¶
- Creating a virtual environment¶
- Activating a virtual environment¶
How to Use virtualenv in Python
virtualenv is a tool that allows you to create virtual environments in Python and manage Python packages. It helps you avoid installing packages globally; global installations can result in breaking some system tools or other packages.
For example, let’s say Project A and Project B require the same library. While this does not seem like a big deal at first, things can get difficult if you need different versions of the same library between Project A and Project B. This becomes a problem because Python cannot differentiate the version number in the site-packages directory.
This is where setting a virtual environment in Python is very useful. It is also an excellent practice to help you write better Python code.
In this article, we’ll show how to install virtualenv in Python. Then we’ll explore how to set up virtual environments in Python and work with repositories.
Install virtualenv in Python
A virtual environment in Python allows you to create an isolated environment for your projects. It means that your projects can have their own dependencies – independent of every other project’s dependencies.
With a Python virtual environment for each project, you are free to install different versions of the same Python package for each project. This is because every Python environment is independent of all the others.
At their core, virtual environments in Python are just directories containing a few scripts; consequently, you can set as many Python virtual environments as you like.
Let’s install virtualenv in Python!
virtualenv is easy to install. First, let’s update pip.
pip install --upgrade pip pip --version
Next, you can install virtualenv :
Now that virtualenv is installed, let’s create a virtual environment in Python called mytest :
virtualenv -p python3 mytest
You will get an output similar to this one:
created virtual environment CPython3.8.11.final.0-64 in 10455ms creator CPython3Windows(dest=C:\Users\xavie\mytest, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\xavie\AppData\Local\pypa\virtualenv) added seed packages: pip==22.0.3, setuptools==60.6.0, wheel==0.37.1 activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
And here we go! In the next section, we’ll explore using virtualenv in Python. You can find more information about virtualenv in the official Python documentation.
How to Work With and Maintain virtualenv in Python
Before installing or using packages in your new Python virtual environment, you need to activate it.
In Mac or Unix
If you are a Mac or Unix user, you can do it as follows:
Next, you can check that you are in a virtual environment with the following command:
It should be in the mytest directory:
And that’s it! Now you can start installing the required packages for your project.
In Windows
If you are a Windows user, you can activate virtualenv this way:
Now your prompt should be prefixed with the name of your environment; in this case, it’s mytest .
Next, you can check that you are in your Python virtual environment with the following command:
Like the Mac or Unix environment, it should indicate the mytest directory:
Now you can install all the packages you need. You can do so individually or with the help of a requirements.txt file. If you do not know how to do this, refer to my earlier article on how to create a Python requirements file.
But how does virtualenv work under the hood?
Once you activate your Python virtual environment, you get a different path for the Python executable. This is because the $PATH environment variable is modified in the active environment.
After activating your Python virtual environment, the bin directory is now at the beginning of the path, meaning that the shell uses your virtual environment’s instance instead of the Python system version.
Important: Don’t store your Python scripts and your requirements.txt file inside your Python virtual environment.
Deleting Virtual Environments in Python
The easiest way to delete a virtual environment in Python is to delete the folder manually. By leaving your Python scripts outside your virtualenv folder, you avoid the risk of deleting your whole project the next time you want to clear your Python virtual environment.
Also, you might want to use the same virtual environment for different projects. Keeping all your Python scripts outside your folder will make the whole process easier to handle.
If you are new to Python and want to improve your skills quickly, I highly recommend you check out our Python programming track.
What About pyenv and pyenv-virtualenv?
pyenv-virtualenv is a pyenv plugin to manage Python virtual environments. pyenv comes in handy when you need to install and switch between different Python versions; however, we cannot create virtual environments with arbitrary versions of Python.
With pyenv-virtualenv , it’s easier to switch between Python versions within different virtual environments. Feel free to read my article about pyenv if you want to learn more about this topic.
Using virtualenv With Repositories
Now, you might want to push your project on GitHub. After you’ve finished working in your Python virtual environment, you first need to initialize the repository:
Then, you need to include the mytest folder in a .gitignore file. This way, the Python virtual environment will be ignored in source control.
Once this is done, we can place our project’s dependencies in a requirements.txt file:
pip freeze > requirements.txt
The freeze command will read the dependencies and create a text file containing a list of dependencies and their version number.
Once this is done, we add the file to be pushed to the repository:
And finally, we commit the files and push the project to our repository.
I want to emphasize the importance of the requirements.txt file here. When the file is not there, it can be challenging for another person to use a project.
For example, let’s say you have an Open3D project to work on point clouds and you use the JVisualizer to run visualizations in Jupyter Notebook. You use Open3D 0.12.0 to build your project; later, you decide to upload the project on GitHub to share it with your friends. If you do not add a requirements.txt file and let your friends simply install the latest version of Open3D (0.14.1), they will not be able to run your Jupyter Notebook.
By providing the information to recreate the same virtual environment you used for your project, you will make everything run more smoothly for others. This will save you from headaches – after they’ve created their virtual environment, your colleagues would only need to enter the line below:
pip install -r requirements.txt
If you need more information on using GitHub, you can read Kateryna’s quick guide to Git here. And if you haven’t been keeping your requirements.txt file up to date, check out my article for an easy fix.
Closing Thoughts on virtualenv in Python
In this article, we learned how to set up a virtual environment in Python using virtualenv and why it’s important. We’ve also learned how to use a virtual environment in conjunction with GitHub.
virtualenv will make your life as a developer easier and help you write cleaner code. If you’re not yet doing so, I encourage you to develop the habit of setting up Python virtual environments when you start a new project.
Last but not least, you can find more tips to write better Python code here. And remember to visit LearnPython.com for more content.
Installing packages using pip and virtual environments¶
This guide discusses how to install packages using pip and a virtual environment manager: either venv for Python 3 or virtualenv for Python 2. These are the lowest-level tools for managing Python packages and are recommended if higher-level tools do not suit your needs.
This doc uses the term package to refer to a Distribution Package which is different from an Import Package that which is used to import modules in your Python source code.
Installing pip¶
pip is the reference Python package manager. It’s used to install and update packages. You’ll need to make sure you have the latest version of pip installed.
Debian and most other distributions include a python-pip package; if you want to use the Linux distribution-provided versions of pip, see Installing pip/setuptools/wheel with Linux Package Managers .
You can also install pip yourself to ensure you have the latest version. It’s recommended to use the system pip to bootstrap a user installation of pip:
python3 -m pip install --user --upgrade pip python3 -m pip --version
Afterwards, you should have the latest version of pip installed in your user site:
pip 21.1.3 from $HOME/.local/lib/python3.9/site-packages (python 3.9)
The Python installers for Windows include pip. You can make sure that pip is up-to-date by running:
py -m pip install --upgrade pip py -m pip --version
Afterwards, you should have the latest version of pip:
pip 21.1.3 from c:\python39\lib\site-packages (Python 3.9.4)
Installing virtualenv¶
If you are using Python 3.3 or newer, the venv module is the preferred way to create and manage virtual environments. venv is included in the Python standard library and requires no additional installation. If you are using venv, you may skip this section.
virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.
python3 -m pip install --user virtualenv
py -m pip install --user virtualenv
Creating a virtual environment¶
venv (for Python 3) and virtualenv (for Python 2) allow you to manage separate package installations for different projects. They essentially allow you to create a “virtual” isolated Python installation and install packages into that virtual installation. When you switch projects, you can simply create a new virtual environment and not have to worry about breaking the packages installed in the other environments. It is always recommended to use a virtual environment while developing Python applications.
To create a virtual environment, go to your project’s directory and run venv. If you are using Python 2, replace venv with virtualenv in the below commands.
The second argument is the location to create the virtual environment. Generally, you can just create this in your project and call it env .
venv will create a virtual Python installation in the env folder.
You should exclude your virtual environment directory from your version control system using .gitignore or similar.
Activating a virtual environment¶
Before you can start installing or using packages in your virtual environment you’ll need to activate it. Activating a virtual environment will put the virtual environment-specific python and pip executables into your shell’s PATH .
You can confirm you’re in the virtual environment by checking the location of your Python interpreter: