在Python中导入包和子包
我是Python新手。尽管该语言干净且易于学习,但我发现很难理解导入的机制。我在网上搜索了很多相关内容,大多数文章都不全面,对于像我这样的初学者来说相当混乱。所以,我想再次请这里的专家帮助我理解这个概念。
Python3 --version = Python 3.10.1
System - Mac M1 (2020)
问题 1 - 我有以下文件夹结构 -
Package1/
/__init__.py
/module1.py
/module2.py
/SubPackage1/
/__init__.py
/subpack1_module1.py
/SubPackage2/
/__init__.py
/subpack2_module1.py
现在我想导入 SubPackage1 中的整个 SubPackage2 以使用其模块。这是我在 SubPackage1 的 '__init__.py
' 文件中编写的代码,
from Package1 import SubPackage2
这向我显示以下错误 -
ModuleNotFoundError: No module named 'Package1'
将一个子包导入另一个子包的正确方法是什么?虽然我发现了一个作弊方法,如下 -
import sys
sys.path.append('root/Package1/')
import SubPackage2
但是我认真地觉得这个方法是菜鸟方法并且不可扩展。
问题 2 - 现在假设我已经完成了上面的包,想在另一个文件夹/包/程序中使用它,同样的,我们使用 Pandas 时只需编写 import Pandas as pd 即可。最好的方法是什么?为了更清楚,让我定义文件夹结构 -
Package 1/
/__init__.py
/all other subpackages and modules
Folder 2/
/test.py
现在在 test.py 中我想使用 Package1。所以我想在 test.py 上写 -
import Package1
但它再次给了我同样的 ModuleNotFoundError 。 sys.path.append('path') 技巧有效,但不可扩展。另一种方法是创建轮子(使用setup.py),但这样的话,如何在开发模式下使用轮子,这样我就不必每次版本有变化时都创建轮子。
I am new to python. Although the language is clean and easy to learn, I am finding a hard time understanding the mechanics of imports. I have searched a lot about it on the web, most of the articles are not comprehensive and are rather confusing for a beginner like me. So, once for all, I want to ask the experts here to help me understand this concept.
Python3 --version = Python 3.10.1
System - Mac M1 (2020)
Problem 1 - I have the following folder structure -
Package1/
/__init__.py
/module1.py
/module2.py
/SubPackage1/
/__init__.py
/subpack1_module1.py
/SubPackage2/
/__init__.py
/subpack2_module1.py
Now I want to import the entire SubPackage2 in SubPackage1 to use its modules. This is the code I am writing in the '__init__.py
' file of SubPackage1
from Package1 import SubPackage2
This is showing me the following error -
ModuleNotFoundError: No module named 'Package1'
What is the right way to import one sub-package into another sub-package? Although I have found a cheat to this as follows -
import sys
sys.path.append('root/Package1/')
import SubPackage2
But I seriously feel that this method is a noob method and is not scalable.
Problem 2 - Now let's say I have completed the above package and want to use it in another Folder/Package/Program, in the same way, we use Pandas by just writing import Pandas as pd. What is the best way? To be more clear let me define the folder structure -
Package 1/
/__init__.py
/all other subpackages and modules
Folder 2/
/test.py
Now in test.py I want to use Package1. So I want to write on test.py -
import Package1
But again it gives me the same ModuleNotFoundError. sys.path.append('path') trick works but it is not scalable. Another way is to create wheels (using setup.py), but in that case, how to use wheels in development mode so that I don't have to create wheels every time there is a change in the version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是 python 相对于其他语言的最佳功能之一。导入很简单。
您可以直接从同一文件夹或下面的任何文件导入,如下所示:
如果您在
module1.py
中并且想要从 module2.py 导入代码,您只需执行以下操作:您甚至可以添加方便的像这样的别名:
然后您就可以访问
module2
的所有类、属性和函数。同样,您可以以非常相似的方式从子文件夹导入(因为 python 会看到所有子文件夹)。因此,如果您在
module1.py
中并且想要导入subpack2_module1.py
,您也可以执行相同的操作。这里我们使用点符号表示文件夹路径。同样,您甚至可以添加这样的别名:
使用上面的内容,没有歧义并且结构很干净。编码员确切地知道代码所在的位置。
笔记:
还有从目录结构之外的模块导入的方法,但这种方法不太常见,因为目录代表项目。
This is probably one of the best features of
python
over other languages. The importing is simple.You can import directly from any file in the same folder or below like this:
If you are in
module1.py
and want to import code from module2.py you simple do this:you can even add a convenient alias like this:
You then have access to all the classes, attributes and functions of
module2
.likewise, you can import from subfolders in a very similar way (as python sees all the subfolders). So if you are in
module1.py
and want to importsubpack2_module1.py
you do the same. Here we use the dot notation for the folder paths.again, you could even add an alias like this:
Using the above, there is no ambiguity and the structure is clean. The coder know exactly where the code is situated.
note:
There are also ways of importing from modules outside of the directory structure, but that is less common as the directory represents the project.