python module_Python module

python module

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.

A Python Module is essentially a python script file that can contain variables, functions and classes. Python modules help us organize our code and then reference them in other classes or python scripts.

Python modules ( 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.

A file containing Python definitions and statements is called a python module. Therefore, the file name is naturally the module name, suffix .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.

For better understanding, let's create a python module to fully explore it. First create a printNumbers.pyfile named .with the following content.

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;

Now, in the python interpreter, import the module with the following command;

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.

This import command will look for printNumbers.pyfiles in the current directory and in the PATH variable location. Once the file is found, the code in the file will be available to us.

Now to access the module’s function we need to use the module name like below:

Now to access the functionality of the module, we need to use the module name like this:

Sometimes if the module is big, to ease the function calling we can rename the import like below:

Sometimes, if the module is large, to simplify function calls, we can rename the import as follows:

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中有很多内置模块。 其中一些重要的参数是– 集合日期时间日志记录数学numpyospipsystime 。 您可以在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 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模块编写了教程。 只需点击下表中的链接即可学习这些模块。

Python Modules
Python os module
Python sys module
Python time
Python MySQL
Python CSV
Python multiprocessing
Python pickle
Python time sleep
Python queue
Python unittest
Python socket
Python SimpleHTTPServer
Python json
Python signal
Python random
Python System Command
Python Daemon Thread
Python Copy
Python threading module
Python struct
Python logging
Python subprocess
Python argparse
Python functools
Python itertools
Python getopt
Python ftp
Python tarfile
Python lxml
Python ConfigParser
Python datetime
Python decimal module
Python collections
Python zipfile
Python pdb
Python io
Python fractions
Python AST
Python HTTP
Python xmltodict
Python gzip
Python HTML Parser
Python inspect module
Python Send Email
Python tempfile
Python SQLite
Python shutil
Python timeit
Python getpass module
Python urllib
Python pytz
Python pendulum
Python arrow module
Python模块
Python操作系统模块
Python sys模块
Python时间
Python MySQL
Python CSV
Python多处理
Python泡菜
Python时间睡眠
Python队列
Python单元测试
Python套接字
Python SimpleHTTPServer
Python JSON
Python信号
Python随机
Python系统命令
Python守护程序线程
Python复制
Python线程模块
Python结构
Python日志记录
Python子进程
Python的argparse
Python功能工具
Python Itertools
Python getopt
Python的FTP
python tar文件
python lxml
Python ConfigParser
Python日期时间
Python十进制模块
Python集合
Python压缩档
python pdb
Python io
Python分数
Python AST
Python HTTP
Python xmltodict
Python gzip
Python HTML解析器
Python检查模块
Python发送电子邮件
Python临时文件
Python SQLite
Python关闭
Python timeit
Python getpass模块
Python URLlib
python pytz
Python摆锤
Python箭头模块

References:

参考文献:

翻译自: https://www.journaldev.com/14329/python-modules

python模块

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324110732&siteId=291194637