user1427661
user1427661

Reputation: 11794

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?

Upvotes: 7

Views: 6038

Answers (2)

thebjorn
thebjorn

Reputation: 27360

Before you put/support anything in production there is minimal benefit from a virtualenv. It's just an extra step to activate the virtualenv, and of course you have to install your standard environment in every virtualenv.. not really that much extra effort...

When you've put something in production, however, it's potentially a great win when things go bump in the night :-)

Upvotes: 0

Rob Carpenter
Rob Carpenter

Reputation: 689

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/

Upvotes: 14

Related Questions