python模块_Python模块
python模块
Python Module is essentially a python script file that can contain variables, functions, and classes. Python modules help us in organizing our code and then referencing them in other classes or python scripts.
Python Module本质上是一个python脚本文件,可以包含变量,函数和类。 Python模块可帮助我们组织代码,然后在其他类或python脚本中引用它们。
Python模块 (**Python Modules**)
A file containing Python definitions and statements is called a python module. So naturally, the file name is the module name which is appended with the suffix .py
.
包含Python定义和语句的文件称为python模块。 因此,文件名自然就是模块名,后缀.py
。
For better understanding let’s create a python module to explore it completely. First create a file named printNumbers.py
with the following contents.
为了更好地理解,我们创建一个python模块来对其进行全面探索。 首先创建一个名为printNumbers.py
的文件,其中包含以下内容。
def printForward(n):
#print 1 to n
for i in range(n):
print(i+1)
def printBackwards(n):
#print n to 1
for i in range(n):
print(n-i)
Now in the python interpreter import this module with the following command;
现在,在python解释器中,使用以下命令导入该模块;
import 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.
此导入命令将在当前目录和PATH变量位置中查找printNumbers.py
文件。 找到文件后,文件中的代码将可供我们使用。
Now to access the module’s function we need to use the module name like below:
现在要访问模块的功能,我们需要使用如下模块名称:
Sometimes if the module is big, to ease the function calling we can rename the import like below:
有时,如果模块很大,为简化函数调用,我们可以按以下方式重命名导入:
导入Python模块的特定功能 (**Importing specific function of a Python Module**)
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模块的所有功能。 我们可能只需要一个或两个功能。 在这种情况下,我们可以使用import语句的以下变体;
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()
在这里要注意的一件事,当我们导入printForward
,它已包含在当前符号表中。 因此,我们不需要调用– printNumbers.printForward()
类的函数。
Another variant can be useful sometimes. Here we used renaming as we did previously to ease our use of the function.
有时,另一个变体可能会有用。 在这里,我们像以前一样使用重命名来简化功能的使用。
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模块常见问题解答 (**FAQs on Python Modules**)
Let’s look into some commonly asked questions related to Python modules.
让我们看一些与Python模块相关的常见问题。
Python中有哪些内置模块? (**What are the built-in modules in Python?**)
There are a lot of built-in modules in Python. Some of the important ones are – collections, datetime, logging, math, numpy, os, pip, sys, and time. You can execute help('modules')
command in Python shell to get the list of available modules.
Python中有很多内置模块。 其中一些重要的参数是– 集合 , 日期时间 , 日志记录 , 数学 , numpy , os , pip , sys和time 。 您可以在Python Shell中执行help('modules')
命令以获取可用模块的列表。
Python中的模块和软件包有什么区别? (**What is the difference between module and package in Python?**)
Python package is a collection of python modules. Python module is a single python file whereas python package is a directory having multiple python scripts and __init__.py
file defining the package details.
Python包是python模块的集合。 Python模块是单个python文件,而python package是具有多个python脚本和__init__.py
文件的目录,这些文件定义了软件包的详细信息。
在哪里可以找到Python模块列表? (**Where can I find the Python Modules List?**)
You can find the list of Python modules from their official page of Python Module Index. However, if you are looking for the Python modules available to you, then you can execute help('modules')
command in Python shell to get the list of available modules.
您可以在Python模块索引的官方页面上找到Python模块列表。 但是,如果您正在寻找可用的Python模块,则可以在Python Shell中执行help('modules')
命令以获取可用模块的列表。
Python Modules List
Python模块列表
GitHub Repository for a list of most important python modules and learn them through their specific tutorials and example programs. GitHub存储库以获取最重要的python模块列表,并通过其特定的教程和示例程序来学习它们。
如何从其他目录导入模块? (**How can I import a module from the different directory?**)
When we try to import a python module, it’s looked into the current directory and the PATH variable location. So if your python file is not present in these locations, then you will get ModuleNotFoundError
. The solution is to import sys
module and then append the required directory to its path variable.
当我们尝试导入python模块时,它会查看当前目录和PATH变量位置。 因此,如果这些位置不存在您的python文件,则将得到ModuleNotFoundError
。 解决方案是导入sys
模块,然后将所需目录附加到其path变量。
Below code shows the error when we try to import from a different directory and how I am fixing it by adding its directory to the path variable.
下面的代码显示了当我们尝试从其他目录导入时的错误,以及如何通过将其目录添加到path变量来修复该错误。
$ python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test123
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'test123'
>>> import sys
>>> sys.path.append('/Users/pankaj/temp')
>>> import test123
>>> test123.x
10
>>> test123.foo()
foo
>>>
Python模块列表 (**Python Modules List**)
There are thousands of Python modules and more are getting developed every day. We have written tutorials for a lot of popular Python modules. Just follow the links from the below table to learn these modules.
有成千上万的Python模块,并且每天都有越来越多的模块开发。 我们已经为许多流行的Python模块编写了教程。 只需点击下表中的链接即可学习这些模块。
References:
参考文献:
https://docs.python.org/3/tutorial/modules.html
https://docs.python.org/3/tutorial/modules.html
https://docs.python.org/3/py-modindex.html
https://docs.python.org/3/py-modindex.html
翻译自: https://www.journaldev.com/14329/python-modules
python模块
还没有评论,来说两句吧...