gpt4 book ai didi

python - `pip install -t` 不适用于命名空间包

转载 作者:太空宇宙 更新时间:2023-11-03 11:26:44 27 4
gpt4 key购买 nike

当我使用 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(?) 放置了一个 .pth 文件,该文件预计位于站 pip 目录中(注意 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,但这似乎没有任何区别。这似乎与 this one 类似,建议使用 --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 issue 的 Matt Iversen (Ivoz) 的帖子中找到了这个答案.请注意,本期继续讨论您可能会遇到的一些极端情况。

关于python - `pip install -t` 不适用于命名空间包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237954/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com