How to execute .py file with double-click - python

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!

Related

Can't open python scripts, asks to choose how would you like to open this file

So I had python for a long time, but I didn't code just downloaded scritps and all that. I wanted to install python 3.7, when I did everything seemed to work fine, but my .py files didn't open.enter image description here
Images
You may tell windows what program should open py files with. If you want to open them with a text editor or a python interpreter installed, etc. Go to start menu and type "default programs", from there you need to tell windows what praogram you want to associate with py file

Python 3 install error. This file does not have a program associated

I have encountered an error while try to run a python 3.6.4 script after upgrading from 2.7.
I had initial installed both python 2.7 and 3.6 but them deleted 2.7 because I had no use for it and i am not getting this error.
I have tried:
Reinstalling python 3.6.4
changing the install location of said install
Repairing the python 3.6 install
Error:
A Python script by itself is not an executable program. So if you want to run the script Evosim.py by just entering Evosim.py in the command line, the .py file ending needs to be associated with the Python interpreter. It seems that currently in your system this is not the case.
There are two ways to deal with your situation:
Start your script by entering python Evosim.py instead of only Evosim.py in the command line.
Fix the broken association of the .py file ending with the Python interpreter. The answers to this question migth help you with that.
Solution #1
Go to your registry by searching regedit.
Open category HKEY_CLASSES_ROOT. Select .py. If .py don't exists then make it by yourself.
Double click the Default. It will make a popup. Change its value to Python.IDLE and click OK. Make a new String key in .py naming Content Type with value text/plain.
Solution #2
If the above method don't works then just go ahead and reinstall your python (also uninstall Python Launcher). Your modules and files will be at their place.
You can now open .py files.
Running command prompt as an administrator resolved my issue, and able to execute my python script
1). Right click any python file
2). Click Open with then Choose another app
3). Click on More apps then Look for another app on this PC. It should open up file explorer
4). Select python.exe wherever it is located.
A sample path is C:\Users\<user>\AppData\Local\Programs\Python\Python38\python.exe for a user installation. A way to check this path is by typing where python into cmd; it will list all installations of Python.
Now the python.exe you chose is associated with all .py files and typing file.py should not give an error.

How to get Spyder to open python scripts (.py files) directly from Windows Explorer

