简体   繁体   中英

PyCharm can't find import in same folder

在此输入图像描述

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

在此输入图像描述

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

What causes this behaviour and how can I fix it?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM