ms4py.blog

Python Portable on Windows

written on Wednesday, May 5, 2010

Introduction

In my project SITforC I created a setup for Windows with NSIS, so that the software is not dependent on a Python installation. This implied the requirement of a portable version of Python, because I don't want to use such tools like py2exe or pyInstaller. If you like the idea of a portable Python version and need one with your own dependencies, see this short guide to generate it of your own.

Note: You can download the current version of SITforC to get a portable Python version with some common dependencies like pygtk, numpy and matplotlib.

Instructions

Step 1: Copy the Python directory to a working dir.

Step 2: Copy the file python27.dll (in C:\Windows\SysWOW64 or in C:\Windows\system32) to the new Python directory.

Step 3: Copy the folder Microsoft.VC90.CRT to the Python directory. These are the necessary runtime components of Visual C++ Libraries ("vcredist"). If you have installed any edition of Visual Studio this folder can usually be found under C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\redist\x86.

Alternatively, you can get it here.

Step 4: Create a batch file for debugging. Adjust the path to the executable and change the scriptname if needed.

@echo off
SET PYTHONPATH=%CD%
Python27\python script.py
PAUSE

For pygtk support you can use the pygtk all in one package.

Step 5: Run the batch file. If you have Python packages with compiled files (.pyd), you could get an error like "*Application configuration not found".The error message provides the location of this pyd-file. Copy the folder Microsoft.VC90.CRT to this folder.

Repeat step 5 until you get no more errors and your script is working.

Note: This step may be unnecessary on current versions of Python.

Step 6: Now you can use a "Release" batch by removing the PAUSE command.

@echo off
SET PYTHONPATH=%CD%
Python27\python script.py

Start a GUI application with the START command and the pythonw executable:

@echo off
SET PYTHONPATH=%CD%
START Python27\pythonw script.py

Maybe you want to pass all commandline arguments to your Python script:

@echo off
SET PYTHONPATH=%CD%
Python27\python script.py %*

This entry was tagged portable and windows


blog comments powered by Disqus