Virtualenv for Python projects
What is Virtualenv ?
Virtualenv is a tool that lets you create an isolated Python environment for your project. It creates am environment that has its own installation directories, that doesn’t share dependencies with other virtualenv
environments. You can even configure what version of Python you want to use for each individual environment. It’s very much recommended to use virtualenv
when dealing with Python applications.
Installation
To install virtualenv
run:
pip install virtualenv
Usage
If you have a project in a directory called my-project
you can set up virtualenv
for that project by running:
cd my-project/
virtualenv venv
If you want your virtualenv
to also inherit globally installed packages run:
virtualenv venv --system-site-packages
These commands create a venv/
directory in your project where all dependencies are installed. You need to activate it first though (in every terminal instance where you are working on your project):
source venv/bin/activate
You should see a (venv)
appear at the beginning of your terminal prompt indicating that you are working inside the virtualenv
. Now when you install something like this:
pip install <package>
It will get installed in the venv/
folder, and not conflict with other projects.
To leave the virtual environment run:
deactivate
Important: Remember to add venv
to your project's .gitignore
file so you don't include all of that in your source code.
It is preferable to install big packages (like Numpy), or packages you always use (like IPython) globally. All the rest can be installed in a virtualenv
.