Installing python on demand? (Windows)

02.10.2009 by kerim in autoinstall | python

After the discussion here, i just spent some hour thinking about an initial (first, rudimentary, basic, not satisfying, almost bad) solution for the problem (that works only for windows) on how to deal with python code that should run on a system that has no python.

My first idea was to write a python program that gets "compiled" using py2exe. It would check for the existence of a python distribution and if none is present ask the user which one he would like to download. Then he would download it, get it installed and the program he intended to run in the first place be started. The whole thing has just one big problem. You actually have to distribute 5 MB for the compiled initial exe with every program you want to distribute.

My next thought was to write something similar as a batch. The following stuff works under windows (at least my WinXP). Create a batch file and enter the following:

@echo off
setlocal
set regpath=HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\
reg query "%regpath%"
if errorlevel 1 (
    rundll32 url.dll,FileProtocolHandler "index.html"
    rem start "index.html"
    EXIT /B /0
)

MYPROGRAM.py

Create an html file like this:

<html>
<body>
<H1>Customize this html</H1>
Sorry, but you can't start INSERT YOUR PROGRAM HERE without the Python Runtime Environment.
Please go to <a href="http://python.org/download/"> the Python Webpage and download it.</a>
    <p>After installation you need to run INSERT YOUR PROGRAM HERE again</p>
</body>
</html>

When distributing your code you should have the batch file, the python code and the html file in one directory. Your installer should create a link to the batch file. Now what happens?

1) The batch file checks the windows registry for any python installation. Python installations are located at HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\ in the registry)

2) If none is found it opens the browser with some html explaining the problem and linking to the python.org site. The user must download a distribution, install it and start the batch again (now this IS ugly)

2b) if something is found it starts the python script

If you happen to have python installed and cant get the error to happen just change "PythonCore" into "PythonsCore". That key doesn't exist in the registry, so the the error should fire.

Alternatives to above that come to my mind currently:

  1. You might directly link to the download instead of a custom html file (then you can use the start command)
  2. Instead of this rather user unfriendly batch file one might think about a program written in .Net which would require just some 200 KB and do the stuff automatically and more user friendly.

I think it IS vital that people can distribute python code without the user needing to manually install stuff. My take would be alternative 2. Any volunteers?


comments powered by Disqus