PyCharm not adding sources root to `sys.path`(PyCharm 未将源根目录添加到“sys.path)
问题描述
在 PyCharm 的运行配置中,我已经检查了两个:
In a run configuration in PyCharm I've check both:
将内容根添加到 PYTHONPATH
将源根目录添加到 PYTHONPATH
但我无法导入相对于我的源根目录的文件.这是 PyCharm 的新安装(系统重建)和现有项目 &运行之前有效的配置现在失败.
But I can't import files relative to my sources root. This is a new installation of PyCharm (system rebuild) and an existing project & run configurations which worked before now fail.
在 Python 控制台中,我打印 sys.path
的值并查看我的内容根目录,但未列出源根目录.我仔细检查了项目结构,发现目录 src
被适当地列为源文件夹.
In the Python Console I print the value of sys.path
and see my content root, but not the source root listed. I double check the Project Structure and see the directory src
is listed as a Source Folder appropriately.
当我从源根目录在控制台上运行 python 时,所有导入都按预期工作.
When I run python at the console from the source root directory all imports work as expected.
知道我在 PyCharm 中可能缺少什么配置来导致这种情况吗?还是我的预期不正确?
Any idea what configuration I might be missing in PyCharm to cause this? Or perhaps my expectation is incorrect?
推荐答案
我在这里找到了解决方案:
I found the solution here:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000164624-SOLVED-pycharm-seem-to-fail-to-add-to-PYTHONPATH
只需删除.idea
项目目录,重新创建项目即可解决问题.
Just delete the .idea
project directory and re-create the project to fix the problem.
这篇关于PyCharm 未将源根目录添加到“sys.path"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!