【发布时间】:2021-12-01 05:44:06
【问题描述】:
创建一个简单的客户端库,以便使用我的 api 的人可以轻松使用它。对 python 相当陌生(3 个月),以前从未创建过我自己的模块/库/包。我看了很多非常简单的教程,并认为我做得很好。但是,尽管按照信中的说明进行操作,但我收到了一个未找到模块的错误。这是基本格式(注意,由于工作场所政策,我已经替换了大多数文件、类和方法的名称,但是它应该对结构没有影响)
python-sdk (the name of the repository)
| ⊢src (my boss wants everything wrapped inside of a src directory)
| | ⊢library (this will be the package that is imported by users)
| | | ⊢__init__.py
| | | ⊢filename.py
| | | | ⊢class myclass1
| | | | | ⊢staticmethod()
| | | | ⊢class myclass2
| | | | | ⊢method()
| | test-app.py
当前文件名包含 2 个类(非常简单的类,只是为了测试),名为 myclass1 和 myclass2。每个类都包含一个方法,现在只需添加或乘以 2 个值。我这样做只是为了学习创建库。知道后面代码怎么实现了。
初始化文件包含以下内容:
from filename import myclass1, myclass2
测试应用很简单:
from library import myclass1, myclass2
print(myclass1.staticmethod1(15, 20))
myclassinstance = myclass2(15, 20)
print(myclassinstance.method())
根据堆栈跟踪,它从 test-app.py 的第一行开始,它看到库的导入,因此它转到库中的 init 文件。然后在 init 的第 1 行,它看到了 from filename import myclass1, myclass2 并对文件名感到愤怒。
上面写着:
/home/sexmaster/PycharmProjects/python-sdk/venv/bin/python /home/sexmaster/PycharmProjects/python-sdk/src/test-app.py
Traceback (most recent call last):
File "/home/sexmaster/PycharmProjects/python-sdk/src/test-app.py", line 1, in <module>
from library import myclass1, myclass2
File "/home/derek1st/PycharmProjects/python-sdk/src/library/__init__.py", line 1, in <module>
from filename import class1, class2
ModuleNotFoundError: No module named 'filename'
Process finished with exit code 1
有什么猜测吗?这应该是非常基本的。我不知道出了什么问题。我在 pycharm 中没有任何价值。
【问题讨论】:
标签: python import module package