Python 模块

Python 模块基本上是一个 Python 脚本文件,可以包含变量、函数和类. Python 模块有助于我们组织我们的代码,然后将其引用到其他类或 Python 脚本中。

Python 模块

包含 Python 定义和陈述的文件被称为 Python 模块,因此,自然而然地,该文件名称是附带字母 .py 的模块名称. 为了更好地理解,让我们创建一个 Python 模块来完全探索它。

 1def printForward(n):
 2
 3    #print 1 to n
 4    for i in range(n):
 5        print(i+1)
 6
 7def printBackwards(n):
 8
 9    #print n to 1
10    for i in range(n):
11        print(n-i)

现在在python解释器中,用以下命令导入此模块;

1import printNumbers

This import command will look for printNumbers.py file in the current directory and PATH variable locations. Once the file is found, the code in the file will be available for us to use. Now to access the module's function we need to use the module name like below: python modules, python module import example Sometimes if the module is big, to ease the function calling we can rename the import like below: python module import example function

导入 Python 模块的特定函数

Sometimes it's unnecessary to import all the functions of a python module. We may need only one or two functions. In that case, we can use the following variant of the import statement; python tutorial, python import module function using from One thing to notice here, as we import printForward, it is included in the current symbol table. So we don't need to call the function like - printNumbers.printForward() Another variant can be useful sometimes. Here we used renaming as we did previously to ease our use of the function. python tutorial, python modules import variant with from and as Also if we want to import all the names that a module defines there is another variant for importing. This imports all names except those beginning with an underscore (_). But this is not ideal practice as this introduces an unknown set of names into the interpreter. python modules tutorial, module import variant 3 with from and star

关于 Python 模块的问题

让我们看看一些与Python模块相关的常见问题。

Python中的内置模块有哪些?

在Python中有许多内置模块,其中一些重要内容是:(/社区/教程/python-collections),((/community/tutorials/python-datetime),(logging)(/community/tutorials/python-logging),(math)(/community/tutorials/python-math),(numpy)(/community/tutorials/python-numpy-tutorial),(os)(/community/tutorials/python-os-module),(pip)(pip)(community/tutorials/python-pip),(sys)(community/tutorials/python-sys-module),以及(time/community/tutorials/python-time-module)。您可以在Python

Python 中模块和包的区别是什么?

Python package是 Python 模块的集合,Python 模块是单个 Python 文件,而 Python 包是一个具有多个 Python 脚本和 __init__.py 文件的目录,定义了包细节。

我可以在哪里找到 Python 模块列表?

您可以在其官方页面上找到Python模块的列表(Python Module Index)。 但是,如果你正在寻找可用的Python模块,那么你可以在Python壳中执行‘help('modules')’命令来获取可用的模块列表。

Python Modules List

请查看此 GitHub 存储库列出最重要的 python 模块,并通过其特定的教程和示例程序来学习它们。

如何从不同的目录中导入一个模块?

当我们试图导入一个 python 模块时,它会检查当前目录和 PATH 变量位置,所以如果你的 python 文件不在这些位置,那么你会得到ModuleNotFoundError。解决方案是导入sys模块,然后将所需目录附加到其路径变量中。下面的代码显示了我们试图从不同的目录导入时的错误,以及我如何通过将其目录添加到路径变量来修复它。

 1$ python3.7
 2Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
 3[Clang 6.0 (clang-600.0.57)] on darwin
 4Type "help", "copyright", "credits" or "license" for more information.
 5>>> import test123
 6Traceback (most recent call last):
 7  File "<stdin>", line 1, in <module>
 8ModuleNotFoundError: No module named 'test123'
 9>>> import sys
10>>> sys.path.append('/Users/pankaj/temp')
11>>> import test123
12>>> test123.x
1310
14>>> test123.foo()
15foo
16>>>

Python 模块列表

有成千上万的Python模块,每天都在开发,我们已经为许多流行的Python模块撰写了教程,只需遵循下表的链接来学习这些模块。

QQ Python 模块 QQ QQ -- -- QQ [Python os module] (/community/tourises/python-os-module)__ _. [Python sys moment] (/community/troits/python-sys-module) [Python时间] (/community/troits/python时间) [Python时间] (/community/troits/mysql-ext-trot-trothot-trot-trot-trot-trot-trot-trot-trot-trot-trot-trothot-trot-trot-trothot-trothot-trothot-trothot-trothothot-trothot-trotrothotro-thot-trotho-thothot-t-thotro-tto-trotro-thotho-tho-trotro-tro-tro-thothothothotho [Python系统命令](/community/troductions/python-system-command-os-sub process-call) [Python Daemon Thread] (/community/troductions/python-daem-thread) [Python Copy] (/community/troductions/python-copy-deep-dep-copy) [Python Copy] (/community/troduction/tion-py-py-deep-deep-copy) [Python Copy][ ] [Python 线程模块] (/community/touris/python-threading-multipreading) | |. [Python struct] (/社区/道/平通-地-地-地-地-地-地-地-地-地-地-地-地-地) [Python伐木] (/社区/地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地-地 /-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- [Python检查模块] (/社区/touris/python-inspect-module) _. [Python send-smtplib] [Python send-smt-smtplib] [Python temfile] [Python temfile] (/社区/tiols/python-temfile-module] [Python时间] [Python SQLite] (/社区/tiols/python-sqlite-trodule-trodule] [ [Python时间] (/社区/tiols/python-timit-timit-module] [Python时间] [Python时间] (/社区/tios/pyon-timit-module] ( BR1] [Python getpass module] (/community/trodures/python-getpass-module) [Python urllib] (/community/trodures/python-urllib-python-3-urllib) [Python pytz] (/community/trodures/python-get-module) [Python倒数] (/commun/trodures/python-pendulum-module) | Python箭头模块

参考:

Published At
Categories with 技术
Tagged with
comments powered by Disqus