Python in several ways summarized import module

Following small way to bring an import module in Python several summary for everyone. Xiao Bian feel very good, now for everyone to share, but also to be a reference. Follow the small series together and come back handling large equipment in Wuxi , right
inside the module encapsulates many useful features, sometimes you need to be imported outside the calling module. There are several common ways:

1 . import

<font style="color:rgb(77, 77, 77)"><font face="&quot"><font style="font-size:16px">>>> 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']
</font></font></font>

The most common way to import directly to the module name written on the back of the import.

2 .from … import …

And import similar, but more specific method or variable you want to import, such as:

<font style="color:rgb(77, 77, 77)"><font face="&quot"><font style="font-size:16px">>>> 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']
</font></font></font>

But will pollute the namespace, it is recommended to use import.

3. Import module name string

We may want to import this module:

<font style="color:rgb(77, 77, 77)"><font face="&quot"><font style="font-size:16px">>>> import "sys"
SyntaxError: invalid syntax
</font></font></font>

python import reception is variable rather than a string, it would be "sys" assignment to a variable it?

<font style="color:rgb(77, 77, 77)"><font face="&quot"><font style="font-size:16px">>>> x="sys"
>>> import x
Traceback (most recent call last):
 File "<pyshell#4>", line 1, in <module>
  import x
ImportError: No module named 'x'
</font></font></font>

This does not work, this means that rather than import module x x stands for the name of the sys module.

We need to use the exec function:

<font style="color:rgb(77, 77, 77)"><font face="&quot"><font style="font-size:16px">>>> x="sys"
>>> exec("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']
</font></font></font>

The import statement string and constructed to perform the function passed to exec.

The disadvantage is that each execution to be exec compile, run multiple times can affect performance.

__Import__ better way is to use the function.

<font style="color:rgb(77, 77, 77)"><font face="&quot"><font style="font-size:16px">>>> 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']
</font></font></font>

This approach requires a variable to hold the object module, so that subsequent calls.

Published 50 original articles · won praise 1 · views 1162

Guess you like

Origin blog.csdn.net/hi_onebro/article/details/104657342