Set Python IDLE as Default Program to Open .py Extensions [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I am on Windows 7. I have Python 2.7.8 (64 bit) installed. Today, I changed the default program that opens .py files from IDLE to Windows Command Processor and stupidly selected the checkbox that said "always use the selected program to open this kind of file".
What I want to do is change my default program back to IDLE.
When I attempt to change it back to IDLE, I go to Control Panel\Programs\Default Programs\Set Associations and select the .py name and click Change Program. I do see python.exe but selecting that does nothing. I then use the "Browse" button to navigate to C:\Python27\Lib\idlelib but don't know if I should select idle.py, idle.pyw, idle.bat or some other IDLE program that will force the default program to be IDLE!
Nothing happens after I select one of these.
How do I make IDLE be the default program that opens .py files and now disassociate Windows Command Processor from being the default?

If the "always use the selected program to open this kind of file" checkbox is grayed out, ungray it by:
Open regedit Go to
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts
Find the .py extension among the list
Delete the UserChoice key (folder)
Then you can navigate to:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python XX and choose IDLE...
or
navigate to C:\...\PythonXX\Lib\idlelib\idle.bat and select that.
---- If You are updating from a previous version of Python ----
I just updated from Python 3.4 to Python 3.5 and it was a nightmare setting IDLE as my default program. I wasted so much time. I finally got it to where I no-longer have to right-click and select IDLE. Instead, I just click on a .py file and it opens IDLE by default... and it has a legit-looking Python icon (not an ugly .bat icon).
I'm posting this so mainly I'll remember how I did this!
Uninstalled Python 3.4 using control panel (Windows).
Deleted remaining C:\Python34 folder entirely.
Went into registry:
Windows "start" orb (bottom-left) > type "run" > type "regedit" in Run dialog box.
Looked in the following areas and deleted python keys entirely:
HKEY_CURRENT_USER\Software
HKEY_LOCAL_MACHINE\SOFTWARE
HKEY_USERS.DEFAULT\Software
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node
(Optional, do with caution) In left pane of Registry Editor, right-click and select "Find". I looked in keys, values and data but looking in just keys might be enough. I deleted the ones that looked very specific to start-up procedures. You can press F3 to find again each time.
I ran CCleaner specifically to remove / clean-up the registry, but also to clean up temp folders and just general maintenance. Run it several times.
Restart computer
Install new version of Python. If installation fails first time, consider un-checking some of the "Advanced Options" during installation. Ex: "download debug binaries".
After you install, .py files may not be associated to a program. You can go to your control panel and set file associations.
Look for .py file extension and click "Change program" button in the top-right. Browse to the location of idle.bat. Example: C:\Python35\Lib\idlelib. Click the idle.bat file and press OK.
At this point, you should be able to click a python file (file with .py extension) and it will open using IDLE's idle.bat program. I've read that sometimes this doesn't work for some people.
But now, the icon of .py files look like the icon of the .bat program. This looks like crap so I did the next steps to associate a Python icon with .py files.
Start > run > regedit (to launch Registry Editor)
In Registry Editor, navigate to: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.py\UserChoice and make note of the Progid (Applications\idle.bat in my example).
If UserChoice is not there, navigate to HKEY_CLASSES_ROOT\.py and make note of the (Default) value (ex: py_auto_file).
I didn't know which to change so I changed BOTH.
Go to: HKEY_CLASSES_ROOT\Applications\idle.bat. A "DefaultIcon" folder didn't exist so I right-clicked idle.bat and chose New > Key and named it "DefaultIcon". For the (Default) data, I put C:\Python35\Lib\idlelib\Icons\idle.ico to reference the .ico image that's within the IDLE icons folder.
Go to: HKEY_CLASSES_ROOT\py_auto_file and did the exact same steps as step 12.1.
Restart your computer.
End-result
After all these steps, I am now able to double-click a .py file in Windows and it launches the file in Python IDLE.

Right click on any .py file
Click Open With...
Click Choose a default program...
IfIDLEis on the list, click it.
else
Click Browse, and find the IDLE program
Click OK and voila!

Related

Run a Python program at start-up on Windows

I am developing a key-logger on Python (only for curiosity sake). And the script will be an executable. The process will not need a UI or user interaction.
Is there any way, even in another executable to make the key-logger start at start-up?
I don't use Windows, but you can try making a batch script that runs your python file and make that script Run a program automatically when Windows starts:
Click the Start button Picture of the Start button , click All
Programs, right-click the Startup folder, and then click Open.
Open the location that contains the item you want to create a shortcut
to.
Right-click the item, and then click Create Shortcut. The new
shortcut appears in the same location as the original item.
Drag the shortcut into the Startup folder.
As I said, I don't use Windows, so it might be totally wrong.
You can refer here for making the BAT file, which basically says:
#echo off
python c:\somescript.py %*
pause
I think that the above answers are too complex. What I did was just drag and drop or copy and paste my file in the startup folder by clicking the quick access toolbar, typing "startup", and the job is done.
I am using Windows 10 operating system, so it might be different in your case.
I hope this is useful.
Edit: The key to this solution is to have the .py extension files open by default by the python console (not a text editor), otherwise it will just open the file instead of executing it. In order to select the default program a type of file is opened with, right click on the .py file -> Open with -> Choose default program. See this example:
Use VBScript:
1-> create anyname.vbs with this data:
Set wvbs=CreateObject("Wscript.Shell")
wvbs.run "full location of your File",0
2-> copy anyname.vbs file in C:\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup this folder
Now when windows start it will run your file in hidden mode
Open run with "Start + R", then open "shell:startup". it opens you a folder(the folder that was mentioned before at start menu), and every file that is on this folder, well run at startup.
The folder path is: "C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" (you can copy it on windows explorer, or copy this path and put your account name on USERNAME)
this is the trick i used in my script:
from os import getcwd
from shutil import copy
copy(getcwd()+'/FILE_NAME.exe','C:/Users/USERNAME/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Startup')
They are some ways for file name as well, but im not familiar with it. this code copies it self to startup folder and starts each time windows boots
Create a shortcut in the shell:startup folder with an absolute path to your pythonw.exe executable. The w version of Python is needed so that it starts up without a shell in the background.
Detailed instructions
Open the startup folder
Type the Windows and R keys at the same time. In the Run dialog, type shell:startup and it enter. This takes you to the Startup folder. Shorcuts in this folder are launched when the computer starts. (For me, the startup folder is at C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.)
Create new shortcut
Right-click in this folder and say "New shortcut." Browse to "This PC," "Windows (C:)," "Program Files,", "Python39", and select pythonw.exe. Your Python install is likely in a different location. More recently, Python is found somewhere within %appdata%. (You can paste %appdata% into the run dialog to open this folder and look for Python. It opens in Roaming by default, but be sure to look in Local, too.)
As mentioned at the start of this answer, it is important that you select pythonw.exe rather than python.exe as pythonw.exe will run without opening a command prompt.
On the next "Create Shortcut" screen, titled, "What would you like to name the shortcut," you can name it whatever you want. For the OP, I recommend "Self penetration test."
Click "Finish."
Edit the shortcut to launch your script
In the Startup folder, right click on the app you just created and select "Properties."
In the Properties dialog's Shortcut tab, edit the "Target" to look something like "C:\Program Files\Python310\pythonw.exe" pentest_keylogger.py and the "Start in" to be the folder in which pentest_keylogger.py is found. (As above, you will need to use your Python distribution's path.)
Test your script
Hit OK. Then double-click on the icon to see if it works silently as you desire.
Finally, reboot your machine and use resmon (from the run dialog) or Ctl-Alt-Delete and the task manager to see if your app is running in the background.
Acknowledgements
Thanks to Eryk Sun and Behfar baghery for the core ideas presented here.

