简体   繁体   English

PyCharm 无法识别 Python 文件

[英]PyCharm not recognizing Python files

PyCharm is no longer recognizing Python files. PyCharm 不再识别 Python 文件。 The interpreter path is correctly set.解释器路径设置正确。

截屏

Please check File |请检查File | Settings ( Preferences on macOS) | Settings (macOS 上的Preferences )| Editor | Editor | File Types , ensure that file name or extension is not listed in Text files. File Types ,确保文本文件中未列出文件名或扩展名。

To fix the problem remove it from the Text files and double check that .py extension is associated with Python files.要解决此问题,请将其从文本文件中删除,并仔细检查.py扩展名是否与Python文件相关联。

文本

I had a similar problem where certain .py files were showing up as regular text files after completion, thus rendering the code without syntax coloring, tab completion features, etc. Through using this post as a starting point for debugging the issue, I found the following:我有一个类似的问题,某些.py文件在完成后显示为常规文本文件,因此呈现的代码没有语法着色、制表符完成功能等。通过使用这篇文章作为调试问题的起点,我发现下列的:

  1. (from OSX): PyCharm → Preferences → IDE Settings → File Types (来自 OSX):PyCharm → 首选项 → IDE 设置 → 文件类型
  2. Select the affected File Type from the list on the top half of this dialog box, Recognized File Types (in my case, Text Files)从该对话框上半部分的列表中选择受影响的文件类型, Recognized File Types (在我的情况下,文本文件)
  3. Listed in the second half of the dialog box, Registered Patterns were the names of the files that I was having naming/syntax issues with.在对话框的后半部分列出的Registered Patterns是我遇到命名/语法问题的文件的名称。 I clicked on each of those and then clicked on the - for each in turn to remove them from the Registered Patterns list.我单击了其中的每一个,然后依次单击了-以将它们从Registered Patterns列表中删除。
  4. Click Apply单击Apply
  5. Sigh of relief when the syntax highlighting returns and the icon changes back to that of a python file.当语法高亮返回并且图标变回 python 文件的图标时,松了一口气。

I accidently made a text file myfilename , renamed it to the myfilename.py version but it stayed with text file formatting even after the extension change.我不小心制作了一个文本文件myfilename ,将其重命名为myfilename.py版本,但即使在扩展名更改后它仍保留文本文件格式。

Here's how I fixed it for For PyCharm 2017.2 for Windows.这是我为 Windows 的 PyCharm 2017.2 修复它的方法。

  1. Go to File > Settings > Editor > File Types > Text转到File > Settings > Editor > File Types > Text
  2. Under Registered Patterns , I found the new myfilename.py in the list.Registered Patterns下,我在列表中找到了新的myfilename.py
  3. Remove it from the list with the - button使用-按钮将其从列表中删除
  4. Click Ok点击Ok

I had a similar problem and none of the answers already submitted helped to resolve it.我有一个类似的问题,已经提交的答案都没有帮助解决它。

I eventually discovered that my affected filename was listed in the Auto-detect file type by content section in Preferences->Editor->File Types.我最终发现我受影响的文件名列在首选项->编辑器->文件类型Auto-detect file type by content部分中。 Removing the filename from there and apply the changes resolved my issue immediately.从那里删除文件名并应用更改立即解决了我的问题。

PyCharm 文件类型首选项窗口

Got it to work finally!终于可以上班了!

I had this same problem.我有同样的问题。 I tried removing the pycharm caches in the ~/Library folders to no avail.我尝试删除 ~/Library 文件夹中的 pycharm 缓存无济于事。 Kept saying in the log that "Some skeletons failed to generate..."一直在日志中说“一些骷髅无法生成……”

So, here is what worked.所以,这就是有效的。

  1. Go into preferences进入偏好
  2. In the project settings click project interpreters and then Configure Interpreters在项目设置中单击项目解释器,然后单击配置解释器
  3. Delete the existing Interpreters (using ' - ' and bottom) and then click OK at the bottom删除现有的解释器(使用' - '和底部)然后单击底部的确定
  4. If you have a project loaded it will say "You have no interpreter, configure one now. You can click that or go back into preferences->Project Interpreter->Configure interpreters如果您加载了一个项目,它会说“您没有解释器,现在配置一个。您可以单击它或返回首选项->项目解释器->配置解释器
  5. Click the + to add a new interpreter.单击+添加新的解释器。 If you are using the os x built-in python, you can select the version you want from the list.如果您使用的是 os x 内置的 python,您可以从列表中选择您想要的版本。
  6. Click Ok again, wait a minute or two for rebuilding indexes and viola it works (for me at least)再次单击确定,等待一两分钟重建索引,中提琴它可以工作(至少对我来说)

The most common issue is that you have .py in your txt file types最常见的问题是您的 txt 文件类型中有 .py

The other one that happens every now and again, is that you have associated the actual file name to the txt file type时常发生的另一件事是,您已将实际文件名与 txt 文件类型相关联

Solution remains the same解决方案保持不变

navigate to file->settings->file types->text files and look for .py or the "file name" that is being formatted as text导航到文件->设置->文件类型->文本文件并查找 .py 或被格式化为文本的“文件名”

I faced a similar issue on PyCharm 2021.1.我在 PyCharm 2021.1 上遇到了类似的问题。 As mentioned in the other solutions, the .py file that was being rendered as a text file was not present in the following menu:如其他解决方案中所述,正在呈现为文本文件的.py文件不存在于以下菜单中:

File | Settings | Editor | File Types | Recognized File Types | Text | File name patterns

The following worked for me instead:以下对我有用:

Select the file in the editor | File | File properties | Associate with File Type...

文件类型关联

