在 setuptools 中使用 console_scripts 时出现 ImportError
ImportError when using console_scripts in setuptools
我正在尝试在 Python 中构建一个名为 dnsrep
的程序,我正在使用 setuptools 以便我可以在不使用命令 python dnsrep
的情况下调用 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
您必须先安装 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!
我正在尝试在 Python 中构建一个名为 dnsrep
的程序,我正在使用 setuptools 以便我可以在不使用命令 python dnsrep
的情况下调用 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
您必须先安装 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!