[英]`pip install -t` doesn't work with namespace packages
当我使用pip install -t some_dir
安装一个包时,我通常可以在运行python时导入包,将some_dir
添加到我的PYTHONPATH中。 像这样:
~/dev/scratch [venv] » pip install -t some_dir pytest
...
Successfully installed py pytest
~/dev/scratch [venv] » PYTHONPATH=some_dir python
...
>>> import pytest
>>>
但是,我最近将库“stompest”添加到我的依赖项中,这显然是一个“命名空间包”(并不完全确定这意味着什么)。 它似乎不适用于相同的模式:
~/dev/scratch [venv] » pip install -t some_dir stompest
...
Successfully installed stompest
~/dev/scratch [venv] » PYTHONPATH=some_dir python
...
>>> import stompest.config
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named stompest.config
如果我只是正常安装stompest(比如虚拟环境),它工作正常:
~/dev/scratch [venv] » pip install stompest
...
Successfully installed stompest-2.1.6
~/dev/scratch [venv] » python
...
>>> import stompest.config
>>>
问题似乎是pip(?)列出了一个预期在site-dir中的.pth
文件(请注意f_locals['sitedir']
):
~/dev/scratch [venv] » cat some_dir/stompest-2.1.6-py2.7-nspkg.pth
import sys,types,os; p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('stompest',)); ie = os.path.exists(os.path.join(p,'__init__.py')); m = not ie and sys.modules.setdefault('stompest',types.ModuleType('stompest')); mp = (m or []) and m.__dict__.setdefault('__path__',[]); (p not in mp) and mp.append(p)
我也试过设置PYTHONUSERBASE=some_dir
,但这似乎没有任何区别。 这似乎是一个类似的问题,到这一个 ,这表明使用--egg
作为一种解决方法。 我不能让--egg
与-t
一起使用,因为我得到一个错误,我“试图将软件包安装到不在PYTHONPATH上的目录,哪个Python不读取”.pth“文件“。 问题是我无法将目录添加到PYTHONPATH,因为它似乎试图安装到临时目录:
~/dev/scratch [venv] » PYTHONPATH=some_dir pip install -t some_dir --egg stompest
Collecting stompest
Using cached stompest-2.1.6.tar.gz
Installing collected packages: stompest
Running setup.py install for stompest
Complete output from command /home/nalderso/dev/scratch/venv/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-Wc3oaO/stompest/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-fZlXM2-record/install-record.txt --compile --install-headers /home/nalderso/dev/scratch/venv/include/site/python2.7/stompest --home=/tmp/tmpHNVOP0:
running install
Checking .pth file support in /tmp/tmpHNVOP0/lib/python/
/home/nalderso/dev/scratch/venv/bin/python -E -c pass
TEST FAILED: /tmp/tmpHNVOP0/lib/python/ does NOT support .pth files
error: bad install directory or PYTHONPATH
You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from. The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/tmp/tmpHNVOP0/lib/python/
and your PYTHONPATH environment variable currently contains:
'some_dir'
Here are some of your options for correcting the problem:
* You can choose a different installation directory, i.e., one that is
on PYTHONPATH or supports .pth files
* You can add the installation directory to the PYTHONPATH environment
variable. (It must then also be on PYTHONPATH whenever you run
Python and want to use the package(s) you are installing.)
* You can set up the installation directory to support ".pth" files by
using one of the approaches described here:
https://pythonhosted.org/setuptools/easy_install.html#custom-installation-locations
Please make the appropriate changes for your system and try again.
----------------------------------------
Command "/home/nalderso/dev/scratch/venv/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip-build-Wc3oaO/stompest/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-fZlXM2-record/install-record.txt --compile --install-headers /home/nalderso/dev/scratch/venv/include/site/python2.7/stompest --home=/tmp/tmpHNVOP0" failed with error code 1 in /tmp/pip-build-Wc3oaO/stompest
我做错了什么,或者这是pip
的问题?
它适用于您执行此操作:
import site
site.addsitedir('some_dir')
请参阅https://docs.python.org/2/library/site.html#site.addsitedir
在这个pip问题中,我在Matt Iversen(Ivoz)的帖子中找到了这个答案。 请注意,此问题中的讨论将继续讨论您可能遇到的一些极端情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.