我在 Python 3.6.0rc1 中发现了隐式命名空间包的奇怪行为。你能告诉我我是错的还是 Python 3.6 的错误?
我正在使用名称空间包marrow
,它有两个单独的包marrow.util
和marrow.mailer
. 第二个取决于第一个。
假设我们已经marrow.util
安装site-packages
了 Python 2.7、3.5 和 3.6:
$ ls -la /usr/lib/python*/site-packages/marrow
/usr/lib/python2.7/site-packages/marrow:
total 24
drwxr-xr-x. 3 root root 4096 Dec 23 12:23 .
drwxr-xr-x. 196 root root 16384 Dec 23 12:23 ..
drwxr-xr-x. 3 root root 4096 Dec 23 12:23 util
/usr/lib/python3.5/site-packages/marrow:
total 12
drwxr-xr-x. 3 root root 4096 Dec 23 12:24 .
drwxr-xr-x. 99 root root 4096 Dec 23 12:24 ..
drwxr-xr-x. 4 root root 4096 Dec 23 12:24 util
/usr/lib/python3.6/site-packages/marrow:
total 12
drwxr-xr-x. 3 root root 4096 Dec 23 14:25 .
drwxr-xr-x. 37 root root 4096 Dec 23 14:25 ..
drwxr-xr-x. 4 root root 4096 Dec 23 14:25 util
这里没有__init__.py
文件,这是正确的,因为marrow
是命名空间包。您可以在安装过程中看到此日志消息:
Skipping installation of <deleted>/site-packages/marrow/__init__.py (namespace package)
然后您在其他目录中构建(但未安装)marrow
命名空间包的第二部分。marrow.mailer
例如像这样:
$ pwd
/builddir/build/BUILD/marrow.mailer-4.0.2
$ ls
coverage.xml debuglinks.list elfbins.list LICENSE.txt marrow.mailer.egg-info README.textile setup.py debugfiles.list debugsources.list example marrow PKG-INFO setup.cfg test
$ ls marrow/
__init__.py __init__.pyc mailer __pycache__
当我在此文件夹中运行 Python 2.7.12 或 3.5.2 并尝试导入marrow.util
(从站点包)时,它按预期工作。
$ pwd
/builddir/build/BUILD/marrow.mailer-4.0.2
$ python2
Python 2.7.12 (default, Sep 29 2016, 12:52:02)
[GCC 6.2.1 20160916 (Red Hat 6.2.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import marrow.util
>>>
$ python3.5
Python 3.5.2 (default, Sep 14 2016, 11:28:32)
[GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import marrow.util
>>>
但是当我尝试使用 Python 3.6 导入相同的模块时,它失败了:
$ python3.6
Python 3.6.0rc1 (default, Dec 10 2016, 14:50:33)
[GCC 6.2.1 20160916 (Red Hat 6.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import marrow.util
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'marrow.util'
>>>
当我尝试marrow.mailer
在 Mock 中构建为 RPM 包时,我发现了这个问题。使用 Python 2.7 和 3.5 一切正常,但 Python 3.6 无法marrow.util
从站点包导入,因此marrow.mailer
在构建 RPM 期间测试失败。
来自失败测试的示例回溯:
Traceback:
test/test_addresses.py:8: in <module>
from marrow.mailer.address import Address, AddressList, AutoConverter
marrow/mailer/__init__.py:12: in <module>
from marrow.mailer.message import Message
marrow/mailer/message.py:21: in <module>
from marrow.mailer.address import Address, AddressList, AutoConverter
marrow/mailer/address.py:12: in <module>
from marrow.util.compat import basestring, unicode, unicodestr, native
E ModuleNotFoundError: No module named 'marrow.util'
我在 Python 3.6 的变更日志中找不到与此问题相关的任何内容。
谢谢你的帮助。
编辑:我已经检查sys.path
了 Python 3.6,一切看起来都很好:
$ python3.6
Python 3.6.0rc1 (default, Dec 10 2016, 14:50:33)
[GCC 6.2.1 20160916 (Red Hat 6.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']
>>>
编辑2:
因为我仍然找不到任何解决方案并且我没有任何响应,所以我创建了一个简单的 Bash 脚本来重现我的情况。您唯一需要的是 Python 3.5 和 Python 3.6。
#!/bin/bash
# Change this to run script with different Python
#PYTHON=python3.5 # system Python 3.5
PYTHON=~/temp/Python-3.6.0/python # compiled Python 3.6
# Create venv and activate
$PYTHON -m venv venv
source ./venv/bin/activate
# Install marrow.util package as a part of namespace package marrow
pip install marrow.util
# Create simple folder structure
mkdir -p marrow/mailer
# Create structure of __init__.py files
# For namespace package with related content
cat >> marrow/__init__.py << EOL
try: # pragma: no cover
__import__('pkg_resources').declare_namespace(__name__)
except ImportError: # pragma: no cover
__import__('pkgutil').extend_path(__path__, __name__)
EOL
# For mailer module just with print()
cat >> marrow/mailer/__init__.py << EOL
print('Imported!!!')
EOL
# Testing
# Importing marrow.util installed via pip in venv
$PYTHON -c "import marrow.util"
# Importing marrow.mailer created manually in PWD
$PYTHON -c "import marrow.mailer"
# deactivate venv
deactivate
如果您使用 Python 3.5 执行此脚本,您将看到 Python 3.5 可以marrow.util
通过 pip 导入安装,但无法导入marrow.mailer
本地文件夹。但是 Python 3.6 可以导入本地模块marrow.mailer
,但不能导入模块marrow.util
。