为什么导入在setuptools entry_point脚本中失败,而在python解释器中却没有?

时间:2016-01-15 02:14:03

标签: python python-2.7 import setuptools

我有以下项目结构:

project
|-project.py
|-__init__.py
|-setup.py
|-lib
  |-__init__.py
  |-project
    |-__init__.py
    |-tools.py

project.py

from project.lib import *

def main():
    print("main")
    tool()

if __name__ == "__main__":
    main()

setup.py

from setuptools import setup

setup(
    name = "project",
    version="1.0",
    packages = ["project", "project.lib"],
    package_dir = {"project": ".", "project.lib": 'lib/project'},
    entry_points={
        'console_scripts': [
            'project = project.project:main',
        ],
    },
)

tools.py

def tool():
    print("tool")

如果我跑

import project.lib.tools
project.lib.tools.tool()

它按预期工作,但运行命令project失败并带有

Traceback (most recent call last):
  File "/usr/local/bin/project", line 9, in <module>
    load_entry_point('project==1.0', 'console_scripts', 'project')()
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 568, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2720, in load_entry_point
    return ep.load()
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2380, in load
    return self.resolve()
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2386, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "build/bdist.linux-x86_64/egg/project/project.py", line 3, in <module>
ImportError: No module named lib

我不明白为什么这两个解释器没有相同的默认导入路径。

此设置的原因是我希望能够import project.lib.tools,但保留目录结构lib/project

完整的distutils文档严重无法说明如何在分发包之后导入包(setuptoolsdistutils的差异并不是那么神秘 - 无法知道distutils的行为是否在此扩展。

我在Ubuntu 15.10上使用setuptools 18.4-1和python 2.7。

如果我按照@AnttiHaapala的回答中的建议更改了项目结构和setup.py

$ project
Traceback (most recent call last):
  File "/usr/local/bin/project", line 9, in <module>
    load_entry_point('project==1.0', 'console_scripts', 'project')()
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 568, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2720, in load_entry_point
    return ep.load()
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2380, in load
    return self.resolve()
  File "/usr/local/lib/python2.7/dist-packages/pkg_resources/__init__.py", line 2386, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "build/bdist.linux-x86_64/egg/project/project.py", line 3, in <module>
ImportError: No module named lib

1 个答案:

答案 0 :(得分:9)

您的项目结构似乎是b0rken。分发的标准布局是setup.py位于顶层。然后,您的项目将包含1个(顶级)包,即project,包含子包project.lib。因此,我们得到以下目录布局:

Project-0.42/
 +- project/
 |    +- __init__.py
 |    +- lib/
 |    |   +- __init__.py
 |    |   +- tools.py
 |    +- project.py
 +- setup.py

然后在您的setup.py中,您可以完成

from setuptools import find_packages

setup(
    ...
    # remove package_dir, it is unnecessary
    packages=find_packages(),
    ...
)

package_dir实际上并不能同时处理顶级+子包。在pip remove project之后,您可以确定您没有在网站包中安装任何错误版本的版本,然后运行python setup.py develop将源链接到site-packages

之后,问题在于您使用Python 2来破坏导入系统,该系统假定相对导入。在project.py中,您的import project.lib默认情况下会假定相对导入,并尝试实际导入project.project.lib。由于这不是你想要的,你应该添加

from __future__ import absolute_import

位于该文件的顶部。我认真地建议你添加这个(如果你在任何地方使用division运算符,为什么不是/导入),以避免这些陷阱并保持Python 3兼容。