创建了一个PyPI包并安装,但运行时会返回导入错误

时间:2014-04-19 00:47:11

标签: python pypi

我创建了一个简单的包,并且能够在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,但它返回以下错误: Traceback(最近一次调用最后一次):   文件“C:\ Python34 \ lib \ runpy.py”,第171行,在_run_module_as_main中     “主要”,mod_spec)   文件“C:\ Python34 \ lib \ runpy.py”,第86行,在_run_code中     exec(代码,run_globals)   文件“C:\ Python34 \ Scripts \ daysgrounded.exe__main __。py”,第5行,in   文件“C:\ Python34 \ lib \ site-packages \ daysgrounded__main __。py”,第12行,in     进口cli ImportError:没有名为'cli'的模块

您可以在https://github.com/jcrmatos/DaysGroundedhttps://pypi.python.org/pypi?name=daysgrounded&version=0.0.8&:action=display找到该应用 {{3}}

我做错了什么?是在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__ = 'jcrmatos@gmail.com'

__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

1 个答案:

答案 0 :(得分:3)

这是由于three ways of invoking a python package

之间的差异
  1. python daysgrounded
  2. python daysgrounded/__main__.py
  3. python -m daysgrounded
  4. 如果你在项目中尝试了这些方法,你会注意到第三种方法不起作用,这正是runpy使用的方法。它不起作用的原因是因为sys.path不正确,因为python在前两种方式中为daysgrounded/添加了sys.path,而不是第三种方式。

    为了使所有三种方法都能正常工作,你必须确保你的sys.path是正确的,并且有两种方法可以做到这一点。

    如果您希望能够import clisys.path应为daysgrounded/,这意味着您需要修改__init__.py以将其添加到sys.path

    import sys
    import os.path
    sys.path.insert(1, os.path.dirname(__file__))
    

    如果您希望能够from daysgrounded import clisys.path应该是daysgrounded/上方的目录,这意味着您需要修改__main__.py以将其添加到sys.path 1}}:

    import sys
    import os.path
    sys.path.insert(1, os.path.dirname(sys.path[0]))