How do I change the default editor for a python file?

When I right-click on a .py file, I am given the option to "Edit with IDLE". How can I replace IDLE with another IDE, such as PyCharm? ie when I right-click on any .py file, I would like there to be the option to "Edit with PyCharm".
I am using Windows by the way.
Make a .reg file that looks like the following, then run it and re-login to Windows. Obviously make the path and filename match your desired editor. Note the escaped slash characters. Change how it appears in the menu by substituting your desired command name for "Edit."
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\SystemFileAssociations\.py\Shell\Edit\command]
#="E:\\Applications\\Dev\\VSCode\\Code.exe \"%1\""
The "Edit with IDLE" option is actually placed there by IDLE itself. The thing you could do is just change the default program you want python files to be opened with (by right clicking, choosing "Open with" and choose a default program).
The additional entries in the context menus are specified in the registry. So i think if you want to manually add a "Edit with PyCharm" option you would have to edit the registry yourself. But I wouldn't recommend to do that at all.
Assuming you have Windows 7/8 and have the program (in this case PyCharm) installed already, you would:
Right click any python file (typically ending with .py)
Click "Open With"
Either select PyCharm or the desired program from the list of programs OR
Click Choose Default Program and 'find more options' or 'choose program from list' and find PyCharm in the directory you installed it in and select program
Hope this helps! Be sure to specify if you meant a different OS than Widows.

How to change default directory for IDLE in windows?

