简体   繁体   English

未找到在 virtualenv 中使用 PIP 安装的模块

[英]Module installed with PIP in virtualenv not found

Getting a very strange error.得到一个非常奇怪的错误。 I am making a virtual environment and initializing it with a pip requirements.txt file, but when I go to run code in the activated environment, the virtual environment interpreter claims to be missing some (and only some) of the modules:我正在创建一个虚拟环境并使用 pip requirements.txt 文件对其进行初始化,但是当我在激活的环境中运行代码时,虚拟环境解释器声称缺少一些(并且只有一些)模块:

(venv) $ pip list
certifi (2017.7.27.1)
chardet (3.0.4)
decorator (4.1.2)
idna (2.5)
ipython (6.1.0)
ipython-genutils (0.2.0)
jedi (0.10.2)
numpy (1.13.1)
olefile (0.44)
pexpect (4.2.1)
pickleshare (0.7.4)
Pillow (4.2.1)
pip (9.0.1)
prompt-toolkit (1.0.15)
protobuf (3.3.0)
ptyprocess (0.5.2)
Pygments (2.2.0)
PyYAML (3.12)
pyzmq (16.0.2)
requests (2.18.3)
scipy (0.19.1)
setuptools (38.5.1)
simplegeneric (0.8.1)
six (1.10.0)
torch (0.2.0.post3)
torchvision (0.2.0)
tornado (4.5.1)
tqdm (4.15.0)
traitlets (4.3.2)
urllib3 (1.22)
visdom (0.1.5)
wcwidth (0.1.7)
wheel (0.30.0)

So I double check:所以我仔细检查:

(venv) $ pip install tqdm 
Requirement already satisfied: tqdm in ./venv/lib/python3.6/site-packages
(venv) $ python  
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 13 2017, 12:02:49) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from tqdm import tqdm
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tqdm'

people have suggested forcing a reinstall , using a different interpreter source, and just reinstalling.人们建议强制重新安装,使用不同的解释器源,然后重新安装。 None of these have worked.这些都没有奏效。 this is very mysterious.这是非常神秘的。 Have any of you seen anything like this?你们有没有见过这样的事情? Saw a similar unresolved problem here在这里看到了一个类似的未解决的问题

UPDATE: Fixed.更新:已修复。 H/T to @Riverman for helping my find the issue: the problem was that an old unused alias was left over from a while ago, and while pip still had it own point back to python3.6, the python command itself was aliased to some old Anaconda3 version I had lying around somewhere. H/T to @Riverman 帮助我找到问题:问题是前一段时间遗留了一个旧的未使用的别名,虽然 pip 仍然有自己的指向 python3.6,但 python 命令本身被别名为我躺在某处的一些旧的 Anaconda3 版本。 Alias, I forgot, do not go away by just re-sourcing your .bashrc file, so I ran unalias with the offending python command and it worked!别名,我忘了,不要仅仅通过重新获取您的 .bashrc 文件而unalias ,所以我unalias有问题的 python 命令运行unalias并且它起作用了!

Would still love to hear from people though if they could explain why this can occur, though.不过,如果他们能解释为什么会发生这种情况,仍然很乐意听取人们的意见。 I though venv completely insulated you from the outside environment...is it because aliasing is a system level effect so it seeps into the venv??我虽然 venv 完全将你与外部环境隔离开来……是因为混叠是一种系统级效果,所以它会渗入 venv 吗??

While being inside the virtualenv, please issue the following commands:在 virtualenv 中,请发出以下命令:

  • pip freeze点冻结
  • pip -V点-V
  • python -V蟒蛇 -V
  • which python哪条蟒蛇
  • which pip哪个点

Share your results here to analyze it.在此处分享您的结果以进行分析。 I've also experienced pretty similar issues with the requests package before, but that happened on windows to me.我之前也遇到过与requests包非常相似的问题,但这发生在我的 Windows 上。

You used pip(which installs for Python 2.7) and you are trying to import the installed package in Python3 so it wont work.您使用了 pip(为 Python 2.7 安装)并且您正尝试在 Python3 中导入已安装的包,因此它无法工作。 You should do pip3 install package-name .你应该做pip3 install package-name pip3 installs for Python3. pip3 为 Python3 安装。 Install pip3 using apt-get install python3-pip It will work.使用apt-get install python3-pip安装 pip3 就可以了。

I had a very similar problem: I was working on a virtual environment (virtualenv) and installed pandas inside this environment with:我有一个非常相似的问题:我正在一个虚拟环境(virtualenv)上工作,并在这个环境中安装了熊猫:

pip3 install pandas

However, when I tried to import this module when working on a jupyter notebook that was also inside this virtual environment with the command:但是,当我在使用以下命令处理也在此虚拟环境中的 jupyter 笔记本时尝试导入此模块时:

import pandas as pd

I was getting the error:我收到错误:

ModuleNotFoundError: No module named 'pandas'

Finally, I noticed that, even though I was activating my jupyter notebook from inside my virtual environment, I was opening the jupyter notebook from outside my virtual environment (because I also had this module installed outside of all my virtual environments).最后,我注意到,即使我是从我的虚拟环境内部激活我的 jupyter notebook,我也是从我的虚拟环境外部打开 jupyter notebook(因为我也在我的所有虚拟环境之外安装了这个模块)。 My solution was to uninstall jupyter that was outside my virtual environments and when I ran again the juypter nb from inside the desired environment everything worked perfectly.我的解决方案是卸载虚拟环境之外的 jupyter,当我再次从所需环境中运行 juypter nb 时,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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