can't execute pyw on windows - python

I want to hide the console window of a python program, so I change the file extensions to "pyw", but when I open it, the python IDLE show up even though I choose open it with "pythonw.exe"
If I use "pythonw test.py" in cmd, it works.
So I want to know what's wrong with this and how to solve this, thank you.

Change the program that opens python files.
Assuming you're using Windows, right click any python file (in your case any .pyw file, not .py), properties, change Opens with to pythonw instead of IDLE

For me, I had multiple version of Python installed that was causing issues. Once I had only had one version, I applied that pythonw.exe was the default for .pyw files and it worked.

Related

How can I make .py files run using Python Shell by default?

I made a Python GUI using Tkinter, but when I run it directly (double click the file) it opens the black python window but automatically closes by itself in less than half a second. I found a way to make it open the IDLE editor but it just opens the editor and doesn't run it.
I want it to run the way it runs when you open the IDLE editor and press Run Module. This runs it using Python Shell.
Is there a way I can make it automatically run using Python Shell?
Based on Mark Tolonen's comment you should do two things
rename your file to a .pyw from .py to prefer console-less runs
set your system to open .pyw files with pythonw if that's not configured already
Linux: configure xdg-open
Windows: right click and choose an application from the context menu (you may need to find where Python is installed to select the pythonw.exe executable)
Okay, one of the comments on the original question is correct.
As Terry Jan Reedy (user:722804) said,
It is possible that your mygui.py file is missing 'root.mainloop()' or the equivalent to start the GUI. IDLE lets you omit that during development so that one can interact with tkinter to retrieve values and make changes to widgets.
Adding gui.mainloop() to the end of my program worked.

How to open a python file in Cmder Terminal Quicker?

I want to open a python file in cmder terminal quickly. Currently, the fastest way i know how is to navigate to the directory of the python file in cmder terminal and then run it by calling "python file.py". This is slow and cumbersome. Is there a way for me to have a file or exe, that, when i run it (or drag the program onto it), automatically makes the program run in cmder straight away.
Windows 10
Clarification: I'm using cmder terminal specifically because it supports text coloring. Windows terminal and powershell do not support this.
On windows you can go to the directory with the file in the explorer and then simply hold shift as you right click at the same time. This will open the menu and there you will have the option to use the command shell/powershell and then you don't have to navigate to the directory inside the shell anymore and can just execute the python file.
I hope that helps.
Answer: The escape codes just weren't properly configured for the windows terminals. You can get around this by using colorama's colorama.init(). It should work after that.

.py file opens PyCharm instead of running the script

I have written a python script (lets call it myTest.py) and would like to launch it from the windows command promt.
My python interpreter was added to the PATH environment variable, therefore i can launch it just fine as python myTest.py and it is executed successfully.
I would however like to launch it by simply typing myTest.py in the command promt. But if I do this, the file is opened in PyCharm and is not executed.
I have checked assoc .py which gives me .py=Python.File, as well as ftype Python.File which gives me Python.File="C:\WINDOWS\py.exe" "%L" %*. As far as I understand this, a .py file should be launched with py.exe at the given path.
(If you are as confused as I was by the %L option of ftype, that is not mentioned in its help document: It makes no difference in my case and is equivallent to %0 and %1. Source: https://mail.python.org/pipermail/python-list/2015-November/699263.html)
Can anyone explain why my script is opened in PyCharm and not launched with py.exe? What do I have to do to simply execute it instead?
Edit:
The answer of #Hamed169 solves the problem (thanks!), but doesnt't give any details on why there is a difference between the program that is used to open a file and the one that is returned by ftype.
For everyone that is interested in why there is a difference I have found the following question (has currently no final answer, but quite a bit of information): https://superuser.com/questions/204354/how-do-i-get-ftype-assoc-to-match-windows-explorer
Right Click on a .py file
Click Properties
Click "change" in Opens with:
Select your python interpreter instead PyCharm
Run your .py files and enjoy:)
Thanks a lot Hame
We should change it to the python.exe file path, i.e., (in my case):
C:\Users\NEDA\AppData\Local\Programs\Python\Python38-32

Python shell is open but not .py file?

I am using python 3.4.2 IDLE on windows.
When I open the IDLE shell and then open .py file, then it works,
but when I try to open the .py file by double cliking, it just doesn't open, or proceed anything. Looks like as if nothing has happened.
I would like to open .py file and then just press F5 to see what is going on rather than individually open all the file (I am still beginner to python, I know I can use pycharm, but at this point, just that would be good enough)
When you double click on a .py file, it will run it using the Python Interpreter. You can right-click on the file instead and choose to open it in IDLE.

"Edit with IDLE" option missing from context menu

