Question

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!

Was it helpful?

Solution 2

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top