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

What are the ways for Python to import modules?

2025-01-21 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 ways to import modules in Python". 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. Regular import

Regular import should be the most commonly used import method, something like this:

You just need to use the word import and then specify the module or package you want to import. The advantage of importing in this way is that you can import multiple packages or modules at once. Although this saves space, it violates the Python style guidelines. The Python style guide recommends that each import statement be on a separate line. Sometimes when you import a module, you want to rename it. This function is easy to implement.

two。 Import using the from statement

Many times you just want to import a module or a part of the library. In a complex code base, it is useful to see where a function was imported. However, if your code is well maintained and highly modular, it is convenient and concise to import only part of the content from a module.

Of course, you can also use the from method to import the entire contents of the module.

This is convenient in a few cases, but it can also disrupt your namespace. The problem is that you may have defined a variable or function with the same name as the import module, and if you try to use a variable or function with the same name in the os module, you will actually use what you define. As a result, you may end up with a rather confusing logic error. The only module in the standard library that I recommend importing completely is Tkinter.

If you happen to be writing your own module or package, you will be advised to import everything in the _ init_.py file to make the module or package easier to use. Personally, I prefer to import explicitly rather than implicitly.

3. Relative import

Specifically, periods are used to determine how to import other packages or modules relatively. The reason for doing this is to avoid conflicts between modules imported into the standard library by chance.

Relative imports apply to the code you eventually put into the package. If you have written a lot of highly relevant code, you should use this import method. You will find that there are many popular packages on PyPI that also use relative imports. Also note that if you want to import across multiple file levels, you only need to use multiple periods.

4. Optional import

If you want to give priority to a module or package, but also want to have an alternative without that module or package, you can use the optional import method. By doing so, you can import multiple versions of a software that support it or achieve performance improvements.

5. Local import

When you import a module in a local scope, you perform a local import. If you import a module at the top of the Python script file, you are importing the module into the global scope, which means that any subsequent function or method may access the module.

One of the advantages of using a local scope is that the module you use may take a long time to import, and if so, it may make more sense to place it in a function that is not called frequently rather than directly in the global scope. To be honest, I have almost never used partial imports, mainly because if there are import statements everywhere inside the module, it will be difficult to tell the reason and purpose of doing so. By convention, all import statements should be at the top of the module.

This is the end of the content of "what are the ways to import modules in Python". 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.

Share To

Development

Wechat

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

12
Report