Python安装包不会导入模块

时间:2015-05-13 02:58:45

标签: python module packages setuptools

我想创建一个名为knife的python应用程序,可以从CLI执行,问题是它无法导入模块。

我的文件结构是这样的:

my_project/
    knife/
        __init__.py
        knife.py
        external.py
    setup.py

我的setup.py看起来像这样:

#!/usr/bin/python

from setuptools import setup, find_packages

setup(name='Knife',
      version='0.3',
      description='Very cool project',
      author='John Doe',
      author_email='author@email.com',
      packages=find_packages(),
      py_modules=['knife.external'],
      scripts=['knife/knife.py'],
     )

我的knife.py看起来像这样:

#!/usr/bin/python

import external

def main():
    print("Execute main function")

if __name__ == "__main__":
    main()

因此在使用setup.py install安装模块后,我尝试运行刀,但继续抛出此错误:

$ knife.py 
Traceback (most recent call last):
  File "/usr/bin/knife.py", line 4, in <module>
    __import__('pkg_resources').run_script('Knife==0.3', 'knife.py')
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 723, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/usr/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1643, in run_script
    exec(script_code, namespace, namespace)
  File "/usr/lib/python2.7/site-packages/Knife-0.3-py2.7.egg/EGG-INFO/scripts/knife.py", line 3, in <module>
    __requires__ = 'Knife==0.3'
ImportError: No module named external

真正发生了什么?我该如何解决呢?

1 个答案:

答案 0 :(得分:1)

external.py也应该发货。所以setup.py添加了external.py:

  scripts=['knife/knife.py', 'knife/external.py'],