本文介绍了在 PyCharm 中更改文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个文本文件,并将其重命名为 testTreeGen.py.

I created a text file, and renamed it into testTreeGen.py.

问题是 PyCharm 没有将其检测为 python 源,因此我无法执行它.

The problem is that PyCharm does not detect it as a python source so that I can't execute it.

我如何教 PyCharm 这是 python 脚本?我尝试删除文件并重新创建它,并创建了另一个 python 文件并重命名了它;所有最终都是作为文本文件而不是 Python 脚本的文件.

How can I teach PyCharm that this is python script?I tried remove the file and recreated it, and created another python file and renamed it; all ends up being the file as text file not Python script.

版本是 3.0.2.

重构不起作用.

即使在我将其重命名为 python 脚本之后,文件属性也会恢复为文本(我在第一次重构之前创建的原始文件属性).

The file property comes back to text (the original file property that I created before the first refactoring) even after I renamed it as python script.

奇怪的是,无论我在哪里创建 testTreeGen.py,PyCharm 都会将其创建为文本文件,就好像它记得文件名应该是文本文件一样.除了 testTreeGen.py 之外,我可以创建任何 python 脚本.

The weird thing is that no matter where I create the testTreeGen.py, PyCharm creates it as a text file as if it remembers the name of the file should be text file. I can create python script anything but the testTreeGen.py.

推荐答案

设置(Mac 上的首选项) |编辑 |文件类型 |文字

检查那里的模式(底部列表)——你必须有 testTreeGen 或类似的模式.只需删除它.

Check patterns there (bottom list) -- you must have had testTreeGen or similar pattern. Just remove it.

更新 2021-02-28:

自 2020.3 版本起,您还应该在 Auto-detect file type by content 文件类型下进行检查.

Since 2020.3 version you should also check under Auto-detect file type by content file type.

在此答案中查看更多/带有屏幕截图:WebStorm 文件关联系统无法识别文件

See a bit more / with screenshots in this answer: WebStorm File association system does not recognize a file

这通常发生在创建新文件时,而不是使用特定的文件模板,而是使用 New |File 并忘记输入文件扩展名(此处需要,而不是自动添加文件扩展名的特定文件模板)-这是允许创建具有任何扩展名的任何文件(例如 .htaccess 或只是 README).

This usually happens when creating new file and instead of using specific file template you use New | File and forgetting to enter file extension (which is required here as opposed to specific file template which adds file extension automatically) -- this is to allow to create ANY file with any extension (e.g. .htaccess or just README).

由于此类文件(在您的情况下为 testTreeGen)没有扩展名,IDE 不知道如何处理它并将其视为纯文本(虽然不是自动的 - 它要求您选择正确的预选了文本文件"类型的文件类型,但绝大多数用户只是按 Enter 而不是阅读屏幕上的内容-放心,我是专业人士,我知道自己在做什么").

Since such file (testTreeGen in your case) does not have extension, IDE does not know how to treat it and treats it as plain text (not automatically though -- it asking you to choose correct file type with "text file" type preselected, but vast majority of users just hitting enter instead of reading what that screen says -- "relax, I'm a professional and I know what I'm doing").

现在,由于 testTreeGen 模式在那里,并且通用 *.rb*.py 文件更大/更精确无论文件扩展名如何,都被视为文本文件 - 重命名无济于事.

Now, since the testTreeGen pattern is there and it is bigger/more precise that generic *.rb or *.py, file is treated as Text file regardless of file extension -- renaming will not help.

结论/建议——多注意确认对话框.

Conclusion / advice -- pay a bit more attention to confirmation dialogs.

这篇关于在 PyCharm 中更改文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:19