gpt4 book ai didi

python - 自己的pip python包导入不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:55 25 4
gpt4 key购买 nike

我目前正在尝试创建我的第一个 python 包。可悲的是我遇到了一些错误。我只是想定义一些应该可导入的类。

这是我的文件夹结构

/pip-blitz-query
/blitzjs_query
__init__.py
blitz.py
blitz_http.py
...
setup.py

这是我尝试从 blitzjs_query 导入类 Blitz 时发生的错误:

>>> import blitzjs_query
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\blitzjs_query\__init__.py", line 1, in <module>
from blitz import Blitz
ImportError: No module named 'blitz'

这是我的 __init__.py 文件:

from blitz import Blitz
from connection import Connection
from blitz_queue import Queue
from blitz_socket import Socket
from blitz_http import Http

最后是我的 setup.py 文件。我已经尝试了 __init__.py 文件的不同变体,有些里面什么也没有——但它根本行不通

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

# Get the long description from the README file
here = path.abspath(path.dirname(__file__))
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
long_description = f.read()

setup(
name='blitzjs_query',

version='0.0.2',

description='HTTP/Socket.io adapter for the blitz.js framework',
long_description=long_description,

url='https://github.com/nexus-devs/pip-blitz-query',

author='Nexus Devs',
author_email='nexus@nexus-stats.com',

license='MIT',

classifiers=[
'Development Status :: 1 - Planning',
'Intended Audience :: Developers',
'Topic :: Internet :: WWW/HTTP',
'License :: OSI Approved :: MIT License',

'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],

keywords='development blitz nexus socket.io http',

packages=find_packages(),

install_requires=['pymitter', 'socketIO-client', 'promise', 'requests'],
)

最佳答案

通常您使用相对导入(在子模块前加上一个或多个 .):

from .blitz import Blitz
from .connection import Connection
from .blitz_queue import Queue
from .blitz_socket import Socket
from .blitz_http import Http

因为您没有创建名为 blitz 的模块,而只创建了一个模块 blitzjs_query.blitz

可以引用PEP 328有关相对和绝对导入的更多信息:

Guido has Pronounced that relative imports will use leading dots. A single leading dot indicates a relative import, starting with the current package. Two or more leading dots give a relative import to the parent(s) of the current package, one level per dot after the first. [...]

关于python - 自己的pip python包导入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43667811/

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