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 knowledge points about package import

2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the knowledge points about package import". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the knowledge points about package import"?

1. Use _ _ all__ to control the variables that can be imported

Using from module import * imports all variables in module by default. If you only want to import a few variables from the module, you can use _ _ all__ in module to control the variables you want to be imported by other modules.

# profile.py name=' Xiaoming 'age=18 _ _ all__= [' name']

Open python console to verify

> from profile import * > print (name) Xiaoming > print (age) Traceback (most recent call last): File ", line 1, in NameError: name 'age' is not defined

_ _ all__ applies only in the case of using from module import *.

It often appears in the _ _ init__.py of a package.

two。 The magic of namespace packages

Namespace package, for many people, may be an unfamiliar name.

Unlike the regular package we are familiar with, it does not have a _ _ init__.py file.

More specifically, it can merge two non-adjacent subpackages across spaces into a virtual machine package, which we call a namespace package.

For example, part of the code layout of a project is as follows

Foo-package/ spam/ blah.py bar-package/ spam/ grok.py

In both directories, there is a common namespace spam. There is no _ _ init__.py file in any directory.

Let's see what happens if you add both foo-package and bar-package to the python module path and try to import it.

> import sys > sys.path.extend (['foo-package',' bar-package']) > import spam.blah > import spam.grok >

When a package is a namespace package, it no longer has the same _ _ file_ attribute as a regular package, it is replaced by _ _ path__

> import sys > sys.path.extend (['foo-package',' bar-package']) > import spam.blah > import spam.grok > spam.__path__ _ NamespacePath (['foo-package/spam',' bar-package/spam']) > spam.__file__ Traceback (most recent call last): File ", line 1, in AttributeError: 'module' object has no attribute' _ _ file__'

3. A pit in module overloading

Due to the existence of sys.modules, when you import an imported module, it actually has no effect.

In order to reload the module, some people will remove the imported package from the sys.modules and then import it.

Just like down there.

> import foo.bar successful to be imported > import foo.bar > import sys > sys.modules ['foo.bar'] > del sys.modules [' foo.bar'] > import foo.bar successful to be imported

I am using import foo.bar in the above example. If you use the imported form of from foo import bar, you will find that overloading is also invalid.

This should be regarded as a small pit, people who do not know will fall into the pit and cannot climb out.

> import foo.bar successful to be imported > import foo.bar > import sys > del sys.modules ['foo.bar'] > from foo import bar >

Therefore, you may need to avoid reloading the module in a production environment. In debug mode, it will provide some convenience, but you should be aware of the disadvantages of this overload to avoid falling into the pit.

At this point, I believe you have a deeper understanding of "what are the knowledge points about package import?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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: 246

*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