Python模块如何工作 [英] How do Python modules work
问题描述
我想知道Python模块如何工作? 假设我想让我的Python程序带有一些自定义模块与之打包,我应该将它们与主.py文件放在一起(如果不使用通常的方式如何加载它们)
I was wondering how do Python modules work? Say I want to make my Python program some custom modules to package with it, where would I put them in relation to the main .py file?(and how would I load them if not using the usual way)
推荐答案
如果模块特定于您的程序,则可以将其放置在与主脚本相同的目录中.从文档:
If the module is specific to your program, you can put into the same directory as the main script. From the documentation:
导入名为
spam
的模块时,解释器将在包含输入脚本的目录中,然后在由环境变量PYTHONPATH指定的目录列表中搜索名为spam.py的文件.
When a module named
spam
is imported, the interpreter searches for a file named spam.py in the directory containing the input script and then in the list of directories specified by the environment variable PYTHONPATH.
我建议您阅读完整的有关模块的教程.它很短,并且有很多有用的信息.
I would recommend that you read the entire tutorial on modules. It's pretty short and has a lot of useful info.
这篇关于Python模块如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!