In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to solve the cross-reference problem of Python 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!
The first cross-reference or cross-reference is actually an import loop. For a detailed description of the import loop, you can see my excerpt from the second edition of python Core programming: the Python import loop method.
The appendix gives a solution to cross-reference, try, no, but the cross-reference problem itself is very clear, if you are not clear what is cross-reference, see appendix I.
Circular references do not pay much attention in the python circle, and the language does not provide a mechanism to prevent circular dependencies.
In general, interdependence between modules should be avoided in the overall structure, that is, A does not depend on An if it depends on BPerry B, which is also a goal of code refactoring.
For emergencies, it is often too late to make big changes to the code.
Just find the module that causes the circular reference (at least two), figure out the reference relationship, and let a module import it when it is really needed (usually in a function), or put it into the last import of the code. in this way, the problem of module circular dependency can be basically solved.
A little trick:
When you look at the error stack of a circular reference, you will find a file that has been imported into the same module twice. Be careful to look around here.
Summary:
Import loops (cross-references) should be avoided during python development, but if you develop a large Python project, you are likely to end up in this situation.
Some of the more reliable methods are:
1. Put the import statement after the method definition.
two。 Move the import statement to the inside of the function, and the relevant module will be imported only when executed to this module.
Appendix:
One: cross-reference of Python module
Interpretation:
How can I have modules that mutually import each other?
The following two files refer to each other and the Python interpreter reports an error.
Foo.py:
From bar import bar_var
Foo_var=1
Bar.py:
From foo import foo_var
Bar_var=2
Import foo produces the following error:
> import foo
Traceback (most recent call last):
File "", line 1, in?
File "foo.py", line 1, in?
From bar import bar_var
File "bar.py", line 1, in?
From foo import foo_var
ImportError: cannot import name foo_var
> > >
But the error is not reported because of the cross-reference, but the reference does not exist!
When you import a module, you will not do it again if you find that it has been imported.
From foo import foo_var
It is equivalent to running foo.py and then making foo_var = foo.foo_var
This sentence will find that foo has been imported, but foo_var=1 has not yet executed the
So bar.foo_var = foo.foo_var will make an error.
The solution is not to import globally, but to import in local scope.
If you change from XXX import YYY to import XXX, you won't have the above error.
This is the end of the content of "how to solve the cross-reference problem of Python module". 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.