Skip to main content
 首页 » 编程设计

python之无法安装两个使用相同命名空间的包

2025年05月04日8bluestorm

更新:当单独使用 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 

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

请您参考如下方法:

安装其中一个包并下载另一个包后......

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

Namespace Packages 上的文档setuptools/pkg_resources 方式说明:

Note, by the way, that your project's source tree must include the namespace packages' __init__.py files (and the __init__.py of any parent packages), in a normal Python package layout.

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