问题描述
我创建了一个简单的包并且能够在 PyPI 中发布它.它的文件结构是:
I've created a simple package and was able to publish it in PyPI.It's file struc is:
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"main", mod_spec)文件C:\Python34\lib\runpy.py",第 86 行,在 _run_code 中exec(代码,run_globals)文件C:\Python34\Scripts\daysgrounded.exe__main__.py",第 5 行,在文件C:\Python34\lib\site-packages\daysgrounded__main__.py",第 12 行,在导入命令行导入错误:没有名为cli"的模块
I installed it in another machine and run c:\Python34\Scripts\daysgrounded.exe, but it returns the following error:Traceback (most recent call last): File "C:\Python34\lib\runpy.py", line 171, in _run_module_as_main "main", mod_spec) File "C:\Python34\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Python34\Scripts\daysgrounded.exe__main__.py", line 5, in File "C:\Python34\lib\site-packages\daysgrounded__main__.py", line 12, in import cliImportError: No module named 'cli'
您可以在 https://github.com/jcrmatos/DaysGrounded 和https://pypi.python.org/pypi?name=daysgrounded&version=0.0.8&:action=display
我做错了什么?是在 setup.py 中吗?
What am I doing wrong? Is it in 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
推荐答案
这是由于 三种调用python包的方式:
python daysgrounded
python daysgrounded/__main__.py
python -m daysgrounded
如果您在项目中尝试每一种方法,您会注意到第三种方法不起作用,而这正是 runpy
使用的方法.它不起作用的原因是因为您的 sys.path
不正确,因为 python 首先将 daysgrounded/
添加到您的 sys.path
两种方式,但不是第三种.
If you try each of these on your project you'll notice that the third method doesn't work, which is exactly the one runpy
uses. The reason it doesn't work is because your sys.path
is incorrect, because python adds daysgrounded/
to your sys.path
for the first two ways, but not the third.
为了让这三种方式都能工作,你必须确保你的 sys.path 是正确的,有两种方法可以做到这一点.
In order for all three ways to work, you have to make sure that your sys.path is correct, and there are two ways of doing this.
如果你想能够做import cli
,sys.path
应该是daysgrounded/
,这意味着你需要修改__init__.py
将其添加到 sys.path
:
If you want to be able to do import cli
, sys.path
should be daysgrounded/
, which means you need to modify __init__.py
to add it to 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
:
If you want to be able to do from daysgrounded import cli
, sys.path
should be the directory above daysgrounded/
, which means you need to modify __main__.py
to add it to sys.path
:
import sys
import os.path
sys.path.insert(1, os.path.dirname(sys.path[0]))
这篇关于创建了一个 PyPI 包并安装,但运行时返回导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!