返回介绍

Modules

发布于 2025-02-25 23:44:06 字数 2907 浏览 0 评论 0 收藏 0

In Pythoh, any .py file is a module in that it can be imported. Because the interpreter runs the entrie file when a moudle is imported, it is traditional to use a guard to ignore code that should only run when the file is executed as a script.

%%file foo.py
"""
When this file is imported with `import foo`,
only `useful_func1()` and `useful_func()` are loaded,
and the test code `assert ...` is ignored. However,
when we run foo.py as a script `python foo.py`, then
the two assert statements are run.
Most commonly, the code under `if __naem__ == '__main__':`
consists of simple examples or test cases for the functions
defined in the moule.
"""

def useful_func1():
    pass

def useful_fucn2():
    pass

if __name__ == '__main__':
    assert(useful_func1() is None)
    assert(useful_fucn2() is None)
Overwriting foo.py

Organization of files in a module

When the number of files you write grow large, you will probably want to orgnize them into their own directory structure. To make a folder a module, you just need to include a file named __init__.py in the folder. This file can be empty. For example, here is a module named pkg with sub-modules sub1 and sub2 .

./pkg:
__init__.py foo.py      sub1        sub2

./pkg/sub1:
__init__.py     more_sub1_stuff.py  sub1_stuff.py

./pkg/sub2:
__init__.py sub2_stuff.py
import pkg.foo as foo
foo.f1()
1
pkg.foo.f1()
1

How to import a module at the same level

Within a package, we need to use absolute path names for importing other modules in the same directory. This prevents confusion as to whether you want to import a system moudle with the same name. For example, foo.sub1.more_sub1_stuff.py imports functions from foo.sub1.sub1_stuff.py

! cat pkg/sub1/more_sub1_stuff.py
from pkg.sub1.sub1_stuff import g1, g2

def g3():
    return 'g3 uses %s, %s' % (g1(), g2())
from pkg.sub1.more_sub1_stuff import g3

g3()
'g3 uses g1, g2'

How to import a moudle at a different level

Again, just use absolute paths. For example, sub2_stuff.py in the sub2 directory uses functions from sub1_stuff.py in the sub1 directory:

! cat pkg/sub2/sub2_stuff.py
from pkg.sub1.sub1_stuff import g1, g2

def h1():
    return g1()

def h2():
    return g1() + g2()
from pkg.sub2.sub2_stuff import h2

h2()
'g1g2'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文