Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

python - Change file type in PyCharm

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...