Import a directory into pycharm

I am trying to import an old project into pycharm for debugging. The directory structure looks like this:

top folder ---> one folder

top folder ---> folder two

The problem is that the programs in the subfolders use:

import top from top import module 

Pycharm returns an error: "There is no module named top"

How can i fix this?

+8
python pycharm
source share
2 answers

First of all, to do what Games said, you need to make sure that each folder representing the package is executed by placing the __init__.py file, which is an empty python file named exactly __init__.py , which tells the interpreter that the folder represents a python package.

The second thing to look for is that pycharm likes to complain about imported code if PyCharm doesn't know about this directory. Go to the configuration of the project interpreter and go to "Paths" and add links to paths that are not in the project or directly below the interpreter.

Another thing to add is to set the source root of the project code by right-clicking on the folder representing your root and clicking on Mark Directory As ...> Source Root.

+11
source share

I ran into the same problem, but this is not caused by the absence of the init .py file. The reason is that the project has two identical name modules, so PyCharm does not know how to import. It is strange that PyCharm only reports runtime error.

My project files:

 source root1 |-- moduleA |-- __init__.py |-- A.py source root2 |-- moduleA |-- __init__.py |-- B.py 

Actually, source root1 is my code, and source root2 is my test code.

therefore, the solution is to change the name of the test module.

  source root1 |-- moduleA |-- __init__.py |-- A.py source root2 |-- testmoduleA |-- __init__.py |-- B.py 
+1
source share

All Articles