Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the three ways to import the Python module?

Shulou Source: shulou.com Published: 2022-06-02 18:25:27 10月03日 Update

This article introduces the relevant knowledge of "what are the three ways of Python import module". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Import

> import sys

> sys.path

['','C:\\ Python34\\ Lib\\ idlelib','C:\\ Windows\\ system32\\ python34.zip','C:\\ Python34\\ DLLs','C:\\ Python34\\ lib','C:\\ Python34','C:\\ Python34\\ lib\\ site-packages']

The most common way is to write the name of the module to be imported directly after the import.

2. From... Import...

Similar to import, except that it is more specific about the methods or variables to import, such as:

> from sys import path

> path

['','C:\\ Python34\\ Lib\\ idlelib','C:\\ Windows\\ system32\\ python34.zip','C:\\ Python34\\ DLLs','C:\\ Python34\\ lib','C:\\ Python34','C:\\ Python34\\ lib\\ site-packages']

However, it can cause namespace pollution, so import is recommended.

3. Import a module with a name string

We might want to import the module like this:

> > import "sys"

SyntaxError: invalid syntax

Python import receives variables instead of strings, so what about assigning "sys" to a variable?

> x = "sys"

> import x

Traceback (most recent call last):

File "", line 1, in

Import x

ImportError: No module named'x'

This doesn't work either, which means importing a module named x instead of the sys module represented by x.

We need to use the exec function:

> x = "sys"

> exec ("import" + x)

Sys.path which gynecology hospital in Zhengzhou is good for http://www.sptdfk.com/

['','C:\\ Python34\\ Lib\\ idlelib','C:\\ Windows\\ system32\\ python34.zip','C:\\ Python34\\ DLLs','C:\\ Python34\\ lib','C:\\ Python34','C:\\ Python34\\ lib\\ site-packages']

The import statement is built into a string and passed to the exec function for execution.

The disadvantage of exec is that it has to be compiled every time it is executed, and running multiple times will affect performance.

A better way is to use the _ _ import__ function.

> x = "sys"

> sys = _ import__ (x)

> sys.path

['','C:\\ Python34\\ Lib\\ idlelib','C:\\ Windows\\ system32\\ python34.zip','C:\\ Python34\\ DLLs','C:\\ Python34\\ lib','C:\\ Python34','C:\\ Python34\\ lib\\ site-packages']

This approach requires a variable to hold the module object for subsequent calls.

This is the end of the content of "what are the three ways to import Python modules". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Module mode variable function character string content name more knowledge no practical successful next representative hospital just dilemma gynaecology practice Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB Redmi MySQL vpn