1

Recently I have started to use virtualenv, and it has changed my life, and made so many things so much easier. However, some packages I would like available in all virtualenvs, and I haven't figured out how to do this. Is there a way to have pip install certain packages every time I create a new virtualenv?

elethan
  • 16,408
  • 8
  • 64
  • 87

3 Answers3

2

From documentation: If you build with virtualenv --system-site-packages ENV, your virtual environment will inherit packages from /usr/lib/python2.7/site-packages (or wherever your global site-packages directory is).

Sergey Gornostaev
  • 7,596
  • 3
  • 27
  • 39
1

If there is a particular set you always want, which is distinct from the global site-packages collection, you could write a simple wrapper script which sets up the env, activates it, and installs those packages.

#!/bin/sh
virtualenv "$1"
. "$1"/bin/activate
pip install six  # f'rinstance

If you save this as venvwrapper you cound alias virtualenv=venvwrapper in your .bashrc or similar.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

While the other answers to this question are both very valuable and informative, I was looking for a solution similar to that suggested by @Sergey Gornostaev. However, I am on Ubuntu and install most of my Python packages (at least the ones I want available globally) through apt. That means my packages end up in dist-packages while site-packages remains empty, so I can't use @Sergey's solution. Instead, I used the solution in this answer.

Basically, I install the packages I want available in all environments through apt if possible. Then I add the following (or some variation of it) to my .bashrc:

export PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages

This way all those packages are available, and then I can install indivdual packages sepecifc to a given virtual environment through pip from within the virtualenv. It might not be an ideal solution, but it meets my current needs, so this is what I will be using for now.

Community
  • 1
  • 1
elethan
  • 16,408
  • 8
  • 64
  • 87