PyCharm 在同一文件夹中找不到导入 [英] PyCharm can't find import in same folder
问题描述
我正在使用 PyCharm 并且我正在从同一目录中的另一个 python 文件导入一些常量.导入在运行时工作,但我在导入语句上以及每次使用文件中的常量时都会得到这个恼人的红色下划线.
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.
这是文件层次结构
(请忽略文件夹上的红色下划线,它们与此无关)
是什么导致了这种行为,我该如何解决?
What causes this behaviour and how can I fix it?
推荐答案
最好将其替换为:
from pca_mixtures.constants import *
问题在于 PyCharm 如何将 PYTHONPATH 设置为项目的根目录.
The issue is because of how PyCharm sets PYTHONPATH to the root of your project.
虽然您可以将目录添加为 Sources
,但我不建议这样做.这将使您的项目变得脆弱.
While you can add directories as Sources
, I would not recommended. That will make your project fragile.
这篇关于PyCharm 在同一文件夹中找不到导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!