1

I have the following folder structure:

project/
├── main.py
├── package/
    ├── __init__.py
    └── module.py
    └── test.py

In module.py, I have a function func1.

In test.py, I have the following code:

 from module import func1

PyCharm shows me red lines under "module" and under "func1", claiming "Unresolved reference module" and "Unresolved reference func1". However, when I run the code with Python3, it runs fine.

How can I avoid these red lines?

Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • 2
    You should tell PyCharm where are the different "root" directories of your project. On left side, where you have the files, go to your project directory, select, and right click, then "mark directory as...", and then "source root". Note: you may have multiple source root (test, documentation, etc.) – Giacomo Catenazzi Oct 19 '20 at 15:04
  • @GiacomoCatenazzi works like a (py)charm. Thanks – Erel Segal-Halevi Oct 19 '20 at 15:48
  • Another more general way (not specific to PyCharm) is to properly package your library code. Then you install it in a virtual environment. With this you can import your library from anywhere. I have answered [this question](https://stackoverflow.com/questions/64107888/importing-module-from-a-directory-above-current-working-directory) with *one* way to do it. – darcamo Oct 19 '20 at 16:31

1 Answers1

0

Try invalidating your caches and restarting IDEA (File > Invalidate Cache). Wait for IDEA to re-index your project (Status indicator on lower right status bar). The caches will sometimes become corrupted. This is most likely to occur upon reorganizing a project as you mentioned you did.