What worked for me was to, right-click on the file, then select "Override File Type".对我有用的是,右键单击文件,然后选择“覆盖文件类型”。

文件覆盖

From the drop down list, select the language you want.从下拉列表中,选择所需的语言。

选择语言

(Can not show images as my rep is low, but they are clickable.) (无法显示图像,因为我的代表很低,但它们是可点击的。)

To revive an old conversation here, none of the above answers worked for the newer version of PyCharm due to updates.为了在这里恢复旧对话,由于更新,上述答案都不适用于更新版本的 PyCharm。 Upon creating new .py files they were being detected as .txt files and thus not able to run the files, as many others experienced above.在创建新的 .py 文件时,它们被检测为 .txt 文件,因此无法运行这些文件,正如上面许多其他人所经历的那样。 I did not get any errors, there was just not any ability to run the file even though it had a .py extension because it was not being detected as a Python file.我没有收到任何错误,即使它具有 .py 扩展名,也无法运行该文件,因为它没有被检测为 Python 文件。

PyCharm Version: 2020.1.4 Build: 201.8743.11 PyCharm 版本:2020.1.4 构建:201.8743.11

Here's what works instead now:这是现在起作用的方法:

File > Manage IDE Settings > Restore Default Settings文件 > 管理 IDE 设置 > 恢复默认设置

The only problem with this is if you've added a lot of custom settings you'll have to go back in and add them again.唯一的问题是,如果您添加了许多自定义设置,则必须返回并再次添加它们。

I had a text file that was not recognized by Pycharm as .py after I changed its ending on Windows.在我在 Windows 上更改其结尾后,我有一个文本文件未被 Pycharm 识别为 .py。 The solution was to right click on it in the Project menu (the one on the left that details all files in the project).解决方案是在项目菜单中右键单击它(左侧详细说明项目中所有文件的菜单)。 One of the options was Override File Type .选项之一是Override File Type Changing that to Python was successful.将其更改为 Python 是成功的。

在 Editor->File Types->Recognized File Types 下有一个名为“File type auto-detected by file content”的部分,列出了我的文件,从那里删除后,它被格式化为 python

I had the same situation here.我在这里也有同样的情况。 Ubuntu 18 - PyCharm2022.1.1 (Community Edition). Ubuntu 18 - PyCharm2022.1.1(社区版)。

Solved as mention above with the right click above the file type.如上所述通过在文件类型上方单击右键解决。

  1. Find the the file to change in the Project tree (Alt + 1).在项目树中找到要更改的文件 (Alt + 1)。
  2. Then, click in override file type and choose the extension you need.然后,单击覆盖文件类型并选择您需要的扩展名。
  3. Select apply and it is done.选择应用就完成了。

Note : Remember to write the correct extension for Python which is .py in most cases.注意请记住为 Python 编写正确的扩展名,在大多数情况下为 .py。 You can check the extension types in File, Settings, Editor, Code Style, File Types.您可以在文件、设置、编辑器、代码样式、文件类型中检查扩展类型。

I hope it help, regards.我希望它有所帮助,问候。

Had a similar issue after changing the project name and the above didn't help (it kept using an old interpreter).在更改项目名称后遇到了类似的问题,以上没有帮助(它一直使用旧的解释器)。 What did help was the following:有什么帮助如下:

  1. In the project folder go to the .idea folder在项目文件夹中转到 .idea 文件夹
  2. In the workspace.xml find the interpreter that appears in the error message.在 workspace.xml 中找到错误消息中出现的解释器。 Can be found by looking for: option name="SDK_HOME" value="C:\Users\yourInterpreterFolder\python.exe"可以通过查找找到: option name="SDK_HOME" value="C:\Users\yourInterpreterFolder\python.exe"
  3. Replace the value with the path to your interpreter.将值替换为您的解释器的路径。

Keep coding happily :)继续愉快地编码:)

ps my error message was of the following form: ps我的错误信息是以下形式:

Error running ...: Cannot run program "...\python.exe" (in directory "C:\Users\pathToProject"): CreateProcess error=2, The system cannot find the file specified运行时出错...:无法运行程序“...\python.exe”(在目录“C:\Users\pathToProject”中):CreateProcess error=2,系统找不到指定的文件

I had a similar problem, when I upgraded from using the old version of Pycharm to the latest version.当我从使用旧版本的 Pycharm 升级到最新版本时,我遇到了类似的问题。 My *.ipynb files were treated as *.py files.我的 *.ipynb 文件被视为 *.py 文件。

I had a similar situation but the file was not under "Text" in Settings -> Editor -> File Types, it was under "Auto-detect based on file content".我有类似的情况,但该文件不在“设置”->“编辑器”->“文件类型”中的“文本”下,而是在“基于文件内容的自动检测”下。 Once I deleted it from there, things worked fine.一旦我从那里删除它,一切正常。

I've tried many methods.我试过很多方法。 My pycharm version is 2020. I've checked the settings and file types, and both are OK.我的pycharm版本是2020。我检查了设置和文件类型,都可以。 Finally, I got my solution from the link below.最后,我从下面的链接中得到了我的解决方案。

I right-click the XXX_XXX.py (error file) file, click overwrite file type, and then choose python.我右键单击XXX_XXX.py(错误文件)文件,单击覆盖文件类型,然后选择python。 Problem solved.问题解决了。

https://intellij-support.jetbrains.com/hc/en-us/community/posts/207739289-test-models-py-not-recognized-as-a-python-file https://intellij-support.jetbrains.com/hc/en-us/community/posts/207739289-test-models-py-not-recognized-as-a-python-file

右键单击urls.py -> 覆盖文件类型 -> Python

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

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