Python 包结构没有导入正确的模块
Python package structure not importing correct module
按照 PYPI 文档,它展示了如何安装一个包,效果很好。但是一旦我将 class 引入到文件中,我就找不到 class。我附上了很多代码,但我认为上下文是必要的。
现在当我在目录中工作时这工作正常但是一旦它上传到 PYPI 并在我的计算机上的其他地方使用它找不到 class.
树
package_upload
├── EasyNN
│ ├── EasyNN.py
│ ├── __init__.py
│ └── import_this.py
├── LICENSE.txt
├── README.md
└── setup.py
Setup.py
import setuptools
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setuptools.setup(
name='EasyNN',
version='0.0.3',
description='Currently Testing',
packages=setuptools.find_packages(),
python_requires='>=3.6',
url="https://github.com/danielwilczak101/EasyNN",
author="Daniel",
author_email="daniel@gmail.com",
long_description = long_description,
long_description_content_type = "text/markdown",
classifier=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
"Operating System :: OS Independent",
],
install_requires = ["matplotlib ~= 3.3.2",
"pytest>=3.7",
"tabulate >=0.8.7"
],
)
EasyNN.py
import import_this
class NN:
def say_hello(self):
print("Hello NN World!")
import_this.py
def foo():
print("I've been imported")
运行Python代码
import EasyNN
nn = EasyNN.NN()
错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'EasyNN' has no attribute 'NN'
>>>
您正在尝试访问位于 EasyNN/EasyNN.py
内的 NN
。如果你想让 NN
class 从顶级导入中可用,你必须将它包含在你的顶级 __init__.py
文件中:
# EasyNN/__init__.py
from EasyNN import NN
然后,从包外,在您计算机的其他地方,NN
现在可以通过顶级导入获得。
当您导入一个包时,您无法直接访问该包中文件中包含的对象。您需要将它们包含在相应包的 __init__.py
中,或者像这样访问它们:
from EasyNN.EasyNN import NN
上面第一个EasyNN
是包,但是第二个是.py文件叫EasyNN
那个conatins class NN
.
按照 PYPI 文档,它展示了如何安装一个包,效果很好。但是一旦我将 class 引入到文件中,我就找不到 class。我附上了很多代码,但我认为上下文是必要的。
现在当我在目录中工作时这工作正常但是一旦它上传到 PYPI 并在我的计算机上的其他地方使用它找不到 class.
树
package_upload
├── EasyNN
│ ├── EasyNN.py
│ ├── __init__.py
│ └── import_this.py
├── LICENSE.txt
├── README.md
└── setup.py
Setup.py
import setuptools
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setuptools.setup(
name='EasyNN',
version='0.0.3',
description='Currently Testing',
packages=setuptools.find_packages(),
python_requires='>=3.6',
url="https://github.com/danielwilczak101/EasyNN",
author="Daniel",
author_email="daniel@gmail.com",
long_description = long_description,
long_description_content_type = "text/markdown",
classifier=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)",
"Operating System :: OS Independent",
],
install_requires = ["matplotlib ~= 3.3.2",
"pytest>=3.7",
"tabulate >=0.8.7"
],
)
EasyNN.py
import import_this
class NN:
def say_hello(self):
print("Hello NN World!")
import_this.py
def foo():
print("I've been imported")
运行Python代码
import EasyNN
nn = EasyNN.NN()
错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'EasyNN' has no attribute 'NN'
>>>
您正在尝试访问位于 EasyNN/EasyNN.py
内的 NN
。如果你想让 NN
class 从顶级导入中可用,你必须将它包含在你的顶级 __init__.py
文件中:
# EasyNN/__init__.py
from EasyNN import NN
然后,从包外,在您计算机的其他地方,NN
现在可以通过顶级导入获得。
当您导入一个包时,您无法直接访问该包中文件中包含的对象。您需要将它们包含在相应包的 __init__.py
中,或者像这样访问它们:
from EasyNN.EasyNN import NN
上面第一个EasyNN
是包,但是第二个是.py文件叫EasyNN
那个conatins class NN
.