What is the usage of import in python?

王林
Release: 2020-05-09 11:44:15
Original
7960 people have browsed it

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])
Copy after login

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])
Copy after login

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)
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template