Save Python scripts with .py extension automatically - python

In Python IDE, while we save the script, it will prompt the save Dialog. If we specify the filename as "Test". Then file will be saved without extension as "Test" and not "Test.py".
Is it possible to save the script with .py extension automatically (as Test.py)?

Unfortunately, IDLE doesn't and can't add the .py extension automatically; you will just have to get into the habit of adding it yourself, or use another IDE like Eclipse or Komodo that will do it for you.

There is a bug report for IDLE about this: http://bugs.python.org/issue10364
If you want, you can edit IDLE itself to do this for you. A good place to start is save_as in IOBinding.py.

Related

How to execute .py file with double-click

I just uninstalled and reinstalled python on my Windows machine. Before I uninstalled my previous version I was able to just double-click on a python script and it would open the command prompt, run the script, and close automatically. After re-installing with the newest version (3.9), I am no longer able to execute the script like that with a double-click.
Clearly I had done something special last time to set that up for myself, but I don't remember what it was. Any idea how I can get that double-click deal going again?
Doing the following should fix it:
Right click on the .py file you want to open;
Open with -> Choose default program -> More options;
Select the python.exe file.
Explanation:
Your Python scripts have to be processed by another program called the Python interpreter. The interpreter reads your script, compiles it into bytecodes, and then executes the bytecodes to run your program.
Installing a new version might have messed the path to the Python interpreter. The steps listed above will tell Windows to associate .py files with your Python interpreter, thus fixing the issue.
This link with Python on Windows FAQ might also be of help.
There will be an option of "Open With" after right-click on the file go and choose CMD. I hope it helps if not then sorry. Because I use Parrot OS
Save the following text to a file called something like python.reg (the .reg extension is important). You might need to modify the last line to be your exact path to python.exe!
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shell\open]
[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
#="\"C:\\Python2.7\\python.exe\" \"%1\" %*"
Find the python.reg file you just saved and double-click it to load those contents into your Registry. If you've performed one of the other operations in other answers (like "Open With"), those "UserChoice" settings are stored somewhere else in the Registry and will override the "Classes" setting shown in this script. So, do one or the other, don't combine them!

Saving and opening Python Script in Anaconda

I use Anaconda with Spyder that was installed in Anaconda to learn python.
The problem: when I'm trying to save python scripts by extending the file name to .py, the python script automatically gets saved in the Windows notepad. And when I try to open that again in Spyder it doesn't open.
In other words, in Spyder I saved the script by doing "file - save as - 'filename.py'"
and I tried to open the saved script by doing "file - open - 'filename.py'" in Spyder.
However, it doesn't work.
I two questions about this.
Is it normal for python scripts to be saved on windows notepad? If not, what is wrong here and how do you normally save python scripts with Anaconda?
Why doesn't Spyder open the notepad file, the python script I saved? I added the extention .py when I saved it but it is not opening in Anaconda Spyder regardless.
I realize this is a basic question to many and the answer may exist somewhere but I have a very hard time finding it and I'm hoping someone can help me solve this problem. Thanks.
Answer to the First question
Is it normal for python scripts to be saved on windows notepad? If not, what is wrong here and how do you normally save python scripts with Anaconda?
Check what default program is set
for opening .py file in your system you can change it by
rightclicking on .py file >expand open with > choose open with another
app and tick the option 'always use this for .py file'
Second question
Why doesn't Spyder open the notepad file, the python script I saved? I added the extention .py when I saved it but it is not opening in Anaconda Spyder regardless.
Check type of file your saving "the field below filename it should be
allfiles(*)" probably your script is saving "filename.py.txt" which is
not a proper format for python file

How to read a .py file after I install Anaconda?

I have installed Anaconda, but I do not know how to open a .py file..
If it is possible, please explain plainly, I browsed several threads, but I understood none of them..
Thanks a lot for your helps..
Best,
In the menu structure of your operating system, you should see a folder for Anaconda. In that folder is an icon for Spyder. Click that icon.
After a while (Spyder loads slowly) you will see the Spyder integrated environment. You can choose File then Open from the menu, or just click the Open icon that looks like an open folder. In the resulting Open dialog box, navigate to the relevant folder and open the relevant .py file. The Open dialog box will see .py, .pyw, and .ipy files by default, but clicking the relevant list box will enable you to see and load many other kinds of files. Opening that file will load the contents into the editor section of Spyder. You can view or edit the file there, or use other parts of Spyder to run, debug, and do other things with the file.
As of now, there is no in-built way to load a .py file in Spyder directly from the operating system. You can set that up in Windows by double-clicking a .py file, then choosing the spyder.exe file, and telling Windows to always use that application to load the file. The Anaconda developers have said that a soon-to-come version of Anaconda will modify the operating system so that .py and other files will load in Spyder with a double-click. But what I said above works for Windows.
This answer was a bit condensed, since I do not know your level of understanding. Ask if you need more details.
You can use any text editor to open a .py file, e.g. TextMate, TextWrangler, TextEdit, PyCharm, AquaMacs, etc.

Getting custom files to launch like .exe in Windows

How does Windows know that an .exe application is an .exe application. Also, how would you tell windows to send this type of application to this program, that you have created. Like how Python programs are .py and are text files, but when you click on it, it acts like an executable. Or is it the other way around? I don't know. Any type of help would be useful, thanks.
In Windows this is different.
There is a variable called PATHEXT in the environment, like this
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY
Notice that I've added the .py extension to mark python scripts to be executable. That is the first step.
Then, you need to tell Windows which program will run this script. For that, you have the assoc and ftype commands.
When you type assoc you get the list of known file extensions. Here, I get .py=Python.File as output. Now you have to connect this extension to a program, ie. the python executable python.exe. Do this with ftype:
D:\\>ftype Python.File
Python.File="C:\Program Files (x86)\python27\python.exe" "%1" %*
I think that the correct calls to assoc and ftype are done when installing python. Then you only have to append the extension to PATHEXT.
A .py file doesn't execute when you write $ ./file.py unless you add #!/usr/bin/env python or C:\\Python27\python.exe something like that at the very beginning of it. The OS tries to execute the following: the_thing_you_wrote_after_# file.py If you change the extension to .lold it will work too.
If you try to do ./file.py without adding the line I was talking about earlier, the current shell'll try to parse the text and will definitely give errors.
I'm not quite sure, but Windows checks if it's possible to run a file by checking its extension first. It also searches for the entry point. If you rename your test.exe to test.test it won't work.
You can't launch a .py file like an app unless you use special tools like py2exe and cxfreeze.

Python files created do not save as python file

https://drive.google.com/file/d/0BwR0ium7uufRWnhXWWNFcVJ3S2c/edit?usp=sharing
The files that are blank and say "Credit Card Balance" and "mine" are the files which I created by opening up a new document in IDLE and creating a file. The ones with the little pictures and say ps2_newton are programs which I downloaded off of the internet. I can't open my files which I created like they are regular programs and have to open them under notepad to see the code. Why is this? How can I change my files to look and act like the ones I downloaded?
The files you downloaded have an extension of ".py", which tells Windows to open them with the Python launcher, while the ones you created have no extension, or an extension of ".txt", which tells Windows to open them in Notepad.
Since ".py" and ".txt" are both "known extensions", and you have Explorer configured to hide known extensions (which I believe is the default in all versions of Windows), it doesn't show them to you. However, you can see the difference in the icons.
You can configure Explorer to show all extensions, but this site is not the place to ask about that—try SuperUser.
Meanwhile, when you save your scripts, make sure you save them with a .py extension (or, if they're Tkinter or other GUI apps, with a .pyw extension).
I had the same problem. But I just deleted all of the é's and á's etc. And it worked! So try that IF it is an .py file. Otherwise I understand if it does not work becouse it isn't a python file. You change that by doing this: Save as. Just simply type .py after the file name.
PS Sorry for my bad english...

Categories

Resources