PyCharm can't find import in same folder

Viewed 12859

enter image description here

I'm using PyCharm and I'm importing some constants from another python file in the same directory. The import works at runtime, but I get this annoying red underline on the import statement and also every time I use a constant from the file.

Here's the file hierarchy

enter image description here

(Please ignore the red underlining on the folders, they are unrelated to this)

What causes this behaviour and how can I fix it?

3 Answers

It is better if you replace that with:

from pca_mixtures.constants import *

The issue is because of how PyCharm sets PYTHONPATH to the root of your project.

While you can add directories as Sources, I would not recommended. That will make your project fragile.

You can right click pca_mixtures and then mark the directory as Sources Root, this should solve your problem. Good luck!

You can use from .constants import *. Always check if you're able to control+click/ cmd+click to your imported file

Related