在setuptools中使用console_scripts时的ImportError

时间:2015-02-12 05:10:43

标签: python setuptools

我正在尝试在Python中构建一个名为dnsrep的程序,我正在使用setuptools,因此我可以在不使用命令dnsrep的情况下调用python dnsrep模块。我写的setup.py脚本如下:

from setuptools import setup, find_packages

setup(
    name='dnsrep',
    version='0.1',
    description='Program that gives a reputation score to url\'s\n.',
    entry_points = {
        'console_scripts': ['dnsrep = dnsrep:main']
    },
    zip_safe=True,
)

我使用以下命令安装模块:

python setup.py install

我的模块已注册,但是当我运行它时,我收到错误:

Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/bin/dnsrep", line 9, in <module>
    load_entry_point('dnsrep==0.1', 'console_scripts', 'dnsrep')()
  File "build/bdist.macosx-10.6-intel/egg/pkg_resources/__init__.py", line 521, in load_entry_point
  File "build/bdist.macosx-10.6-intel/egg/pkg_resources/__init__.py", line 2632, in load_entry_point
  File "build/bdist.macosx-10.6-intel/egg/pkg_resources/__init__.py", line 2312, in load
  File "build/bdist.macosx-10.6-intel/egg/pkg_resources/__init__.py", line 2318, in resolve
ImportError: No module named dnsrep

1 个答案:

答案 0 :(得分:4)

您必须安装python脚本,然后才能通过定义的entry point

调用它

这是我的虚拟项目:

dnsrep/
├── dnsrep.py
└── setup.py

这就是setup.py的样子:

from setuptools import setup
setup(
    name='dnsrep',
    version='0.1',
    description='Program that gives a reputation score to url\'s\n.',
    py_modules=['dnsrep'],
    entry_points = {
        'console_scripts': ['dnsrep = dnsrep:main']
    },
    zip_safe=True,
)

请注意参数py_modules=['dnsrep'],它将dnsrep.py安装为新模块。

最后,这是我dnsrep.py的虚拟实现:

from __future__ import print_function

def main():
    print("Hey, it works!")

安装完成后,一切都按预期工作,$ dnsrep打印:Hey, it works!