【问题标题】:Cannot install two packages that use the same namespace无法安装使用相同命名空间的两个包
【发布时间】:2013-06-27 09:01:57
【问题描述】:

更新:单独使用 setup.py 安装两个软件包时,它们安装得很好。提取 sdist 生成的 tarball 并安装它们时,会发生相同的错误。这意味着我猜问题出在 setuptools 内部。

我开发了两个具有两个命名空间包的项目:testsuite 和 testsuite.prettyprint。 这两个命名空间包的__init__.py 都包含:

__import__('pkg_resources').declare_namespace(__name__)

这是 testsuite.prettyprint.outcomes 的 setup.py:

import pkgutil
from setuptools import setup


def get_packages():
    return [name for _, name, is_package in pkgutil.walk_packages('.') if name.startswith('testsuite') and is_package]

dependencies = ['nose2>=0.4.6', 'colorama>=0.2.5']

setup(
    name='testsuite-prettyprint-outcomes',
    version='0.1.0-alpha.1',
    packages=get_packages(),
    url='',
    license='BSD3',
    author='Omer Katz',
    author_email='omer.drow@gmail.com',
    description='testsuite-prettyprint-outcomes is a nose2 plugin that prettyprints test outcomes.',
    namespace_packages=['testsuite', 'testsuite.prettyprint'],
    install_requires=dependencies
)

这里是 testsuite.prettyprint.traceback 的 setup.py:

import pkgutil
import sys

from setuptools import setup


def get_packages():
    return [name for _, name, is_package in pkgutil.walk_packages('.') if name.startswith('testsuite') and is_package]

dependencies = ['nose2>=0.4.6', 'pygments>=1.6']

if sys.platform == 'win32':
    dependencies.append('colorama>=0.2.5')

setup(
    name='testsuite-prettyprint-traceback',
    version='0.1.0-alpha.2',
    packages=get_packages(),
    url='',
    license='BSD3',
    author='Omer Katz',
    author_email='omer.drow@gmail.com',
    description='testsuite-prettyprint-traceback is a nose2 plugin that prettyprints traceback on failures and errors.',
    namespace_packages=['testsuite', 'testsuite.prettyprint'],
    install_requires=dependencies
)

同时安装它们时,它拒绝安装一个:

pip install testsuite-prettyprint-outcomes testsuite-prettyprint-traceback --use-mirrors
Downloading/unpacking testsuite-prettyprint-outcomes
  Downloading testsuite-prettyprint-outcomes-0.1.0-alpha.1.tar.gz
  Running setup.py egg_info for package testsuite-prettyprint-outcomes

Downloading/unpacking testsuite-prettyprint-traceback
  Downloading testsuite-prettyprint-traceback-0.1.0-alpha.2.tar.gz
  Running setup.py egg_info for package testsuite-prettyprint-traceback

Downloading/unpacking nose2>=0.4.6 (from testsuite-prettyprint-outcomes)
  Running setup.py egg_info for package nose2

    warning: no previously-included files matching '__pycache__' found anywhere in distribution
    warning: no previously-included files matching '*~' found anywhere in distribution
    warning: no previously-included files matching '*.pyc' found anywhere in distribution
Downloading/unpacking colorama>=0.2.5 (from testsuite-prettyprint-outcomes)
  Downloading colorama-0.2.5.zip
  Running setup.py egg_info for package colorama

Downloading/unpacking pygments>=1.6 (from testsuite-prettyprint-traceback)
  Downloading Pygments-1.6.tar.gz (1.4MB): 1.4MB downloaded
  Running setup.py egg_info for package pygments

Downloading/unpacking six>=1.1,<1.2 (from nose2>=0.4.6->testsuite-prettyprint-outcomes)
  Running setup.py egg_info for package six

Installing collected packages: testsuite-prettyprint-outcomes, testsuite-prettyprint-traceback, nose2, colorama, pygments, six
  Running setup.py install for testsuite-prettyprint-outcomes
    Skipping installation of /home/omer/.virtualenvs/test/lib/python3.2/site-packages/testsuite/__init__.py (namespace package)
    Skipping installation of /home/omer/.virtualenvs/test/lib/python3.2/site-packages/testsuite/prettyprint/__init__.py (namespace package)

    Installing /home/omer/.virtualenvs/test/lib/python3.2/site-packages/testsuite_prettyprint_outcomes-0.1.0_alpha.1-py3.2-nspkg.pth
  Running setup.py install for testsuite-prettyprint-traceback
    error: package directory 'testsuite/prettyprint/outcomes' does not exist
    Complete output from command /home/omer/.virtualenvs/test/bin/python3.2 -c "import setuptools;__file__='/home/omer/.virtualenvs/test/build/testsuite-prettyprint-traceback/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-12l9lq-record/install-record.txt --single-version-externally-managed --install-headers /home/omer/.virtualenvs/test/include/site/python3.2:
    running install

running build

running build_py

creating build

creating build/lib

