简体   繁体   中英

pip on virtualenv is not installing packages in the right python environment

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.

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.

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.

如果其他一切都失败了或者你想因某种原因继续使用sudo,只需直接指定pip可执行文件的路径:

sudo ./my/virtualenv/bin/pip install foobar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM