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 mainly explains how to solve the problem of Python relative import error report. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Python relative import error report how to solve" it!
Relative import
Relative import refers to the mutual import between different modules under the same project, which is called relative import.
Relative imported cases
The project structure diagram is as follows:
The project name is project, and there are xx subprojects and test.py modules in this directory. There are two subdirectories, y and z, in xx subdirectory, abc.py module and yy.py module in y subdirectory, and zz.py module in z subdirectory. Yy.py is the entrance to the xx subproject. Test.py is the entrance to the entire project.
Contents of the test.py module
"this module is the entrance to the entire project project"from xx.y import yyyy.f2 () print (yy.num1, yy.num2, yy.num3) out:12310hello10 123 888"
Contents of the yy.py module
"" this module is the entry point for external references to xx subprojects "" from .ABC import * from.. z.zz import * num3 = 888def f2 (): print (num2) print (num1)
Contents of the zz.py module
"" subproject, imported submodule "" num1 = 10def f (): print ('hello')
Contents of the abc.py module
"" subproject, imported submodule "num2 = 123"
The above is the framework of the simulation of large-scale projects, test run test.py module can see that the output is normal, no problem at all.
Take a closer look at the contents of the yy.py module:
From .abcimport * statement:. Represents the current directory, and abc represents the abc.py module.
From.. z.zz import * statement,.. Represents the parent directory, z represents the z folder, and zz represents the zz.py module.
By the way. Represents the grandfather directory, please be familiar with the from path identification structure.
Relative import: with from. The opening statement, such as from .abcimport *.
Absolute import: a statement that begins with a folder or package or module name, such as from xx.y import yy.
The above has not reported an error yet, but if you try to debug the yy.py module, you will report an error when you run the yy.py module directly.
Analysis of relative import error report
Isn't it very strange! Why running the test.py module does not report an error when calling the yy.py module, but it will report an error when running the yy.py module in a direct script mode. I found a lot of information on the Internet and didn't figure it out. Later, we found that there is a suggestion to add a path to sys.path in the yy.py module, which is feasible but not elegant, and does not explain why the yy.py module will not report an error when it is called, while running yy.py directly will.
I specially checked a lot of information and carefully analyzed the situation of not reporting errors and actively executing Times errors when I was called. The first thing that comes to mind about the above feature is that the content of the variable _ _ name__, is different when it is called and when it is actively executed. Later, look up the data for the module call and found that the content of the variable _ _ package__, is also different when it is called and when it is actively executed.
Let's try to print the _ _ name__ and _ _ package__ variables to see the difference between being invoked and actively executing.
Modify the contents of yy.py module files
"" from .ABC import * from.. z.zz import * print (_ _ name__) print (_ _ package__) num3 = 888def f2 (): print (num2) print (num1)
The result of executing the test.py module:
Xx.y.yyxx.y12310hello10 123 888
Execute the result of yy.py directly (temporarily comment those 2 lines of code relative to the imported code, otherwise the error will result in not seeing the print print):
Pythonic solution of relative Import error report
Carefully observe the differences between what is printed when the yy.py module is imported and what is printed when it is actively executed. I came up with the most pythonic solution.
"" this module is the entry point for external references to xx subprojects "_ _ package__ = 'xx.y'from .ABC import * from.. z.zz import * num3 = 888def f2 (): print (num2) print (num1)
Directly modify the value of the _ _ package__ variable to the value when it is called, so that there is no error when you actively run the yy.py module, and no error is reported when it is called.
At this point, I believe that everyone on the "Python relative import error report how to solve" have a deeper understanding, might as well to the actual operation of it! 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: 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.