Pycharm:将文件夹标记为“源根目录"对于子文件夹不是递归的 [英] Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
问题描述
我已关注 pycharm文档 来设置 IDE 以解决导入问题.但是,似乎每个包含 *.py 文件的文件夹都需要明确添加为源根目录",以便 IDE 解析所有引用.这可以从根文件夹递归完成吗?
I've followed pycharm documentation to set up the IDE to resolve imports. However it seems that each folder containing *.py files needs to be explicitly added as 'sources root' in order for the IDE to resolve all references. Can this be done recursively from a root folder?
这是让 IDE 解析所有代码库引用的正确方法,还是我没有正确设置我的项目结构?
Is this the correct way to get the IDE to resolve all codebase references, or have I not set up my project structure correctly?
我已经按照其他方法在 IDE 中解析引用 此处 和此处 但无济于事.如果我手动将每个文件夹添加为源根目录",IDE 似乎只会解决它们.如果没有递归功能,大型代码库在设置 IDE 时会很费力!
I have already followed other methods for resolving references in the IDE here and here but to no avail. It seems that the IDE will only resolve them if I manually add each folder as a 'sources root'. Without the recursive functionality, large codebases will be laborious when setting up the IDE!
推荐答案
如果你还没有使用过__init__.py
,你应该在每个子目录中添加它,将其标记为包.通过添加它,Python 会将目录视为包含包,使您的模块对其他目录可见,因此可以导入.
If you have not used __init__.py
, you should add it in each sub-directory to mark it as a package. By adding it, Python will treat the directories as containing packages making you modules visible to other directories and therefore able to be imported.
这篇关于Pycharm:将文件夹标记为“源根目录"对于子文件夹不是递归的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!