n_urb
n_urb

Reputation: 21

Naming a new file in PyCharm

I just started learning python. Up until now, I primarly worked with jupyter notebooks, but now wanted to try out some IDEs. So I tried out PyCharm.

So I started creating some files in a project and started trying out the commands and stuff and there is one thing which just drives me crazy, cause I can not seem to find an answer.

Everytime I create a new python file and name it something like "simple_message", it just saves as a normal file and I can't run any code in it. As soon as I remove the underscore, the file convention changes to an actual python file and I can run the code written in it. So I thought it might be the underscore that makes trouble, so I tried creating other files with underscores but all of them worked. Can someone enlighten me, why does the file name "simple_message" not work as a python file?

Thank you!

From this To this

Upvotes: 1

Views: 745

Answers (3)

Reedinationer
Reedinationer

Reputation: 5774

For me I have to right click on a directory (folder) and select "New" → "Python File" to make a python file. If I go "New" → "File" it will create a text document. So long as you click the correct option you shouldn't have to worry about typing the '.py' or '.txt'

Alternatively I can right click and select "New" → "File" and then name it "simple_message.py" and it will save as a python file correctly, but I must include the .py using this method

Upvotes: 2

John R
John R

Reputation: 1508

You may need to right click in the text of your .py file and manually choose run from the menu.

Pycharm doesn’t automatically reindex when you create a new .py file, and so it probably just doesn’t know what you want it to run when you hit the green play button.

If you’re saying your python interpreter won’t run a .py file because it has an underscore in the name, that’s just not possible unless you have a modded interpreter or have done something really strange to your OS and I’m guessing you haven’t done either.

Update: it sounds like you don’t have the .py file extension on your file. This will not run for reasons that have nothing to do with Pycharm. Pycharm may “bridge the gap” for you automatically in some situations, but no reason to rely on that.

Upvotes: 1

Liko28s
Liko28s

Reputation: 141

Currently PyCharm context menu lets you choose the file type to create. If you choose "file" you MUST provide the extension .py If You choose "Python File" pycharm handles the extension for You.

Right click on the pycharm projects tab

Upvotes: 1

Related Questions