Running a python script as a default browser without a hardcoded path to python.exe

2

0

I have a short python script that is set as my default browser in the registry. It will choose to launch IE or Firefox based on the URL (Some work sites only function in IE). In the past, I just used a full path to the python installation to launch it, but this recently broke due to a change in the python install location (it is now bundled with our project files, so it may change more frequently). The path should be updated when it moves, so simply depending on the current environment to find python should be sufficient.

This is the old registry command entry:

[HKEY_CLASSES_ROOT\RRBrowserChooserURL\shell\open\command]

@="\"C:\\Python26\\python.exe\" \"C:\\browser_chooser.pyw\" \"%1\""

Here's the best working option I have come up with so far:

[HKEY_CLASSES_ROOT\RRBrowserChooserURL\shell\open\command]

@="\"C:\\Windows\\System32\\cmd.exe\" \"/c\" \"C:\\browser_chooser.pyw\" \"%1\""

However, this briefly pops up the command window when opening a link. I'd like to avoid that.

Without rewriting my script in another language and compiling it into an executable I can reference directly, is there a good way to get this to execute without knowing the path?

RabbleRooster

Posted 2012-11-15T21:21:33.767

Reputation: 21

Answers

1

You would have to add the Python path to the system variable PATH or create a new system variable that is something like PYTHONDIR and have that point to the Python directory and then call the python.exe with %PYTHONDIR%\python.exe

PS. Why don't you use the Firefox Add-On IE Tabs?

Winter Faulk

Posted 2012-11-15T21:21:33.767

Reputation: 678

I solve the IE problem with Google Chrome Frame - It lets (l)users continue to use IE, but will transparently render a site within IE using the Chrome engine whenever the site asks for it. My websites demand this plugin of my IE users. A clever solution to a horrible problem. – James T Snell – 2012-11-15T23:12:10.567

The python install directory is in PATH, which is why using cmd.exe works. Without a full path to the exe though, it can't be found.

I will try IE tabs when I'm at the office tomorrow. – RabbleRooster – 2012-11-16T03:05:11.553

@RabbleRooster, that's strange because it works for me. I just created a test on my system doing what you are doing and it worked. – Winter Faulk – 2012-11-16T16:49:08.953

What was the syntax that leveraged the PATH? This doesn't work for me: @=""python.exe" "C:\browser_chooser.pyw" "%1"" – RabbleRooster – 2012-11-16T20:32:27.233

@="python.exe "c:\test.py" "%1"" – Winter Faulk – 2012-11-16T22:16:51.887

@Sane Huh. No dice. I even tried to remove the quotes around python.exe in case it made a difference. Thanks for trying that out, though. – RabbleRooster – 2012-11-16T22:50:10.280

0

Can you use a .lnk file (i.e., a filesystem shortcut) in the open\command field in the registry?  If so, create a shortcut that runs your cmd /c command and has the “Run” control set to “Minimized”:

shortcut properties window with Run = Minimized circled

cmd.exe will still run, but it won’t pop up a window; it’ll only display a taskbar button.

Scott

Posted 2012-11-15T21:21:33.767

Reputation: 17 653

0

If compiling without rewriting is an option, you can compile your python script into an exe with py2exe or pyinstaller- it'll compile it into a self contained exe and dlls you can use to run the application on its own with its own copy of python as a dll.

Journeyman Geek

Posted 2012-11-15T21:21:33.767

Reputation: 119 122

1

py2exe is pretty outdated. Probably better to use pyinstaller

– ernie – 2012-11-15T23:52:41.307

ooh, wasn't aware of that. Edited it into my answer, thanks – Journeyman Geek – 2012-11-15T23:54:30.053

Anything is an option; this is a convenience tool. If I couldn't find a better option, I was going to go this route. This script had been passed around the office a bit, and I liked that people could edit it easily to modify the rules if needed. – RabbleRooster – 2012-11-16T03:06:04.233