Pycharm中的文件夹类型和Source roots的问题

前置条件:导入同一个包下的模块

问题:Pycharm中,配置好解释器之后,不指定包名的话,会报错.
即指定了包名,就不会有红色波浪
未配置解释器时,不指定包名不会报错

解决:
模块名前面加个点
选中包,右键 -->marke as,将包设置为source content path;
或者在preference中的project设置也行

原因:
在Pycharm中,有个概念叫上下文的根(content root),它包含了当前工作目录到所有文件的集合,来组织子目录的架构
而这个上下文的根(content root)是由几个部分共同协作的,包括Regular content roots,Source roots,Resource roots,Excluded roots,Template roots
其中,Source roots包含实际的源文件和资源。PyCharm使用Source roots作为解析导入(import)的起点。PyCharm可以解析、检查、索引和编译这些根的内容。
而默认情况下,只有项目的根目录是设置为Regular content roots,而没有配置包为Source roots.要导入的模块不在能找到实际文件的上下文中,所以会报错
也就是说,指定了包名或者点号,就指定了import的起点,否则需要配置Source roots

相关概念

  • Content Root
    • In PyCharm, content is a collection of files with which you are currently working, possibly organized in a hierarchy of subfolders. The folder which is the highest in this hierarchy is called the content root folder or content root (shown as ) for short. A project has at least one content root folder which by default is the project folder itself.
    • Having several content roots enables you to work with files from several directories that do not have a common immediate parent. This is helpful when you use static contents, for example, icons. You can just save them all in a folder and then specify this folder as an extra content root in several projects.
    • https://www.jetbrains.com/help/pycharm/2018.3/content-root.html?utm_campaign=PY&utm_content=2018.3&utm_medium=link&utm_source=product

Content Root分配到以下类别:
By default, all the files in a content root folder are involved in indexing, searching, parsing, code completion, etc. To change this status, folders within a content root can be assigned to the following categories:

  • Regular content roots, created as described in the section Configuring Content Roots.
    A content root is a folder that contains the files that make up your project.

  • Source roots (or source folders; shown as .

    • These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.
    • The files under the source roots are interpreted according to their type. PyCharm can parse, inspect, index, and compile the contents of these roots.
  • Resource roots (or resource folders; shown as ).
    These roots are intended for resource files in your application (images, Style Sheets, etc.) By assigning a folder to this category, you tell PyCharm that files in it and in its subfolders can be referenced relative to this folder instead of specifying full paths to them.

  • Excluded roots (shown as ) are ones that PyCharm “almost ignores”.

    • These roots contain files and folders ignored by PyCharm when indexing, searching, parsing, watching etc.
    • Excluded roots are not visible to PyCharm. Usually, one would like to exclude temporary build folders, generated output, logs, and other project output. Excluding the unnecessary paths is a good way to significantly improve performance.
  • Template roots marked as contain templates for the various web projects.

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Pycharm创建文件夹的方法有多种。以下是两种常用的方法: 方法一: 1. 在Pycharm打开你要创建文件夹的项目。 2. 在项目目录视图,右键点击你想要创建文件夹的位置,并选择"New" -> "Directory"。 3. 输入文件夹的名称,并按下Enter键,即可创建文件夹。 方法二: 1. 在Pycharm打开你要创建文件夹的项目。 2. 在顶部菜单栏选择"File" -> "New" -> "Directory"。 3. 输入文件夹的名称,并按下Enter键,即可创建文件夹。 这些方法可以帮助你在Pycharm轻松创建文件夹。同时,你也可以通过命令行方式在Pycharm创建文件夹,具体操作是在项目所在文件夹位置打开命令行,然后使用命令"mkdir 文件夹名称"来创建文件夹。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python新建一个文件目录,python怎么创建新文件](https://blog.csdn.net/wenangou/article/details/126931939)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Pycharm创建文件和添加setting的三种方法](https://blog.csdn.net/wly048/article/details/126851553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菲儿啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值