我创建了一个简单的程序包,并能够在PyPI中发布它。
它的文件结构是:
DaysGrounded\
DaysGrounded\setup.py
DaysGrounded\setup.cfg
DaysGrounded\requirements.txt
DaysGrounded\MANIFEST.in
DaysGrounded\daysgrounded\
DaysGrounded\daysgrounded\__init__.py
DaysGrounded\daysgrounded\__main__.py
DaysGrounded\daysgrounded\cli.py
DaysGrounded\daysgrounded\gui.py
DaysGrounded\daysgrounded\shared.py
我将其安装在另一台计算机上并运行c:\ Python34 \ Scripts \ daysgrounded.exe,但它返回以下错误:
追溯(最近一次通话):
_run_module_as_main中的文件“ C:\ Python34 \ lib \ runpy.py”,第171行
“主要”,mod_spec)
_run_code中的文件“ C:\ Python34 \ lib \ runpy.py”,第86行
exec(代码,run_globals)
文件“ C:\ Python34 \ Scripts \ daysgrounded.exe__main __。py”,第5行,在
文件“ C:\ Python34 \ lib \ site-packages \ daysgrounded__main __。py”,第12行
进口cli
ImportError:没有名为“ cli”的模块
您可以在https://github.com/jcrmatos/DaysGrounded中找到该应用,然后
https://pypi.python.org/pypi?name=daysgrounded&version=0.0.8&:action=display
我究竟做错了什么?在setup.py中吗?
#!/usr/bin/env python
# -*- coding: latin-1 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from setuptools import setup, find_packages
#import py2exe
from daysgrounded import *
setup(
name=__title__,
version=__version__,
description=__desc__,
long_description=open('README.txt').read(),
#long_description=(read('README.txt') + '\n\n' +
# read('CHANGES.txt') + '\n\n' +
# read('AUTHORS.txt')),
license=__license__,
url=__url__,
author=__author__,
author_email=__email__,
keywords=__keywords__,
classifiers=__classifiers__,
packages=find_packages(exclude=['tests*']),
#packages=__packages__,
entry_points=__entrypoints__,
install_requires=open('requirements.txt').read(),
#install_requires=open('requirements.txt').read().splitlines(),
include_package_data=True,
package_data=__pkgdata__,
#console=['daysgrounded\\__main__.py']
)
Or is it in __init__.py?
"""Manage child(s) grounded days."""
__all__ = [
'__title__', '__version__',
'__desc__', '__license__', '__url__',
'__author__', '__email__',
'__copyright__',
'__keywords__', '__classifiers__',
#'__packages__',
'__entrypoints__', '__pkgdata__'
]
__title__ = 'daysgrounded'
__version__ = '0.0.8'
__desc__ = __doc__.strip()
__license__ = 'GNU General Public License v2 or later (GPLv2+)'
__url__ = 'https://github.com/jcrmatos/DaysGrounded'
__author__ = 'Joao Matos'
__email__ = '[email protected]'
__copyright__ = 'Copyright 2014 Joao Matos'
__keywords__ = 'days grounded'
__classifiers__ = [
'Development Status :: 4 - Beta',
'Environment :: Console',
'Environment :: Win32 (MS Windows)',
'Intended Audience :: End Users/Desktop',
'Intended Audience :: Developers',
'Natural Language :: English',
'Natural Language :: Portuguese',
'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Topic :: Other/Nonlisted Topic',
# Use below to prevent any unwanted publishing
#'Private :: Do Not Upload'
]
#__packages__ = ['daysgrounded']
__entrypoints__ = {
'console_scripts': ['daysgrounded = daysgrounded.__main__:main'],
#'gui_scripts': ['app_gui = daysgrounded.daysgrounded:start']
}
__pkgdata__ = {'daysgrounded': ['*.txt']}
#__pkgdata__= {'': ['*.txt'], 'daysgrounded': ['*.txt']}
谢谢,
JM
最佳答案
这是由于three ways of invoking a python package之间的差异:python daysgrounded
python daysgrounded/__main__.py
python -m daysgrounded
如果您在项目中尝试每种方法,您会注意到第三种方法不起作用,而这正是runpy
使用的方法。它不起作用的原因是因为您的sys.path
不正确,因为python在前两种方式中将daysgrounded/
添加到了sys.path
中,而第三种则没有。
为了使这三种方式都能正常工作,您必须确保sys.path是正确的,并且有两种方式可以做到这一点。
如果希望能够执行import cli
,则sys.path
应该为daysgrounded/
,这意味着您需要修改__init__.py
以将其添加到sys.path
:
import sys
import os.path
sys.path.insert(1, os.path.dirname(__file__))
如果要执行
from daysgrounded import cli
,则sys.path
应该是daysgrounded/
上方的目录,这意味着您需要修改__main__.py
才能将其添加到sys.path
:import sys
import os.path
sys.path.insert(1, os.path.dirname(sys.path[0]))
关于python - 创建了一个PyPI软件包并安装,但运行时返回导入错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23164482/