(Pro-test solution) PyCharm prompts unresolved reference (complete diagram) when importing packages from the directory

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
insert image description here
you can see, the problem shown in the figure above is that the Python从入门到实践custom my_module.pyfiles in the directory cannot be imported normally.

The solution is: on the project 单击右键-> Mark Directory as-> Sources Root
as shown in the figure below.
insert image description here
After that the problem will be solved.
insert image description here
Then enter the settings: file-> settings
insert image description here
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.
insert image description here
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 processorunder 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.pymodelsexperimentalattempt_loadunresolved reference
insert image description here
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.
insert image description here
After completion, you can look at the project structure:
insert image description here
At this time, you can see that Source Folders is the back-end directory.
insert image description here
Then the problem is solved.

Guess you like

Origin blog.csdn.net/m0_63007797/article/details/132122511