我目前正在尝试创建我的第一个python包。可悲的是,我遇到了一些错误。我只想尝试定义一些应该可导入的类。
继承我的文件夹结构
/pip-blitz-query
/blitzjs_query
__init__.py
blitz.py
blitz_http.py
...
setup.py
当我尝试从Blitz
导入类blitzjs_query
时发生的错误:
>>> 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'],
)
答案 0 :(得分:3)
通常你使用相对导入(在子模块前加上一个或多个.
):
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已发表相关进口将使用领先点。单个前导点表示相对导入,从当前包开始。两个或多个前导点为当前包的父级提供相对导入,在第一个之后每个点一个级别。 [...]
答案 1 :(得分:0)
您的模块名为blitzjs_query
,因此请将所有导入作为blitzjs_query.
像这样:
from blitzjs_query.blitz import Blitz
from blitzjs_query.connection import Connection
from blitzjs_query.blitz_queue import Queue
from blitzjs_query.blitz_socket import Socket
from blitzjs_query.blitz_http import Http