creating build/lib/testsuite

copying testsuite/__init__.py -> build/lib/testsuite

creating build/lib/testsuite/prettyprint

copying testsuite/prettyprint/__init__.py -> build/lib/testsuite/prettyprint

error: package directory 'testsuite/prettyprint/outcomes' does not exist

----------------------------------------
Command /home/omer/.virtualenvs/test/bin/python3.2 -c "import setuptools;__file__='/home/omer/.virtualenvs/test/build/testsuite-prettyprint-traceback/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-12l9lq-record/install-record.txt --single-version-externally-managed --install-headers /home/omer/.virtualenvs/test/include/site/python3.2 failed with error code 1 in /home/omer/.virtualenvs/test/build/testsuite-prettyprint-traceback
Storing complete log in /home/omer/.pip/pip.log

我不知道出了什么问题。即使您更改安装顺序,它也会说找不到另一个。

【问题讨论】:

  • @abarnert 单独安装其中任何一个都可以。两者都安装
  • 你在setup.py 文件的namespace_packages 条目中输入了什么?
  • @MartijnPieters 都包含 ['testsuite', 'testsuite.prettyprint']。我检查了,当我 sdist 时 namespace_packages.txt 包含两者。
  • 您是否有可能将您的要求提升到 Python 3.3 并且只依赖 PEP 420 而不是构建显式命名空间包?我猜不是,但是... 修改内容以删除__init__.py 文件和setup.py 中的namespace_packages 等等,它适用于3.3。 (不过当然是3.2,安装后就不能用了,不太友好……)
  • @abarnert 不是真的没有。

标签: python pip namespaces setuptools namespace-package


【解决方案1】:

在安装您的一个包并下载另一个包之后……

您没有在源文件中包含 testsuite/__init__.pytestsuite/prettyprint/__init__.py

Namespace Packagessetuptools/pkg_resources 上的文档说明:

请注意,顺便说一下,您的项目的源代码树必须在正常的 Python 包布局中包含命名空间包的 __init__.py 文件(以及任何父包的 __init__.py)。

如果您不实际安装这些文件,它们就没有任何用处。你最终会得到一个testsuite,其中除了prettyprint 之外什么都没有,而其中除了outcomes 之外什么都没有,所以testsuitetestsuite.prettyprint 根本不是包,更不用说命名空间包了。

【讨论】:

  • 如何同时包含它们?
  • 等一下,让我弄清楚为什么它们没有被包括在内,我会编辑答案。
  • 我刚刚检查并 get_packaged 正确返回所有包。
  • setup.py 文件看起来适合这种类型的设置……但我只在安装鸡蛋时使用过这种类型(easy_install-3.3 .pip-3.3 install --egg .)。当我这样做时,我得到__init__.py文件;只是非鸡蛋安装跳过了它们——带有明确的Skipping installation of …/testsuite/__init__.py (namespace package) 消息。
  • 啊哈。查看 cmets here 及以下。看起来pip 故意不为非鸡蛋安装的命名空间包安装__init__.py,而是使用不同的机制。我不知道 answer 是什么,但至少它告诉我们去哪里寻找。 (不幸的是,我现在得走了,但如果没有其他人先帮忙,我会看看明天我能找到什么。)
【解决方案2】:

您的包裹的names 看起来不对。我只是将一个项目分成了几个子包,我做的一件不同的事情是让每个 name 匹配 namespace_packages 的组件。

所以,对于testsuite.prettyprint.outcomes

setup(
       name='testsuite.prettyprint.outcomes',
       [...] ,
       namespace_packages=['testsuite', 'testsuite.prettyprint']
)

对于testsuite.prettyprint.traceback

setup(
       name='testsuite.prettyprint.traceback',
       [...] ,
       namespace_packages=['testsuite', 'testsuite.prettyprint']
)

要使其正常工作,您需要为所有父级namespace_package 级别(即下至testsuite.prettyprint)提供__init__.py 脚本,就像您已经展示的那样。

生产包中namespace_packages 的良好示例可以在zope 子包中找到。

例如请参阅zope.app.cache setup.py 脚本,http://svn.zope.org/zope.app.cache/trunk/setup.py?view=markup

【讨论】:

  • 但是它们将不能作为 testsuite-prettyprint-traceback 安装。我指定包名称是有原因的。这是我们试图在 setuptools 中跟踪的错误吗?
【解决方案3】:

我看到你正在使用 virtualenv。通常,当我遇到您所面临的错误时,是因为这些软件包不能很好地与 virtualenv 一起使用。

您是否尝试在您的主要 python 安装文件夹中安装这些软件包? (不在 virtualenv 中)

我认为发生这种情况是因为某些 setup.py 文件对主机环境做出了假设,并且没有遵循 setup.py 最佳实践。

如果您仍然卡住,请尝试一下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2014-01-12
    • 2010-10-18
    • 2015-05-21
    • 1970-01-01
    相关资源
    最近更新 更多