102

PyCharm is no longer recognizing Python files. The interpreter path is correctly set.

Screen shot

belteshazzar
  • 2,163
  • 2
  • 21
  • 30
  • 21
    It's not off-topic: `software tools commonly used by programmers` is listed in the FAQ section `What kind of questions can I ask here?`, it's exactly this case. Why voting to close it? – CrazyCoder Jul 27 '12 at 12:35

16 Answers16

163

Please check File | Settings (Preferences on macOS) | Editor | File Types, ensure that file name or extension is not listed in Text files.

To fix the problem remove it from the Text files and double check that .py extension is associated with Python files.

Text

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 6
    I had the same problem but the file name was added to the text file extension – brooc May 10 '15 at 07:56
  • 3
    Any idea why the file is added to that list of text files in the first place? – user1211030 Jan 04 '18 at 08:35
  • @user1211030 probably because of the misclick in the context menu, there is an option to open file as text. – CrazyCoder Jan 04 '18 at 17:54
  • 1
    For me this was hidden in `Setting | Editor | File Types`. (2017.3.2) – Bram Vanroy Mar 20 '18 at 12:31
  • @user1211030 I had this happen when I used `New > File` instead of `New > Python File`. It created an text file with no extension. When I manually added a `.py` extension to the filename, PyCharm automatically created a rule to treat that filename as a text file. – stevepastelan Aug 13 '18 at 17:00
81

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:

  1. (from OSX): PyCharm → Preferences → IDE Settings → File Types
  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)
  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. I clicked on each of those and then clicked on the - for each in turn to remove them from the Registered Patterns list.
  4. Click Apply
  5. Sigh of relief when the syntax highlighting returns and the icon changes back to that of a python file.
Marko
  • 20,385
  • 13
  • 48
  • 64
Vanessa Wallis
  • 931
  • 6
  • 5
  • Fix worked for me PyCharm 3.4.1 for Linux. views.py got accidentally added to the list of TextFiles... Frustrating few days thanks. – Ryan Currah Sep 29 '14 at 15:30
  • 1
    FYI I found my file in the "Auto-detect file type by contents" file type; which, despite the name, was somehow preventing the file from being recognized detected as a Python file. – kranberry Mar 12 '21 at 17:05
52

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. Removing the filename from there and apply the changes resolved my issue immediately.

PyCharm File types preferences window

Chris
  • 4,662
  • 2
  • 19
  • 27
49

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.

Here's how I fixed it for For PyCharm 2017.2 for Windows.

  1. Go to File > Settings > Editor > File Types > Text
  2. Under Registered Patterns, I found the new myfilename.py in the list.
  3. Remove it from the list with the - button
  4. Click Ok
Wes
  • 866
  • 10
  • 19
  • Did the same, had the same problem, but file extension .py is not in the "Text" list and PyCharm still does not recognize it, even after a restart (version 2018.2) – jpp1 Jul 25 '18 at 11:03
10

Got it to work finally!

I had this same problem. I tried removing the pycharm caches in the ~/Library folders to no avail. 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.
  6. Click Ok again, wait a minute or two for rebuilding indexes and viola it works (for me at least)
hlhicks
  • 101
  • 3
9

The most common issue is that you have .py in your txt file types

The other one that happens every now and again, is that you have associated the actual file name to the txt file type

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

Illegal Operator
  • 656
  • 6
  • 14
  • This might seem to be a really rare case, but it happened for me just now and your answer helped. I ended up in this state through "normal behavior" through the following steps: 1. I right-click on a folder on the file tree and selected "new file" instead of "new python file". 2. I assumed that the .py would be added so I typed the name of my python file (suppose it's `foo`) and hit enter. – T Tse Sep 22 '19 at 06:24
  • 3. A dialog box popped up prompting me to associate the file with a file type, and when I'm just simply adding a file I glossed over the text and just changed the input from `foo` to `foo.py` thinking that I need a file name. 4. I renamed the file to `foo.py` thinking it didn't work or something. 5. Now the file is named `foo.py` and I have a rule saying that `foo.py` is a text file. – T Tse Sep 22 '19 at 06:25
7

I faced a similar issue on 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:

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...

File type association

Dharman
  • 30,962
  • 25
  • 85
  • 135
Swaroop
  • 1,219
  • 3
  • 16
  • 32
6

What worked for me was to, right-click on the file, then select "Override File Type".

file_override

From the drop down list, select the language you want.

chose_language

(Can not show images as my rep is low, but they are clickable.)

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Franco
  • 61
  • 1
  • 4
1

To revive an old conversation here, none of the above answers worked for the newer version of PyCharm due to updates. 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. 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.

PyCharm Version: 2020.1.4 Build: 201.8743.11

Here's what works instead now:

File > Manage IDE Settings > Restore Default Settings

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.

ksaint
  • 11
  • 4
1

I had a text file that was not recognized by Pycharm as .py after I changed its ending on Windows. 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. Changing that to Python was successful.

Toma
  • 163
  • 1
  • 10
1

There is a section called "File type auto-detected by file content" under Editor->File Types->Recognized File Types, my file was listed, after removing from there, it got formatted to python

1

I had the same situation here. Ubuntu 18 - PyCharm2022.1.1 (Community Edition).

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).
  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. You can check the extension types in File, Settings, Editor, Code Style, File Types.

I hope it help, regards.

Cyberfete
  • 11
  • 3
0

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
  2. In the workspace.xml find the interpreter that appears in the error message. Can be found by looking for: option name="SDK_HOME" value="C:\Users\yourInterpreterFolder\python.exe"
  3. Replace the value with the path to your interpreter.

Keep coding happily :)

p.s. my error message was of the following form:

Error running ...: Cannot run program "...\python.exe" (in directory "C:\Users\pathToProject"): CreateProcess error=2, The system cannot find the file specified

Anon
  • 619
  • 1
  • 9
  • 18
0

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.

FreeMind
  • 213
  • 3
  • 20
0

I've tried many methods. My pycharm version is 2020. I've checked the settings and file types, and both are OK. 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. Problem solved.

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

Xuemin Chi
  • 21
  • 3
-1

right click on urls.py -> Override File Type -> Python

grow
  • 11
  • 1
    Welcome to Stack Overflow! Your answer might be more helpful to others if you explained how/why it works, and/or why this solution is better than the existing answers that have already been here for a while :) – Alex Waygood Sep 16 '21 at 13:29
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 16 '21 at 13:43