In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.