简体   繁体   English

Pycharm:将文件夹标记为“sources root”对于子文件夹不是递归的

[英]Pycharm: Marking a folder as 'sources root' is not recursive for subfolders

I've followed pycharm documentation to set up the IDE to resolve imports.我已按照pycharm 文档设置 IDE 来解析导入。 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.但是,似乎每个包含 *.py 文件的文件夹都需要明确添加为“sources root”,以便 IDE 解析所有引用。 Can this be done recursively from a root folder?这可以从根文件夹递归完成吗?

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 以解析所有代码库引用的正确方法,还是我没有正确设置项目结构?

I have already followed other methods for resolving references in the IDE here and here but to no avail.我已经按照其他方法在 IDE herehere中解析引用,但无济于事。 It seems that the IDE will only resolve them if I manually add each folder as a 'sources root'.似乎 IDE 只有在我手动将每个文件夹添加为“源根目录”时才会解决它们。 Without the recursive functionality, large codebases will be laborious when setting up the IDE!如果没有递归功能,大型代码库在设置 IDE 时会很费力!

If you have not used __init__.py , you should add it in each sub-directory to mark it as a package . 如果您还没有使用__init__.py ,则应将其添加到每个子目录中以将其标记为 By adding it, Python will treat the directories as containing packages making you modules visible to other directories and therefore able to be imported. 通过添加它,Python会将目录视为包含包,使您的模块对其他目录可见,因此可以导入。

It's possible to select all folders in root a at once by going to Project Structure in Preferences then selecting all and clicking the blue Sources icon at the top.通过转到“首选项”中的“项目结构”然后全选并单击顶部的蓝色“源”图标,可以一次访问 select 根目录中的所有文件夹。 This was at least doable with PyCharm CE 2021.2 running on Big Sur.这至少对于在 Big Sur 上运行的 PyCharm CE 2021.2 是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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