简体   繁体   English

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

[英]`pip install -t` doesn't work with namespace packages

When I install a package with pip install -t some_dir , I can usually then add some_dir to my PYTHONPATH when running python and import the package. 当我使用pip install -t some_dir安装一个包时,我通常可以在运行python时导入包,将some_dir添加到我的PYTHONPATH中。 Like this: 像这样:

~/dev/scratch [venv] » pip install -t some_dir pytest
...
Successfully installed py pytest
~/dev/scratch [venv]  » PYTHONPATH=some_dir python
...
>>> import pytest
>>>

However, I recently added the library "stompest" to my dependencies, which is apparently a "namespace package" (not entirely sure what that means). 但是,我最近将库“stompest”添加到我的依赖项中,这显然是一个“命名空间包”(并不完全确定这意味着什么)。 It doesn't seem to work with this same pattern: 它似乎不适用于相同的模式:

~/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

It works fine if I just install stompest normally (into a virtual environment, say): 如果我只是正常安装stompest(比如虚拟环境),它工作正常:

~/dev/scratch [venv]  » pip install stompest
...
Successfully installed stompest-2.1.6
~/dev/scratch [venv]  » python
...
>>> import stompest.config
>>>

The problem seems to be that pip(?) lays down a .pth file that expects to be in the site-dir (notice f_locals['sitedir'] below): 问题似乎是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)

I tried also setting PYTHONUSERBASE=some_dir , but that didn't seem to make any difference. 我也试过设置PYTHONUSERBASE=some_dir ,但这似乎没有任何区别。 It seems like a similar issue to this one , which suggests using --egg as a workaround. 这似乎是一个类似的问题,到这一个 ,这表明使用--egg作为一种解决方法。 I can't get --egg to work with -t , though, because I get an error that I am "attempting to install a package to a directory that is not on PYTHONPATH and which Python does not read ".pth" files from." 我不能让--egg-t一起使用,因为我得到一个错误,我“试图将软件包安装到不在PYTHONPATH上的目录,哪个Python不读取”.pth“文件“。 The problem is that I can't add the directory to PYTHONPATH because it seems to be trying to install to a temporary directory: 问题是我无法将目录添加到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

Am I doing something wrong, or is this an issue with pip ? 我做错了什么,或者这是pip的问题?

It works when you do this: 它适用于您执行此操作:

import site
site.addsitedir('some_dir')

See https://docs.python.org/2/library/site.html#site.addsitedir 请参阅https://docs.python.org/2/library/site.html#site.addsitedir

I found this answer in a post by Matt Iversen (Ivoz) in this pip issue . 在这个pip问题中,我在Matt Iversen(Ivoz)的帖子中找到了这个答案。 Note that talk in this issue continues about some corner cases you might run into. 请注意,此问题中的讨论将继续讨论您可能遇到的一些极端情况。

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

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