34

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

enter image description here

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

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.

The version is 3.0.2.

enter image description here

EDIT

Refactoring does not working.

enter image description here enter image description here enter image description here

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.

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.

prosseek
  • 182,215
  • 215
  • 566
  • 871

5 Answers5

79

Settings (Preferences on Mac) | Editor | File Types | Text

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

UPDATE 2021-02-28:

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

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


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

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

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.

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • 3
    For me, my file was under "auto detect file by ..." in the list. Took me a while to get it. – Guillaume Feb 28 '21 at 01:39
  • @Guillaume Yes, it's a new entry since 2020.3 where it is often preselected by default instead of the "Text" entry. See this answer with screenshots: https://stackoverflow.com/questions/65700794/webstorm-file-association-system-does-not-recognize-a-file – LazyOne Feb 28 '21 at 02:16
11

Go in the Settings -->Editor --> File Types --> Text --> Registered Patterns (lower list box in the dialog) --> Remove the file name that has been added (Should be the file name you accidentally created as text file) by clicking the "-" right hand side. Apply and you should have this speeding ticket off your record.

enter image description here

  1. Select File Types
  2. Scroll down until you see [text]
  3. select [text]
  4. scroll down to the bottom, you should see your file that Pycharm thinks is text
  5. hit the [-] option to remove the text association and hit OK
  6. When you go back to Project view you should see the file associated with the extension you gave it.
Mark Parris
  • 193
  • 9
  • My file was not listed in "Text" but in "Auto-detect file type by content". That menu can also be found in the list of "Recognized File Types" (even if I would not regard it as a "file type" on its own, which I find confusing.) – Stefan Apr 07 '21 at 06:22
3

I cannot say for sure what mistake you've made, however, this is the way to properly rename a file:

enter image description here enter image description here enter image description here

If the above fails for whatever reason, try the following, otherwise this is bug that you need to report:

enter image description here

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • @Games Brainiac : Refactoring does not solve this issue on my Mac and PyCharm. I updated my post. – prosseek Jan 21 '14 at 17:01
  • @prosseek Aha! I found out what the problem is. This is intelliJ's error, I can't even _see_ the file here. The best way to solve this is just just copy all your stuff, as in your `src` folder and your `test` folder, and then just paste it into a new directory, and then _repoen_ pycharm in that directory. Your `.idea` folder will be regenerated, and thus this problem will be solved. – Games Brainiac Jan 21 '14 at 17:34
  • Actually better idea, just delete your `.idea` directory, and restart pycharm in that directory. – Games Brainiac Jan 21 '14 at 17:36
  • @Games Brainiac: I didn't do exactly what you proposed; I also removed .idea folder, but it didn't work. I guess it's cache problem. I added my own answer. – prosseek Jan 21 '14 at 18:12
  • @prosseek Sorry, I couldn't help. I mean its hard to debug these kinds of problems when you're not next to the person. – Games Brainiac Jan 21 '14 at 18:15
1

I think I had the same problem and you probably have to look in Settings -->Editor --> File Types --> Auto-detect file type by content

This is where it can go wrong

This is where to possibly fix it

Tarek
  • 11
  • 2
0

I deleted the the .idea folder, I cleared the cache using the File-> Invalidate Chache menu option, I deleted the __pycache__ folders. I also tried to check out a new version into a new folder as well as copy and pasted the relevant files into a new folder.

None of the solutions worked so it seems that if you declare the file wrongly it is globally registered in all projects as this wrong file type.

I just reinstalled PyCharm deleting all settings. However, if I would have had more custom plugs-ins or settings, I would have started playing with the global pycharm settings rather than the project settings. One option might be deleting the C:\User\<username>\.PyCharm<version> folder which should contain the config, this resetting your PyCharm to factory settings. Obviously do not want to replicate the error now.

My short version: The file type is not saved in the project folder so if you do not find a menu option to change it, go and play with the global settings which are saved in the in the folder mentioned above.

Coolkau
  • 128
  • 1
  • 8