The installation directory is "d:\python2.7", and every time I open IDLE and click on menu File and Open item, the default directory is also "d:\python2.7". So I have to change the directory to where I want.
Is there any way I can change it? Using configuration file or changing environment variable?
I tried to add PYTHONPATH in environment variable, but it doesn't work.
I also import os, and use os.chdir(), but it only changes the working directory, not what I want.
Thank you.
If you're running IDLE from a Windows shortcut, you can just right-click on the shortcut, choose "Properties", and change the field "Start in" to any directory you like.
In response to the answer by Aya:
If you're running IDLE from a Windows shortcut, you can just right-click on the shortcut, choose "Properties", and change the field "Start in" to any directory you like.
Yes you can, however Python IDLE will no longer run. I've found that if the Start In directory for the IDLE shortcut is C:\Python33 or C:\Python33\Lib\idlelib (in my case) that it will still work but if I added a new directory below either of these (for example: myfiles) containing my programs then the IDLE editor fails. In my situation, I've spend days now trying to get Python to easily get to my .py programs by clicking FILE+OPEN in the IDLE editor. Also, I've tried everything to get IDLE to run without failing from the Windows Explorer using right click and selecting "edit with IDLE" but although Idle does opens okay trying to RUN a program from there fails. I did manage to get IDLE to work using SEND TO and until I get an answer to my IDLE issue Post that is what I suppose I will have to do. Hope this helps.
Open the idle or PyShell
press Alt + M or File -> open Module
type in idlelib.IOBinding
the window opens, go to line 185
change either 'dirname = None' or 'filename = None' to the value you desire.
Eventually you need to start the PyShell or the editor with rights to edit this module like
sudo idle
if you can not edit it you need to see idle.pyw in the same directory an trace how IOBinding is used by the idlelib.PyShell.main() function and change the same variable.

.py files not opening in IDLE from OSX Finder?

I recently re-installed Python 2.7.3 from python.org on OSX 10.8.2, and now nothing (literally nothing I can see) happens when I double-click on .py files in Finder, or when I right-click/Open-With/IDLE(2.7.3). (Edit: IDLE is already set as the default program to open .py files; the "right-click" part was just to illustrate I couldn't open them either way.)
I have Tcl/Tk 8.5.13 installed (re-installed after I re-installed Python), and I can open IDLE itself just fine, so this isn't that issue where IDLE won't work without the right version of Tcl/Tk. I can also open .py files from the Terminal using idle -e file.py without a problem.
I did notice that the Python Launcher activates when I open files this way, and is absent when I double-click on .py files in Finder (although again, literally nothing happens when I do this--I have the IDLE icon in my dock and it doesn't start bouncing or anything). Could this be a Python Launcher problem? If not, does anyone know what's going on here?
Update: I tried opening IDLE first and then double-clicking on .py files, and that works. So it seems it's just an issue of IDLE not being opened when you try to open a .py file in the Finder.
I know that means I now have at least three other ways of opening my .py files in IDLE, but I really like just being able to navigate to a file in Finder and open it when IDLE itself is not yet open. So I'd still really like to solve this problem if possible (also I really hate when stuff that's just supposed to work doesn't).
Update: Per Ned Deily's suggestion I checked for errors using Console.app and found:
3/8/13 10:42:38.006 AM com.apple.launchd.peruser.501[276]: ([0x0-0x199199].org.python.IDLE[1975]) Exited with code: 1
pops up whenever I try to double-click a .py file in Finder.
Make sure you have set the intended instance of IDLE as the default application for all .py files. One way to do so is to select a .py file in a Finder window, then select the Finder -> File -> Get Info menu item (or Cmd-I). In the Open with: section, pull down the application menu item and select the desired IDLE; if you have multiple copies of IDLE installed, select the Other.. menu item at the bottom of the list and use the file chooser to select the right version of IDLE. Then, click the Change all... button to Use this application to open all documents like this one. You should now be able to launch a .py file in the desired IDLE by double-clicking. If this doesn't work, examine system.log to see if there are any error messages being logged. You can use the Console utility to examine logs (/Applications/Utilities/Console.app).

Edit python31 file and it opens notepad and starts python26

I am in python31,
then I go to file open i left click to open file
and it opens in notepad(simple text editor)python31
The moment it opens the notepad, it starts python26
I thought it has something to open with, and I have changed that to python31
And it still opens python26
EDIT:
The file is created by python26, but it is not executable.
I am guessing here, the question it not very clear.
It sounds like the .py extension in Windows is associated with the Python 2.6 runtime. (This normally get setup this way during installation of Python on Windows). You can change this by updating the associated file extensions and programs in Windows.
By double clicking on the file it is not opening the file for editing but instead running it. If you want to edit the file you have to either right-click and select an approriate edit action or open the file from your editor's 'open file' action. (Or change the .py extension to open in your favourite editor)
I had similar problems, and right clicking and changing edit options didn't solve it. You can try repairing python31 installation with running the msi installer and selecting it as the default python

Categories