(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
Python virtualenv Guide
Updated on Jan 07, 2020
This tutorial need pip, if you have not already done so, first go through installing pip.
virtualenv is a tool used to separate different dependencies required by the projects. While working on multiple projects it's a common issue that one project need a version of package that is completely different from the other one, virtualenv helps us to resolve such kind of issues. It also helps to prevent polluting global site package.
Installing virtualenv #
virtualenv is just a package available at pypi, you can use pip to install virtualenv.
pip install virtualenv
After installation you may need to add
C:\Python34\Scripts to your
PATH environment variable. This way commands like pip, virtualenv will become available in any directory level.
Creating a Virtual Environment #
Create a new directory called
python_project and change current working directory to
mkdir python_project cd python_project
To create a virtual environment inside
python_project you need to issue the following command.
This will create a new folder
python_project. This folder will contain a copy of python executables and pip library used to install packages. Here we have used
my_env as name, but you can use anything you want. Now your virtual environment is ready to use, you just need to activate it.
There is one point in this tutorial we have installed virtualenv using python 3.4 suppose you also have python 2.7 and want to create a virtual environment that use python 2.7 instead of 3.4, you can do so using the following command.
virtualenv -p c:\Python27/python.exe my_env
Activating Virtual Environment #
If you are on windows you need to execute the following command.
On Linux enter this.
After issuing the above command your command prompt string will change and will look something like,
( my_env ) Path_to_the_project: $
( my_env ), this indicates that you are now operating under virtual environment.
Now you virtual environment is activated. Anything you install here will be used by this project only.
Let's try to install requests package.
In Windows enter the following code.
my_env\Scripts\pip.exe install requests
You can't use use just pip install requests in windows because it would execute the global pip if you have added
C:\Python34\Scripts to your
PATH environment variable. If you have not added then you will get an error.
Similarly, in Linux you need to execute the following code
my_env\Scripts\pip install requests
Deactivating Virtual Environment #
To deactivate virtual environment you need to use the following command.
This command will put you back in system's default python interpreter, where we can install the package in the global site package.
You should now able to see the motivation behind using virtualenv. It helps us to organise the needs of projects without conflicting with each other.
Other Tutorials (Sponsors)
This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!