How to use virtualenv
How to use virtualenv
How to Use virtualenv in Python
Have you tried to install a Python package for a new project, just to see your other projects break because of some compatibility issues? You can avoid this with the help of 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.
Let’s get started!
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.
Next, you can install virtualenv :
Now that virtualenv is installed, let’s create a virtual environment in Python called mytest :
You will get an output similar to this one:
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:
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?
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.
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:
Once this is done, we can place our project’s dependencies in a requirements.txt file:
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:
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.
How to use virtualenv for Python
Virtualenv is used in python to separate the python used for your project and the one that is in your OS. This also includes the pypi packages. Please take note that i am using Linux mint for this how-to, but other ubuntu variant or linux distro should have no problem.
The way i am using virtualenv is to ensure i don’t confuse myself with system-wide python and the one that i am using for development. A while ago, i tend to run python 3.* with command “python3” which is a temporary solution for development.
First, you need virtualenv installed, you may use pip as follow (from python version 3.4 onwards virtualenv is bundled with the standard library)
(to install to user only)
(to install to system-wide)
i am going to test virtualenv in my workspace folder
next lets make a new folder called “python3_test” and enter it
next we specify the python version we are going to use. Find the location by using “which” command, here are some hints;
i am going to use python 3.5 so in the “python3_test” folder i am going to generate a virtualenv using python3.5 in a folder called env
now we can test the python version
what? but why? well using virtualenv you need to activate is first.
now we check our python version
nice! now you can try to deactivate the virtualenv and try to check the python version
*Important: every time you want to use virtualenv in a folder, you need to use “source env/bin/activate”
If you are afraid you might forgot to run “source env/bin/activate” every time you enter the directory, then continue reading.
We are going to use autoenv (https://github.com/kennethreitz/autoenv). Install autoenv as follow;
now lets test the autoenv
if deactivate command is not found, no worries, it means virtualenv is already deactivated
go up one folder
then enter our python3_test folder
then you find this warning
type “y” to allow, and check your python version
How to use virtualenv with Python? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
I am planning to install a virtual environment for Python in order to keep my Python packages separate. One of the motivations for this is also to have two versions of Python on my machine (Ubuntu 14.04) co-existing. I have the following wonders:
I would like to know experts opinion in order to do things in the right manner and wisely.
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Now, how to install Python For Ubuntu 14.04 you will have python2.7 and python3 already installed, with «python» being an alias to python2.7 by default.
Pip you can install with :
I don’t know how pip for py2 and pip for py3 coexist but they are available as separated packages.
VirtualEnv You can use pip to install virtualenv:
Here I’m using pip for python2
Once a have all set I do the following:
I usually keep source and env names constant in every project because I have some hooks around but I recommend you to replace the names, specially «env» because it’s the key to know in which VirtualEnv you are working, since you will get something like this:
I’ve also keep env out of source to simplify things with version control (no need for marking it for ignoring) but that is just me.
To activate virtualenv:
Now you can pip install inside the VirtualEnv. To change projects exit your current VirtualEnv with:
How to use Virtualenv?
Last updated on July 27, 2020
What is Virtualenv? #
Virtualenv is a handy tool for creating Python Virtual Environments.
So Why do we need Virtual Environments?
You can think of Virtual Environments as a separate Python installation which allows us to work on projects using different versions of the same package without conflicting with one another. Consider the following example:
Let’s say we are working on an e-commerce website and a CRM. Our e-commerce website depends upon version 1 of the foo package but our CRM needs version 2. At a time, we can only work with one version of the foo package. We can’t have both versions simultaneously on the system. A virtual environment can resolve these kinds of problem easily.
Another excellent use case of Virtualenv is that when you are working on a system and you don’t have the privilege to install packages globally. What you can do is create a virtual environment in your home directory and then install all the dependencies of the project inside the virtual environment.
Even if you don’t work with conflicting packages and have full access to the system, it is still a good idea to always start your new project in a virtual environment that way if anything goes wrong with the project, your system-wide Python installation will remain intact.
Now you the Why of Virtualenv let’s get into the How?
Installing Virtual #
To install Virtualenv type the following command:
Creating Virtual Environment #
We now ready to create virtual environments. Create a new directory my-project and change your current working directory to this directory using the cd command:
To create the virtual environment type the following:
Alternatively, you can also use the following command:
This will create a new a new directory named env in your current working directory. The structure of the env directory should look like this:
So what are these files and folder?
These files and folder constitute a separate Python installation.
The bin/ folder contains all the executables that you would find in an ordinary Python installation:
This will create a virtual environment using Python 2.7 instead of Python 3.5.
Activating the Virtual Environment #
To use a virtual environment, we first have to activate it. Activate virtual environment by typing the following command:
If you are on Windows use the following command:
Our virtual environment is now active. Did you notice (env) in front of the shell prompt? It indicates that the virtual environment named env is up and running.
Once virtual environment is activated, any package you add or remove using the pip will only affect the virtual environment you are working on. Packages installed at system-wide installation will not be affected at all.
Deactivating the Virtual Environment #
Once you are done working with the Virtualenv you can deactivate it using the deactivate command:
Programming & Databases
© 2007-2021 All Rights Reserved
Virtualenv Tutorial
Table of Contents
What Virtualenv Is
Virtualenv is a great piece of software. It allows to create virtual environments. Each of them can have a different version of Python and different sets of libraries.
While creating this tutorial I used following tools and versions:
Do I Need a Virtual Environment?
As usually: it depends. Normally you can create and use Python programs without that, but using virtual environments can help a lot.
Such a virtual environment provides many possibilities such as:
Basic Steps with Virtualenv
Installation
Installation is very easy. The best way is to perform a system wide installation so all users can create, and use the virtualenv. This must be done from an administrator account. I use Ubuntu on my laptop and I will use the sudo command, on other systems that could be done some other way.
The command for installing is simple:
A successful command execution should print on the console something like this:
Creating the First Virtual Environment
Creating the virtual environment is very easy. First of all choose the directory where you want to create it. The best location would be your home directory.
Now let’s create the first virtual environment inside this directory. Normally that’s done using the command:
but first let’s talk a bit about the most useful parameters that we can pass there.
Possible Parameters for Virtualenv
How to Create The Virtual Environment for Python
I will change now the command shown earlier by adding some parameters. I don’t want to use any preinstalled packages from my operating system, so the command looks like this:
Output for the command should be like this:
Using the Virtual Python Environment
Activating the Environment
Using the virtual environment is pretty simple. First of all you have to define what environment you want to use. So far there is just one environment but later will be more.
Let’s use this one environment. The environment directory is
the command for loading this environment is:
A successful execution changes the prompt, now it looks like this:
The first part of the prompt is the name of the virtual environment so it is very easy to see which environment is currently used. Of course you can change the prompt to the previous version but I wouldn’t recommend that as it can become very messy when using more environments.
Deactivating The Current Environment
Current environment can be easily deactivated using command
that reverts the prompt to previous version and now it looks like this:
The deactivate command exists only when a virtual environment is active, so don’t be surprised that there isn’t any when you outside the environment.
Installing Packages in Environment
Checking What is Installed
Installation is quite simple:
Usage is even simpler:
Running that command outside the virtual environment shows me just 114 packages so I will not paste the whole list here.
Using the yolk in the new environment needs installing that locally, so let’s install:
Now I can use that locally. On the new virtual environment there isn’t much packages:
Installing Other Packages
Now I can install all packages normally, just in the one environment. It will not touch any other packages from other environments, and even the ones globally packages.
Now I will install Pylons in the environment, but first let’s create other one to ensure that the packages are really installed just in this one.
OK, now there is another environment, let’s check what packages are installed there (of course after installing yolk like in the previous one):
Great, the list looks exactly like the previous one.
Now I just switch to the first environment and install Pylons.
This command prints a lot in the console as it installs many other packages needed by Pylons. The last lines of the messages list look like this:
And also let’s install SQLAlchemy library:
And now let’s check the list of installed packages:
As you can see there is installed the version 0.6.0 of SQLAlchemy library.
Great, it just works but what about the second environment? Does it have those packages also installed?
It seems that everything is OK, so let’s install there Pylons and check the list of installed packages:
Great, but there is no SQLAlchemy. So let’s install that but using the previous version, that means installing the version 0.5.0.
Looks fine, lets check the installed version:
The Summary
Now I’ve got two Python environments in directories:
in both there is a different version of Pylons installed.
Now that’s easy to test the behaviour of the same Pylons application on both versions of SQLAlchemy without any changes in configuration.