How do Python modules work

人盡茶涼 提交于 2019-12-08 17:13:47

问题


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)


回答1:


If the module is specific to your program, you can put into the same directory as the main script. From the documentation:

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.



来源:https://stackoverflow.com/questions/7803493/how-do-python-modules-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!