PythonWin opening different scripts in different versions (Python 2 & Python 3) - python

I use PythonWin as my main script editor.
To differentiate between scripts I use different extensions, *.py for Python 2 scripts and *.py3 for Python 3 scripts.
I have each extension opening in its appropriate version of PythonWin.exe through the HKEY_CLASSES_ROOT registry entries, so that's fine.
The problem is whenever I have PythonWin.exe version 2 running and I click on a *.py3 file it opens my script in the currently open version 2 version of PythonWin. Similarly if I have version 3 open and I open a *.py file it opens in the currently open version 3 PythonWin.
I am wondering if anyone else has had this issue and whether they have found any kind of descent work-around so each file extension opens in their respective version of PythonWin.

It turns out PythonWin.exe has some passable arguments to allow it to open more than one instance.
You can force it to open a new process by using the command
"C:\python27\Lib\site-packages\pythonwin\Pythonwin.exe" /new "%L"
Using this as the opening command in the registry entries for .py and .py3 would open each in their respective version of PythonWin.
The only disadvantage is that each time you open a new file, it will open a new PythonWin instance.
/new will also disable any Dynamic Data Exchange(Windows uses this to communicate between open applications) so the instance of PythonWin will never open any new files opened from Windows Explorer within it.
Once you have an instance open, you may as well just use the "Open" filemenu option to open new files, or just have multiple instances of each version of PythonWin open.

The thing in pythonwin.exe that looks for an existing instance and opens a *.py file in that instance is a protocol called a DDE(Dynamic Data Exchange) server. PythonWin opens one up every time it runs and when it detects a DDE server on the same channel, it passes the previous instance some python code to open the file and then terminates.
The easiest way to prevent PythonWin 2.7 from talking to PythonWin 3.9 is to have them open separately named channels to talk on.
Change the channel name in the intpyapp.py file.
Look for these two functions
def MakeExistingDDEConnection(self):
and
def InitDDE(self):
Change the channel name from "Pythonwin" to "Pythonwin37"(or something) IN BOTH FUNCTIONS
Delete any "intpyapp.pyc" files from your computer and try it out! You'll love it.
(*.pyc files store a compiled version of the code in the *.py file so no changes will be made to the running code until these are deleted)
If that still doesn't work, check out what command line is being used to run pythonwin.exe in the Windows registry
\HKEY_CLASSES_ROOT\Python.File\shell\open\command
and make sure there's no /new or /nodde parameters in the command line.
Here's an example of a good registry entry
"C:\python37\Lib\site-packages\pythonwin\Pythonwin.exe" "%L"

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.

Right click context menu on Windows executing Python scripts

I want to start a python script using the Windows Explorer's right-click context menu.
These are my registry entries to achieve that:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell]
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files]
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files\command]
#="\"C:\\Users\\Admin\\PycharmProjects\\clean_desktop\\wescript.py\" %1"
I also edited the environment variables so that if I simply run the command:
"C:\Users\Admin\PycharmProjects\clean_desktop\wescript.py" "some_argument" in cmd it works just fine.
I tested it with a simple .exe file compiled from an c program
[HKEY_CLASSES_ROOT\*\shell\CleanDesktop - Add to persistent files\command]
#="\"C:\\Users\\Admin\\PycharmProjects\\clean_desktop\\ctest.exe\" %1"
And it works just fine. But if I try the same with a .py file Windows states that: This Application cannot be opened on this PC
Is there a way to achieve what I want without the need of making an .exe file out of the .py file ?

Windows won't open .py files, even when "python launcher for windows (console)" is assigned

Phyton was working fine, until one day IDLE refused to open (nothing happened when I double clicked it), so I decided to reinstall it. After I was done (I installed Python 3.5), IDLE was working again, however all of my scripts had blank icons instead of a Python icon (even though Windows was set to open it with "python launcher for windows (console)"). I tried to open thouse but they would instead open in command line and close after a fraction of a second (which is strange, since some of those scripts were supposed to create applications that should stay put until the "x" (close) button is pressed).
I tried to delete python in path variables and then add it back again, but it didn't work. I also tried to assign other python executables (like "python.exe" and "pythonw") to open those files, whithout success...

OSX: Creating an automator workflow with Python makes the workflow invalid

I'm making a chat client for OSX, and I wanted it to be able to run as both a .app (for my less technologically inclined users) and as a .py file. I made a workflow app that contained two .py files (an auto-updater and the client itself), run by a python script in the .wflow file. This worked well. However, I couldn't update the updater or workflow script, and the icon was the Python rocket instead of the icon I had chosen. Then, I combined the client .py file with the updater .py file. This still worked, and now I could update the updater. I still couldn't update the python script in the workflow, though, and the icon was still wrong. So, I modified the updater to open the .wflow file, split it into a list (based on python comments in the workflow's python script, such as "#Start") of the stuff before the script, the script's modification time, and the stuff after the script. If the modification time isn't the same as the modification time of the remote file (the one that the updater updates from), then the script downloads the remote .py file, replaces characters (<, >, &) that .wflow files replace ('<' -> "<"), and opens document.wflow with the "w" (write/replace) flag. Then, the stuff that was before the old script, the downloaded script, and the stuff that was after the old script (using file.write(''.join(list))) are all put into document.wflow. This should work, but OSX no longer sees document as an automator file.
As you can see, OSX thinks that the old file is a Workflow, while the new file is a "Microsoft Excel 97-2004 workbook". The IMClient.app (the application that contains document.wflow) gives this message when I try to run it: "The document "IMClient" could not be opened because it is damaged or incomplete." Does anyone know how to fix this?
I'm using python 2.7 and OSX 10.7. The updater is downloading files via FTP.
If clarification is necessary, just ask. I'll post the working and nonworking files if anyone wants them.
EDIT: the file command gives "document.wflow: XML document text" for both the old and new file.

Python script into a standalone single file executable that can auto-update?

I wrote a program in Python 2.7 for my office. Using PyInstaller with the --onefile flag, I've compiled it into a single EXE file to distribute to my co-workers. What I'm trying to figure out now is how to supply updates automatically but still keep the program as a single file.
My current method is to have the program alert the user of a new version and give instructions where to obtain the updated file. This works, but I want to find some method of updating without the user's involvement.
I am aware of Esky, which can create auto-updating Python programs, but as far as I can tell, it requires the program to exist as multiple files.
Any suggestions on how I make a standalone, single file EXE python program that can auto-update?
you can download your new executable to a temp folder
use __file__ to know the current location of the executable
show a message that you need to restart
use sys.execcv or subrocess alternative to replace the current running process with a small script something like
"sleep(1) & copy {tempfile} {filename}".format(tmp_file,__file__)
(simple batch commands that overwrite the old exe with the new exe)
When your application has alerted user of a new version and user has said they want it, download the new exe, start the new exe, and shut down current exe. The new exe should remove any old exes it sees around. The different versions need to be named differently.

Categories

Resources