Installing python on demand? (Windows)
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_MACHINESOFTWAREPythonPythonCore 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:
- You might directly link to the download instead of a custom html file (then you can use the start command)
- 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?



on 2 October 2009 at 10:30 Kevin Horn said …
I would do something like this using AutoRun rather than batch:
- it's tiny
- can be compiled into small, single executable files
- has support for registry reading, url downloading, etc
- no dependencies! (other than being on Windows)
on 2 October 2009 at 11:17 Raphael said …
I would argue against using .NET - in case it is not already installed (which might happen) the user would first have to download the huge .NET package before downloading Python. A simple, small C program should be perfect.
on 2 October 2009 at 11:54 Gerdus said …
NSIS is definitely the answer it can even download python for you with a nice progress bar run the python msi silently, etc.
Also how to you get to 5mb? I have gotten a python program down to 2Mb compressed using 7z self extract exe.
on 2 October 2009 at 12:10 rgz said …
There actually quite popular and readily available deployment tools for windows. There's MS .msi platform and the widely used NullSoft installer.
I'd really recommend Nullsoft's solution, it's free as far as I know.
on 2 October 2009 at 13:39 Eric said …
If you're serious about the batch process, wget from the UnxUtils project can download a python installer, saving a step.
on 3 October 2009 at 00:03 Kerim Mansour said …
Interesting ideas:
-autorun. As far as i know it is only executed when devices are inserted and not in specific directories. Problems with trojans and virsus make me sceptical about it. And in Windows 7 it seems to be disabled for anythink but DVD and CD drives.
-NET. The reason why i proposed this is that any windows > XP has .net installed as far as i know. The number of XP installations without .NET (in my experience) tends to be very low.
But i think you are correct. A small C exe might do the job even better.
-NSIS: I presume you mean the DownloadPlugin in NSIS. It seems to be fixed concerning the URL it downloads from (so you wouldnt be able to always download the most current version). I might err however (don't know much about NSIS)
-wget: had thought about that already but there is one disadvantage... you must install or distribute wget ;)
My ideal:
a small program checking the existence of python, downloading the most current version of it, installing it, restarting the original python module.
one might even think of combining this with an "update" mechanism for python itself.
on 3 October 2009 at 04:00 Koen said …
The question is, how does a program obtain the version number of the latest Python?
You can do everything you want with NSIS, it is a full-blown programming language for installers, with low footprint.
on 3 October 2009 at 10:56 Martin Meinhardt said …
I think, Kevin Horn mean AutoIt, not AutoRun (see http://www.autoitscript.com + http://www.autoitscript.com/autoit3/d...).
Another thing: found an interesting "launcher.c" in Distribute/Setuptools. I don't know C(++), but it might be worth looking at.
What I'm wishing for is a small neet program, that creates a EXE from my PY-Script, without bundling all the Python-Lib (as if I would start the script with "python myscript.py" in console).
Yes, there is ExeMaker (http://effbot.org/zone/exemaker.htm), but that is EXE + PY. An EXE, with "embedded" PY-Script, extracted at runtime into memory and started... Custom ICO and it would be perfect.
But let's start small with the current idea.
on 4 October 2009 at 02:50 Kerim Mansour said …
"The question is, how does a program obtain the version number of the latest Python?"
Thats simple. Either you go to "http://python.org/download/" and take the current production versions (follow the links etc.) OR you parse "http://python.org/download/releases/" OR you just Scan the ftp directory of python org and split the directorynames with ('.'). You get the three digits in a second.
Choosing the last version of a major version is simple.
Sadly at least the first 2 solutions depend on the webpage layout. It would be neat if there is a "general" interface for this question.