I have recently installed the Anaconda distribution on Windows 7 (Anaconda 3-2.4.0-Windows-x86_64). Unlike IDLE, I can't right-click and open a py file in the Spyder IDE. I will have to open Spyder first and then navigate to the file or drag and drop it in the editor. Is there any way to open the file in the editor directly from Widows Explorer?
With the current version of Anaconda (4.1.0) you can simply right-click on a python script in Windows File Explorer and choose "Open with". The first time you do this you need to select "Choose default program" and then browse to spyder.exe in the Script directory in your Anaconda installation. Also make sure that the "Always use the selected program to open this kind of file" is unchecked and then click OK. From now on spyder.exe will always be listed as one of the options when you select "Open with" from the right-click menu in Windows File Explorer.
I have had a similar problem with another piece of software that I use.
My work around for this problem is to set the file association for .py files to C:\Anaconda\Scripts\spider-script.py via the Open with dialog. If you now try to open your File.py by double clicking you'll receive an error like
~\file.py is not a valid Win32 application.
This can be resolved by editing the spyder-script.py registry key:
HKEY_USERS\S-1-5-21-3559708500-1520960832-86631148-1002\Software\Classes\Applications\spyder-script.py\shell\open\command
and replacing the default value "C:\Anaconda\Scripts\spyder-script.py" %1 with "C:\Anaconda\python.exe" "C:\Anaconda\Scripts\spyder-script.py" %1. Use the search function for this key if the path isn't the same for your machine, and of course use the appropriate path for your python installation. spyder-script.py should now execute in a python shell.
From the docstring of ftype,
...Within an open command string, %0 or %1 are substituted with the file name being launched through the association.
(Spyder maintainer here) This functionality is available as part of our Windows installer. In other words, if you install Spyder with it, then you'll see a new entry in the Open with menu of the Windows Explorer that allows you to open Python files directly on Spyder.
Unfortunately, it's not possible for us to do the same for other installation methods (i.e. when using Anaconda or pip). That's why it's necessary to resort to different hacks, as the ones mentioned in other answers here, to have this functionality for them.
What is working very well for me in Windows (10), is associating the *.py files with a batch file (let's say "SpyderBATCH.bat") containing this line :
[ANACONDA_FOLDER_PATH]\pythonw.exe" "[ANACONDA_FOLDER_PATH]\cwp.py" "[ANACONDA_FOLDER_PATH]" "[ANACONDA_FOLDER_PATH]/pythonw.exe" "[ANACONDA_FOLDER_PATH]/Scripts/spyder-script.py" %1
Where [ANACONDA_FOLDER_PATH] has to be replaced with the full path to the Anaconda folder (usually under "Program Files").
What Windows does, when double-clicking on a python script (let's say "file.py"), is pass to SpyderBATCH, as parameter number %1, the full path to "file.py".
Then Spyder is launched and displays the script "file.py" in the editor view.
I figured I would post my solution for this as well.
I have Spyder installed in multiple different environments. You can't simply call the spyder-script.py script without errors, because the environment must be activated.
#echo off
call [YOUR_CONDA_PATH]\Scripts\activate.bat [YOUR_CONDA_PATH]
call conda activate [YOUR ENVIRONMENT]
call start [YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\pythonw.exe "[YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\Scripts\spyder-script.py" %1
You can remove the second line and remove the environment extension from the third line if you have Spyder installed in your base environment.
Hopefully for anyone experiencing any weirdness with the other solution, this one will do the trick by activating the environment correctly.
The solution from JoeB152 worked perfectly for me!
If you are interested in adding the spyder icon (or any other) to the .py-files and if you would like to avoid the cmd-pop-up, I found out the following workaround which is feasible without admin rights:
Download the portable version of Bat To Exe Converter (I used v3.0.10).
Open your custom .bat-file in the Bat to Exe Converter.
In the options settings, activate "Icon" and give the path to the respective icon (for me it's in: .../AppData/Local/Continuum/anaconda3/Scripts/spyder.ico).
Set Exe-Format to Invisible (no empty cmd window would pop up anymore)
Convert your .bat-file to an .exe-file.
As usual, set to open .py-files with the newly created .exe.
Enjoy!
Environment:
Windows 10,
Conda 4.8.2,
Spyder 4.0.1,
Python 3.7
This problem is related to anaconda installation defaults - it does not register itself in PATH by default an dicourages users to do so. After proprly registering all directories in path, spyder.exe works as expected.
How to know, what to register?
locate and activate.bat an run it in cmd, then run echo %PATH% and manually register all directories mentioning anaconda.
Alternatively, reinstall anaconda with PATH registratin enabled.
Then you can associate .py files wit spyder.exe and association will work.
System: Windows 11, Python 3.9.7 (Installed through Anaconda3)
This solution will allow you to double click .py files and have them open in the Spyder in the environment of your choice, but does NOT associate .py files with the icon for Spyder.
I'm answering because it took me over an hour to understand & piecemeal together all the (great) solutions that are provided in this thread to get something that works (thanks Martin Sorgel, JoeB152 and Max-K).
NOTE: Some commentors above say to use a Bat-to-Exe converter & that will let you get the icon too.. but, DON'T! All of the ones you're finding via google have got some bad malware in them and my computer ended up deleting the .exe's I was making using that method because they had a Trojan in them.
Full Solution:
STEP 1: Make a .bat file that will launch Spyder in a specific environment.
1.1 Open a plain text editor (e.g. Notepad, etc.) and copy/paste the text below into it.
#echo off
call [YOUR_CONDA_PATH]\Scripts\activate.bat [YOUR_CONDA_PATH]
call conda activate [YOUR ENVIRONMENT]
call start [YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\pythonw.exe "[YOUR_CONDA_PATH]\envs\[YOUR ENVIRONMENT]\Scripts\spyder-script.py" %1
Update [YOUR_CONDA_PATH] in the text above with the path to Anaconda3 on your computer. Mine was C:\Users\myusername\Anaconda3\ & yours is likely similar.
1.2 Save this new file as spyderlaunch.bat and place it on your computer somewhere that you'll NEVER move it (otherwise you'll have to do STEP 2 each time you move this file. I save mine in a python_env_settings folder where I keep info on what packages I installed manually in my different environments).
NOTE: JoeB152 says you can remove the second line and remove the environment extension from the third line of the text above if you have Spyder installed in your base environment. I'm not sure if this works...
1.3 Make sure your new .bat files works! It works if when you double click on spyderlaunch.bat, that it launches and opens Spyder in the environment you want it to! (Spyder will show the environment it opens in on the bottom right hand side: ).
STEP 2: Tell your computer to associate (i.e. open) all .py files with the spyderlaunch.bat file you just created.
2.1 Open an Anaconda Terminal with "run as an administrator" (by right clicking on the application) and run the following 2, separate commands. Update[PATH_TO_YOUR.batfile] to wherever you saved spyderlaunch.bat in 1.2.
assoc .py=Python
ftype Python="[PATH_TO_YOUR.batfile]" "%1" %*
Errors?
If you don't run the Anaconda Terminal application as an administrator you will be denied access to associate .py=Python. If that's not your issue, then check that the spaces and quotation marks are exactly where they appear above. In particular, you may want to make sure there is a space in between the quotation marks around [PATH_TO_YOUR.batfile] and those around %1.
I was unable to find a spyder.exe on my installation of conda. However in my users/.anaconda/navigator/scripts I found a spyder.bat file. Using this to open the file opens an anaconda prompt and shortly after spyder will open the file. The file icon is broken but it works for me. Hope this might help.
(Comment in relation to the responses by JoeB152 and Jessica Haskins - I am new, so I cannot leave comments)
I found that their suggested .bat file works once you copy-paste the following file from A to B:
A) C:\Users\USERNAME\Anaconda3\Scripts\spyder-script.py
B) C:\Users\USERNAME\Anaconda3\envs\ENVRIONMENT_NAME\Scripts\
...where ENVIRONMENT_NAME is the name of your environment, such as main or test.
The .bat file contains:
#echo off
call C:\Users\bloggsj\Anaconda3\Scripts\activate.bat C:\Users\bloggsj\Anaconda3\
call conda activate C:\Users\bloggsj\Anaconda3\
call start C:\Users\bloggsj\Anaconda3\envs\main\pythonw.exe "C:\Users\bloggsj\Anaconda3\envs\main\Scripts\spyder-script.py" %1
Then associate .py files with that .bat file (e.g., via the 'Open with...' dialogue).
Alternatively, you could try using in the last line of the .bat file the file path: "C:\Users\bloggsj\Anaconda3\Scripts\spyder-script.py"
Get Spyder by itself:
https://docs.spyder-ide.org/current/installation.html
Set your default file opener to your newly installed spyder
To be able to add packages:
Make sure Anaconda is installed.
Go to Spyder preferences
Go to Python interpreter
Select: "Use the following Python interpreter"
Select file path with Anaconda and hit apply
Now you should be able to open files directed using Spyder and update your environment using Anaconda.
I made a batch utility spy.bat for this (not in the Anaconda directory)
spy.bat
call E:\Anaconda3\Scripts\activate.bat E:\Anaconda3
e:\Anaconda3\python.exe e:\Anaconda3\Scripts\spyder-script.py %1
Then, I associated the .py file type in Windows with this batch file as:
Right click any .py
Choose 'Open With' -> 'More Apps' -> 'Search on your PC' ->'select spy.bat'
In the spy.bat:
E:\Anaconda3 is my Anaconda installation directory.
First line starts the environment (Anaconda prompt).
Second line starts the spyder-script. %1 is to open the pyscript you want to open.

Impossible to set python.exe to *.py scripts on Win7

i've installed py 2.7 (64bit) on my PC with Win7 (64bit) without problem but I'm not able to run *.py scripts via DOS shell without declare python full path.
Let me better explain :
If I type D:\ myscript.py it doesn't work. The script is open with wordpad
If I type D:\ C:\Python27 myscript.py it works and run correctly
I try to change the default application software for *.py file via Win7 GUI ( control pannel etc etc) but without success.
Python is not present in the list of available sw and in any case also with the manual set I'm not able to associate python.exe at *.py files.
I've checked in my environment variables but I've not found problem (python path is declared in Path = C:\Python27\;C:\Python27\Scripts).
I've tried also to modify HKEY_CLASSES_ROOT->Applications->python.exe->shell->open->command :
old register value "C:\Python27\python.exe" "%1"
new register value "C:\Python27\python.exe" "%1" %*
without success.
Any suggestion?
Thanks
Here is another check to make, which helped me figure out what was going on.
I switched from the 32bit Anaconda to the 64bit version. I deinstalled, downloaded then reinstalled, but several things didn't get cleaned up properly (quick launch stuff, and some registry keys). The problem on my side was that the default installation path changed, from C:\Anaconda to C:\Anaconda2.
I first tried the assoc and ftype tricks, everything was fine there. However, the HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command registry key was pointing to the old Anaconda path. As soon as I fixed this, python.exe showed up when I tried associating with "Open with" and everything went back to normal.
I also added the %* at the end in the registry key.
You could try to use the ASSOCIATE command in CMD:
ASSOCIATE .py C:\PathTo\python.exe
More information at http://ss64.com/nt/associate.html
#slv 's answer is good and helped me a bit with solving this problem. Anyhow, since I had previous installations of Python before this error occured for me, I might have to add something to this. One of the main problems hereby was that the directory of my python-installation changed.
So, I opened regedit.exe and followed these to steps:
I searched the entire registry for .py, .pyw, .pyx and .pyc (hopefully I did not forget to mention any here). Then, I radically deleted all occurrences I could find.
I searched the entire registry for my old python-installation-path (e.g. C:\Users\Desktop\Anaconda3). Then I replaced this path with my new installation path (e.g. C:\Users\Desktop\Miniconda3). Thereby, I also came across and replaced HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command which #slv mentioned.
Afterwards, it was possible again to connect a .py-file from the Open with...-menu with my python.exe.
The *.py file is a source code file. If you set up your system environment correctly, you need to run python myscript.py
the following answer is related to your question
Making Python scripts run on Windows without specifying ".py" extension
Do you know that when you change the default application of a file, you are able to browse for the application?
You can click on the "browse" button (as shown in the red rectangle), then choose C:\Python27\python.exe.
Also remember to click on the "Always use this program to open this program" checkbox, which is shown in the green rectangle, so that win7 knows how to open this file the next time you ask it to open *.py file.
Then I believe you'll be able to run myScript.py simply by typing "myScript.py" in the correct dirctory in DOS shell.
After doing all the above steps, be sure to reopen a new command shell.

Python scripts stopped running on double-click in Windows

I always ran my scripts on windows by double-clicking them. However after I reinstalled my python versions this is not happenning. My python installations are on C:\Python27 and C:\Python33. PATH has C:\Python27\ in it. If I try to run a script from cmd, it works ok. But when I double-click any .py file nothing happens.
I am completely clueless as I don't use windows often for scripting.
What can be the reason for that?
Steps to cure this problem
1) Right click on your script.
2) Choose properties and then
3) Change the option how to open your script (set it as python).
This will make all of your .py extension scripts into running on double click in windows
Double-clicking is not related to PATH, but extension association.
Check register extension option when you install(or recover) the python.
Removed all python versions and folders. Installed WinPython-64bit-2.7.5.1 package (as before) and everything works now. I can run my scripts by double-clicking them. Really strange.
I was having the same issue. The code works in the IDLE but not on double click. I ran the script through the command prompt and it gave me an error that the IDLE didn't find. Windows didn't like the ascii characters I was printing. I removed them and the script started to work on double click again.

Categories

Resources