【问题标题】:Pip install in Virtualenv working, but *not* working with `-r requirements.txt`在 Virtualenv 中的 Pip 安装工作,但 * 不 * 使用 `-r requirements.txt`
【发布时间】:2019-07-07 23:42:30
【问题描述】:

我遇到了在herehere 等答案中讨论的有据可查的Virtualenv-Pip 错误的独特版本。在这些情况下,问题是 pip 安装在全局文件夹中,在某些情况下是因为其中一个 pip 脚本中的 shebang 错误,在其他情况下是因为 bin/activate 脚本中的第 42 行错误,但它始终是全局安装的。

我的问题不同:在我的虚拟环境中pip install <package> 可以正常工作(在本地安装),但pip install -r requirements.txt 安装到虚空例如:

pip install scipy

然后查看/home/user/software/project/venv/lib/python3.6/site-packages的内容我得到了结果:

easy_install.py  
numpy  
numpy-1.16.1.dist-info  
pip  
pip-19.0.2.dist-info  
pkg_resources  
__pycache__  
scipy  
scipy-1.2.1.dist-info  
setuptools  
setuptools-40.8.0.dist-info  
wheel  
wheel-0.33.0.dist-info

但是,在同一个虚拟环境中,我运行 pip install -r requirements.txt,其中 requirements.txt 要求安装 numpy、Cython、sklearn、matplotlib 和 argparse,并输出:

Collecting Cython (from -r requirements.txt (line 1))                                                                                                                                                              
  Downloading https://files.pythonhosted.org/packages/16/98/49aa24054e99e9c7734e49d6996662f547e4e2faae0051d35fbbc461afa4/Cython-0.29.5-cp36-cp36m-manylinux1_x86_64.whl (2.1MB)                                    
    100% |████████████████████████████████| 2.1MB 23.6MB/s                                                                                                                                                         
Collecting numpy (from -r requirements.txt (line 2))                                                                                                                                                               
  Using cached https://files.pythonhosted.org/packages/f5/bf/4981bcbee43934f0adb8f764a1e70ab0ee5a448f6505bd04a87a2fda2a8b/numpy-1.16.1-cp36-cp36m-manylinux1_x86_64.whl                                            
Collecting sklearn (from -r requirements.txt (line 3))                                                                                                                                                             
  Downloading https://files.pythonhosted.org/packages/1e/7a/dbb3be0ce9bd5c8b7e3d87328e79063f8b263b2b1bfa4774cb1147bfcd3f/sklearn-0.0.tar.gz
Collecting matplotlib (from -r requirements.txt (line 4))
  Downloading https://files.pythonhosted.org/packages/71/07/16d781df15be30df4acfd536c479268f1208b2dfbc91e9ca5d92c9caf673/matplotlib-3.0.2-cp36-cp36m-manylinux1_x86_64.whl (12.9MB)
    100% |████████████████████████████████| 12.9MB 6.3MB/s
Collecting argparse (from -r requirements.txt (line 5))
  Downloading https://files.pythonhosted.org/packages/f2/94/3af39d34be01a24a6e65433d19e107099374224905f1e0cc6bbe1fd22a2f/argparse-1.4.0-py2.py3-none-any.whl

这不会导致本地站点包中没有新的添加。更重要的是,当我尝试查看它们的安装位置时,/usr/lib/python2.7/usr/local/lib/python2.7/usr/lib/python3/usr/lib/python3.6usr/lib/python3.7~/.local/lib/python3.6/~/.local/lib/python2.7/ 中没有site-packages 目录。

为了验证这一点,当我运行时: find /usr/ -name "Cython" 我得到一个空的结果。

有什么建议吗?我可以编写一个 bash 脚本,直接调用 pip install,但这似乎很草率,我觉得我应该在它变得更糟之前解决这个问题。

注意事项:

我的机器在 18.04.1 上,它的 python3.6.7 为 python3 和 python 2.7.15 为 python2 作为机器默认安装,我为 sudo apt install python-pipsudo apt install python3-pip 安装了 pip,这个虚拟环境是用virtualenv -p python3 venv创建的

我的requirements.txt

Cython
numpy
sklearn
matplotlib
torch>=0.4.1
torchvision>=0.2.1
argparse
quadprog

Full output from pip install -r requirements.txt

【问题讨论】:

  • 您可以将您的requirements.txt 添加到问题中吗?
  • 另外,除了收集之外,完整的pip 输出?
  • 添加手动pip安装其余部分时会发生什么? Cythonsklearnmatplotlibargparse?
  • 添加我的requirements.txt 和完整的pip 输出。当我手动安装其余部分时,它们正常工作并且位于正确的站点包区域中。
  • 另外,澄清一下,18.04.1,你的意思是 Ubuntu?

标签: python pip installation virtualenv requirements.txt


【解决方案1】:

所以这是部分答案,但即使我不完全理解原因,我至少也找到了问题所在。当我改变时:

virtualenv -p python3 venv

virtualenv -p /usr/lib/python3.6 venv.

pip install -r requirements.txt 工作,产生正确的站点包。

同样,我不知道为什么会这样,但确实如此。我只是用不同的python版本再次尝试了它,但运气不太好,所以我不完全确定这是否是一个通用修复。当我有更好的解释时,我会编辑这个答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 2019-07-04
    • 2020-03-18
    • 2014-02-08
    • 2019-08-27
    • 1970-01-01
    • 2015-01-05
    相关资源
    最近更新 更多