Recently, I encountered a problem in the process of carrying out a Flask project unresolved reference '包名'
. I have been looking for a solution for a long time on the Internet, and none of them can solve the problem in one step.
Later, I analyzed the problem and the solutions on the Internet, and found that most of the solutions on the Internet are for the py files in the same directory of the project.
1. Solution to module import problem in the same directory
As shown in the figure below: As
you can see, the problem shown in the figure above is that the Python从入门到实践
custom my_module.py
files in the directory cannot be imported normally.
The solution is: on the project 单击右键
-> Mark Directory as
-> Sources Root
as shown in the figure below.
After that the problem will be solved.
Then enter the settings: file
-> settings
Finally, unfold according to the figure below Build,Execution,Deployment -> Console -> Python Console -> Add source roots to PYTHONPATH
, check 3, select OK, and the problem is solved.
Of course, this is only a solution to the problem of module imports in the same directory.
2. The solution to the module import problem in different directories.
The problem I encountered is as follows. I want to import functions in other directories in the files processor
under the directory , and I have encountered problems. At this time, it will not work to use the above method without thinking, but the principle of the solution is still the above method. But we have to choose well , that is, choose your root path.AIDetector_pytorch.py
models
experimental
attempt_load
unresolved reference
Sources Root
Take my path as an example, my root path should not be F:\flaskProject2
, but should be F:\flaskProject2\back-end
.
The specific solution is as follows:
still follow the first method of the above solution, but the settings we choose Sources Root
have changed.
After completion, you can look at the project structure:
At this time, you can see that Source Folders is the back-end directory.
Then the problem is solved.