Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Virtual Environment for Python

I've been reading up on virtual environment, and it seems like an extremely useful tool, but now I'm questioning how I've set up my entire python environment thus far. Right now, all of the modules and packages that I have installed are residing in this directory:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

But the virtualenv docs seem to suggest that such universal system installs are a bad thing. If that's the case, then what should I do with my current modules and how should I install future modules? For instance, I recently installed flask from my user directory with this command:

pip install flask

It now resides in site-packages. Should I have done something different? I'm having trouble with the documentation, which seems to suggest that I need to go into a project directory, set up a virtual environment, and install all of the modules that I need using virtualenv. Is this the case? Is there any way to make things less cumbersome? It seems like installing potentially dozens of packages for every single project directory would be a little much.

Or is it the case that I only need to create virtual environments for projects that use older versions of modules than the ones I have installed in the system directory? If that's the case, however, then what's up with the virtualenv mantra that seems to discourage all system installs?

like image 426
user1427661 Avatar asked Feb 02 '13 22:02

user1427661


People also ask

How does a Python virtual environment work?

It Copies Structure and FilesWhen you create a virtual environment using venv , the module re-creates the file and folder structure of a standard Python installation on your operating system. Python also copies or symlinks into that folder structure the Python executable with which you've called venv : Windows. Linux.

What would you use a virtual environment in Python?

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.

Is virtual environment necessary for Python?

virtualenv avoids the need to install Python packages globally. When a virtualenv is active, pip will install packages within the environment, which does not affect the base Python installation in any way. (This is a typical Python installation; your system may vary.)


1 Answers

If you've already installed virtualenv like this:

pip install virtualenv

You'll then want to setup a particular virtualenv folder:

virtualenv [your project folder name]

This will create that project folder with a few important subdirectories.

You'll activate your virtualenv first before installing anything new, the newly installed modules will be available to you only when 'sourced' into your virtualenv. From your project folder type:

source bin/activate

You then will see your virtualenv name in parenthesis on each terminal line. This indicates you are 'sourced' in. NOW install stuff with pip or easy_install.

pip install flask

virtualenv basically sets your path to look in [venv folder]/bin for executables instead of /usr/local/bin or whatever. So you can copy files straight into your virtual env bin folder. (MongoDB files for instance just come in a zip/tar file, you can just untar them into your venv bin folder and you will have access to that particular version of MongoDB when 'sourced' in.) Try for yourself, run this command from your virtual and then default environment to see how it changes.

echo $PATH && echo $PYTHONPATH

To exit out of your virtualenv:

deactivate

Typing this will get you back to your default environment.

If you haven't read this yet, it's a pretty good resource.

https://python-guide.readthedocs.org/en/latest/dev/virtualenvs/

like image 158
Rob Carpenter Avatar answered Sep 27 '22 17:09

Rob Carpenter