Home  >  Article  >  Backend Development  >  What is the usage of import in python?

What is the usage of import in python?

王林
王林Original
2020-05-09 11:44:157966browse

What is the usage of import in python?

import in python is used to import modules.

Specific usage:

The following program uses the simplest syntax to import the entire module to import the specified module:

# 导入sys整个模块
import sys
# 使用sys模块名作为前缀来访问模块中的成员
print(sys.argv[0])

When importing the entire module, you can also specify an alias for the module. For example, the following program:

# 导入sys整个模块,并指定别名为s
import sys as s
# 使用s模块别名作为前缀来访问模块中的成员
print(s.argv[0])

You can also import multiple modules at one time, and separate multiple modules with commas. For example, the following program:

# 导入sys、os两个模块
import sys,os
# 使用模块名作为前缀来访问模块中的成员
print(sys.argv[0])
# os模块的sep变量代表平台上的路径分隔符
print(os.sep)

While importing multiple modules, you can also specify aliases for the modules, such as the following program:

# 导入sys、os两个模块,并为sys指定别名s,为os指定别名o
import sys as s,os as o
# 使用模块别名作为前缀来访问模块中的成员
print(s.argv[0])
print(o.sep)

Recommended tutorial: python tutorial

The above is the detailed content of What is the usage of import in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn