Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement module import in python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to import modules in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Import import module

When import time # imports everything in the module print (time.ctime ()) # calls the function # import in the module to import the module for the first time, the following three steps occur # 1. Open the module file # 2. The file corresponding to the execution module throws the names generated during execution into the module namespace # 3. In the program, there will be a module [alias] whose name only wants the module's namespace area # 4. When creating a module, do not have the same name as the module, otherwise the system will directly look in the nearest file instead of importing the real module # reference format: module name: function name can prevent errors caused by methods with the same name in different modules

Import sys View the path of environment variables

Sys.path

['','D:\ Python file\ python39.zip', 'D:\ Python file\ DLLs', 'D:\ Python file\ lib', 'D:\ Python file', 'D:\

PythonProject1\ pythonProject3\ venv', 'D:\ pythonProject1\ pythonProject3\ venv\ lib\ site-packages'

View the python installation path

Search path:

1. Current directory

two。 If the current directory is not available, search in the environment variable to view all paths using the path variable in the sys module

3. If it cannot be found, search the default path

Import several specific functions from the module

From time import ctime,time # part Import print (ctime ()) # from... import Import for the first time occurs three steps # 1 create a module namespace # 2 execute the corresponding file of the module, throw all the names generated during execution into the module namespace # 3 get a name in the current execution file namespace and point directly to the # name in the module It means that you can use it directly without any prefix # advantage: no prefix code is more concise # disadvantage: easy to conflict with names in the namespace in the current execution file

Similarities and differences:

Same: all execute the corresponding file of the module and also generate the namespace of the module

When calling a function, both need to go to the definition to find the scope relationship.

Different:

Import needs to be prefixed with from Import does not need to be prefixed

Import time as myTime # adds an alias print (myTime.ctime ())

After aliasing, the call cannot be called with the original name, but must be called with an alias.

This is the end of the article on "how to import modules in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report