I have Python 2.7.5 that installed with ArcGIS 10.2.2. When I first right-clicked a .py script I'd previously written it listed the "Edit with IDLE" option in the context menu. However, this option no longer appears when I right-click a .py file.
I have read numerous threads concerning this issue and attempted some of them, such as modifying/removing registry keys and then reinstalling/repairing the software. I am not interested in using an IDE at this point, though many will be happy to know I intend to use an IDE later on. Right now, the purpose is to fix the problem rather than avoid and work around it.
I appreciate the help I've gotten from the online community in the past, and I'm confident someone will come through with a solution for me.
How do I get "Edit with IDLE" back in the context menu?
Most of the time when this problem occurs, people will read answers directing them into the windows registry, often unnecessarily.
In the majority of cases, the registry key
HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE\command
will already exist and have the correct value, but it will only be used by Windows shell context menu if .py files are set by default to open with python.exe.
This is the first thing to check, and solves the problem in the majority of cases:
right click on a .py file
open with...
choose default program...
tick always use the selected program to open this kind of file
choose python.exe
This runs the script in a terminal, but also sets the filetype back to python.
Now check the right click menu again.
As a newer update, for people that are having the "missing idle" issue with Windows 10 using Python 3.6 (64-bit).
From my experience, this happens when you install other python editors, and you change your default app to open with that editor.
The easiest way to fix this issue is to click the "start" button, then navigate to settings --> System --> Default Apps --> "Choose default apps by file type". Scroll down till you find ".py" file type and click on the icon and choose "Python"(has a little rocket ship in the icon). This will change your default app back to "Python.exe", and the context menu "edit with idle" will appear once again on your ".py" files.
Hope this helps!
Directly from: https://superuser.com/questions/343519/python-idle-disappeared-from-the-right-click-context-menu
Here's a reg file to add the command to edit with IDLE for Python.File (.py) and Python.NoConFile (.pyw) file types. It's for Python 2.7, installed in C:\Python27, so substitute the paths to pythonw.exe and idle.pyw that are relevant to your installation. Save it to a .reg file. Then right-click and choose Merge.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE\command]
#="\"C:\\Python27\\pythonw.exe\" \"C:\\Python27\\Lib\\idlelib\\idle.pyw\" -e \"%1\""
[HKEY_CLASSES_ROOT\Python.NoConFile\shell\Edit with IDLE\command]
#="\"C:\\Python27\\pythonw.exe\" \"C:\\Python27\\Lib\\idlelib\\idle.pyw\" -e \"%1\""
Another option would be to just open the file with the idle by choosing open with:
C:\Python27\ArcGIS10.3\Lib\idlelib\idle.bat
as the default program.
I got the "Edit with IDLE" back with the option "Repair" of the deinstallation-menu.
Adding a little more detail to Teodorico Levoff's answer for those who need a little more explanation, like myself.
Open a text editor, such as Notepad, and copy/paste the following, but be sure to modify the paths to pythonw.exe and idle.pyw so that they match the paths in your own system:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE\command]
#="\"C:\Python27\pythonw.exe\" \"C:\Python27\Lib\idlelib\idle.pyw\" -e \"%1\""
[HKEY_CLASSES_ROOT\Python.NoConFile\shell\Edit with IDLE\command]
#="\"C:\Python27\pythonw.exe\" \"C:\Python27\Lib\idlelib\idle.pyw\" -e \"%1\""
Save this text file as idle.reg in the Python27 folder, so you now have a file that resembles this (with your own file path, of course):
C:\Python27\idle.reg
Right-click the idle.reg file, and in the context menu, click Merge. A couple instructions may appear or notices that require a Yes or Continue that I selected. In the end, my .py files give me the option to "Edit in IDLE" again.
I hope these explanations are helpful. Big thank you to those who've solved this problem before and shared there solutions.
For opening your file in IDLE mode or in python exe mode u can follow this simple steps. Works for me in my windows 7 system.
For IDLE mode:
Select the file and press right mouse
press open with---->choose default program
press browse and go to the folder where your python programm is installed
in python folder search IDLE and select the batch file
press open and your file will be open in IDLE edit mode
For python.exe mode:
In command prompt write "where python"
copy the path from cmd
Select the file and press right mouse
press open with---->choose default program
press browse and go to the selected path
press open and your file will be open in IDLE edit mode
As click to save button to save your python code there will be Two Extensions...1) .py and 2) .pyw.
So for Python 2 you have to save python program using extension .pyw.
I think the majority of cases are caused by the Py launcher that comes with Python 3. When you install Python 3 alongside Python 2.x, the *.py and *.pyw files are associated to run with the new Py launcher. Since *.py and *.pyw files are no longer associated with Python.exe, that breaks the "Edit with IDLE" and similar context menu options, despite all relevant registry entries being present and correct.
Right clicking a file and choosing Python.exe and selecting "always use the selected program to open this kind of file" option fixes the problem (even if Python.exe seems to be already set as the default program) but then you lose the Py launcher functionality. This may well be considered a bug with the Python 3.x installer and I think should be fixed at that level by the Python developers.
Meanwhile, I'm sure registry wizards can find a workaround for this but unfortunately, that's beyond me at the moment.
I followed Teodorico Levoff's directions above, as well as Abyx's from the following page:
https://superuser.com/questions/280636/trying-to-edit-a-python-file-but-the-edit-with-idle-has-disappeared-when-i-rig
However, it only works partially. When I right-click on a .py file, no 'Edit with IDLE' shows up. Choosing 'Edit' opens the file with IDLE. Also, the python icon is not associated with .py files. I decide to associate it with Notepad, then associate it with Python again. That works the magic. The icon is back, and so is the 'Edit with IDLE' option. Hope it helps.
This issue is arising because of the problem in the registry of Python installation. While one may edit the registry and resolve the issue, the simple solution for this can be:
DELETE ALL THE REGISTRIES pertaining to the py extensions and
Re-install Python and let installation take its course of action.
The problem will definitely resolve.
Happy Programming
After uninstalling both 2.7 and 3.6, reinstalling 3.6, I ran the init.py ,main.py, and idle.pyw found in C:\Program Files\python\Lib\idlelib and the edit with menu reappeared

Categories

Resources