0

I'm really confused with how virtualenv deals with packages. I'm on OSX and installed python2 and 3 with Homebrew followed by pip install virtualenv.

in terminal:

cd Virtualenv/MyTestEnv
. bin/activate
pip install numpy

would install numpy into my virtualenv folder which can only be accessed if I run my program within that env. From what I read, it does this by modifying the system $PATH. However when I try running a program with numpy I can't:

(MyTestEnv)___________________
| ~/desktop/Python @ My-MBP (chronologos) 
| => ./wordsrt.py
Traceback (most recent call last):
  File "./wordsrt.py", line 2, in <module>
    import numpy
ImportError: No module named numpy

the program has only two lines:

#!/usr/bin/env python
import numpy

And when I do pip list numpy is shown as installed? Is it a problem with the hashbang? Help would be appreciated!

chronologos
  • 338
  • 3
  • 13
  • 1
    First, `$PATH` is irrelevant to where the site-packages go and are found; that's what the shell uses to find executables. `$PYTHONPATH` is what specifies the starting values for Python's `sys.path`. – abarnert Jan 22 '14 at 01:40
  • 1
    However, it's possible that you are somehow running the system-wide `pip` instead of the virtualenv's `pip`. If you're using a recent-enough version, I believe it can detect that you're running the system `pip` while inside a virtualenv and either give you an error/warning or install into the virtualenv anyway, so… are you using `pip` 1.5? If not, try upgrading that first. – abarnert Jan 22 '14 at 01:43
  • Also, remember that OS X already comes with its own Python 2.7, so by installing another one via Homebrew, you're opening a can of worms you may not want to deal with. For example, the first `pip` on your path can easily be the Apple Python 2.7 `pip`, while `python` is the Homebrew Python 2.7 (especially since both installations like to install scripts into `/usr/local/bin`, and Python's standard attempt to disambiguate—giving `python2.7` and `pip-2.7` and similar names—doesn't help when they're both 2.7). So, if you don't actually need the Homebrew 2.7, I'd uninstall it. – abarnert Jan 22 '14 at 01:45
  • Thanks for all your answers guys. I posted what I did below. – chronologos Jan 22 '14 at 01:56

2 Answers2

1

This is the problem:

#!/usr/bin/env python

Another way to run python from the virtualenv that to me feels more natural is

MyTestEnv/bin/python wordsrt.py

Try this.

Jasper van den Bosch
  • 3,169
  • 4
  • 32
  • 55
0

I managed to solve my problem.

Firstly I modified my bashrc to only allow pip when virtualenv is on:

# pip should only run if there is a virtualenv currently activated
export PIP_REQUIRE_VIRTUALENV=true
# cache pip-installed packages to avoid re-downloading
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache
syspip(){
   PIP_REQUIRE_VIRTUALENV="" pip "$@"
}

Then to ensure user-installed binaries take precedence I added this to my bash_profile export PATH=/usr/local/bin:$PATH

chronologos
  • 338
  • 3
  • 13