ImportError: No module... After python setup.py install

ImportError: No module... After python setup.py install

我在安装我的 python 脚本之一时遇到问题。它具有以下结构:

myproject
  setup.py
  src
    myproject
      otherfolders
      main.py
      __init__.py

我的 setup.py 创建了一个入口点,如下所示:

from setuptools import setup, find_packages

setup(name='mypackage',
version='2.4.0',
author='me',
author_email='...',
package_dir={'':'src'},
packages=find_packages('myproject'),
install_requires=[
    "networkx",
    "geopy",
    "pyyaml"
],
zip_safe=False,
entry_points={
    'console_scripts': [
        'myproject=myproject.main:main',
    ],
},
)

现在,在使用 sudo python setup.py install 成功安装后,我 运行 mypackage 收到导入错误:No module named mypackage.main.

我知道有很多类似的问题,我尝试了 most/all 建议的解决方案 here,例如检查 __init__.py 和设置 PYTHONPATH,但是问题仍然存在。 我 运行 在两台不同的 Ubuntu 16.04 机器上进行此操作。

我很确定这以前有效,但即使我返回到较早的提交,它现在也无效。

我注意到安装在 develop 下有效,但在 install 下仍然失败。这对任何人都有意义吗?

packages=find_packages('mypackage') -> packages=find_packages('myproject').

你也应该使用 myproject.main.

问题出在find_packages():

Some projects use a src or lib directory as the root of their source tree, and those projects would of course use "src" or "lib" as the first argument to find_packages().

因此,我不得不将 find_packages('myproject') 更改为 find_packages('src')