ChatGPT解决这个技术问题 Extra ChatGPT

Pycharm utils.py not getting syntax highlight

I have several django projects and several different files with name utils.py, however pycharm treats them as simple .txt files with no syntax highlighting or any other kind of parsing, how can I fix this?


C
CrazyCoder

Please see File | Settings (Preferences on Mac) | Editor | File Types.

Look for your file name mapped to the Text type or to the Auto-detect file type by content type.

Remove the incorrect mapping and it will fix the issue.

If you still can't find the wrong mapping in the IDE settings, locate the options/filetypes.xml file in the Configuration directory. Close the IDE and either edit this file to remove the incorrect mapping or delete this file to reset all the file types to the defaults.

https://i.stack.imgur.com/vS3Oy.png

You can also use Revert File Type Override / Override File Type file context menu actions.


the pattern there is CVS;SCCS;RCS;rcs;.DS_Store;.svn;.pyc;.pyo;*.pyc;*.pyo;.git;*.hprof;_svn;.hg;*.lib;*~;__pycache__;.bundle;*.rbc;*$py.class; however utils.py was registered under Text file pattern , removed it.
This is generalizable to other file types getting associated with the wrong extension as well. I had the same problem with one of my .js files getting associated with the plain text file type and this solution fixed my problem.
Ran into this accident today. Thought the filename extension would normally pickup the "Python Type" from the file-association choices, but I must of accidentally type the filename in the type Textfield.
PHPStorm also has this issue - adding this here to hopefully help future googlers.
Per @Nicolas's soon-to-be-deleted comment-as-answer: "In Pycharm 4.5, the File Type menu is in Pycharm | Preferences | Editor"
C
Chris

As CrazyCoder answered, my file also landed in an unintended association. In my case it was "Auto-detect file type by content". You can just try to add your file to the desired file type (e.g. Python) and PyCharm will automatically try to move the association.


How would the OP do this within PyCharm?
K
Koyasha

FYI, a quick action to reassociate a file's type will be available in the context menu of the Project tool window in IntelliJ IDEA 2021.2 (EAPs should be available publicly around May 2021).


a
avlnx

In my case I had to right click the file in question (a django migration) and select Mark as Python. I probably clicked Mark as Plain Text before by mistake.


S
Ssali Jonathan

I actually solved this by going to the Project section , and then clicking Override File Type by right clicking on the specific file. Then I selected the type of the file from the list of available file types.

That worked


G
Gulzar

For me, the file somehow forgot its type. This can be overridden by

Right click the file name --> Override File Type --> Select file type.

https://i.stack.imgur.com/yeytC.png