4

After I perform source activate on a virtualenv, and I perform the following: pip install requests I get this:

sudo pip install requests
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/dist-packages
Cleaning up...

now when I run python and try to import it on python while using the virtualenv python

>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named requests
>>>

So then I figured, oh maybe it's looking at the wrong python, so I did a which python and which pip

(dev)sshum@mysite:~/dev/bin$ which python
/var/www/mysite/mysite/dev/bin/python
(dev)sshum@mysite:~/dev/bin$ which pip
/var/www/mysite/mysite/dev/bin/pip

However, if I deactivate and try to import these packages, it imports successfully. Not entirely sure what I'm doing wrong.

Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144

3 Answers3

0

This error happens to me, when I install by pip from another console, thinking I am still in activated virtualenv, but in fact I am in deactivated system Python.

Using byobu, I enjoy having multiple consoles open at once on one screen.

  1. Using F2 I open new full window
  2. I activate the virtualenv, I want to work on
  3. For some reason, I split the screen and work in the new split

If I forget to activate the virtualenv in the new split, I am running into the same kind of problems you describe.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • I'm running in one console. – Stupid.Fat.Cat May 21 '14 at 22:37
  • @Stupid.Fat.Cat When you create a new virtualenv - what is output of `$ pip freeze` command? How exactly do you create the virtualenv? By default, the virtualenv shall be well isolated from system Python, but there are options, which allow to share libraries from system. They might be activated either from command line, from system variables or by configuration files, see http://virtualenv.readthedocs.org/en/latest/virtualenv.html?highlight=config . – Jan Vlcinsky May 22 '14 at 09:21
  • virtualenv myenv to create it. – Stupid.Fat.Cat May 22 '14 at 12:19
  • @Stupid.Fat.Cat Try to run `$ virtualenv myenv` with `-v`, so `$ virtualenv -v myenv` and put to your question output from it. – Jan Vlcinsky May 23 '14 at 10:06
0

Found the solution after some tinkering. Seems like sudo pip install {package} changes the path of the install. And since the virtualenv was located in the var folder I needed root permissions to install things (which is bad practice I realized). Instead what I opted to do was su root and then pip install with the virtualenv activated.

Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
0

If everything else fails or you want to keep using sudo for some reason, just specify the path to the pip executable directly:

sudo ./my/virtualenv/bin/pip install foobar
Markus Unterwaditzer
  • 7,992
  • 32
  • 60