gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:24:59 24 4
gpt4 key购买 nike

我创建了一个简单的包并能够在 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,但它返回以下错误:回溯(最近一次调用最后一次): 文件“C:\Python34\lib\runpy.py”,第 171 行,在 _run_module_as_main 中 “主要”,mod_spec) 文件“C:\Python34\lib\runpy.py”,第 86 行,在 _run_code 中 执行(代码,run_globals) 文件“C:\Python34\Scripts\daysgrounded.exe__main__.py”,第 5 行,位于 文件“C:\Python34\lib\site-packages\daysgrounded__main__.py”,第 12 行,位于 导入命令行导入错误:没有名为“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__ = '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

最佳答案

这是由于 three ways of invoking a python package 之间的差异造成的:

  1. python daysgrounded
  2. python daysgrounded/__main__.py
  3. python -m daysgrounded

如果您在项目中尝试上述每一种方法,您会发现第三种方法不起作用,而这正是 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:

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

关于python - 创建了一个 PyPI 包并安装,但运行时返回导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com