ANN: Python Test Environment

Michael Foord Michael.Foord at
Wed Dec 15 13:30:31 CET 2004

Well sort of....... Highly experimental - I'm interested in ways of 
improving this.

I've created a script that will build a 'test environment'. Windoze(tm) 
only as it uses py2exe.

It scans your Python\Lib folder (configurable) and builds a script that 
*fakes* an import of every module (along with some boilerplate). This, 
more or less, amounts to everything in the standard lib. There is then a 
normal to turn this into a python executable.

The result is an executable that will run any python script. It gives 
sensible values for sys.path, sys.argv and __file__.

This is useful for various purposes :

1) Easily have test environments for multiple versions of python - to 
test your scripts.
2) Run any python script on a machine without python installed.
3) Deploying several scripts using py2exe - one build fits all.

Usage :

testenv <python file> arg1 arg2...

Which should be the equivalent of :
python <python file> arg1 arg2...

Sample output :
(Built with Python 2.4 - then 2.3 - prints sys.version first)


D:\New Folder\testenv>testenv arg1 arg2 arg3
2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)]

sys.path = ['D:\\New Folder\\testenv\\', 'D:\\New 
Folder\\testenv', '
D:\\New Folder\\testenv']

sys.argv = ['D:\\New Folder\\testenv\\', 'arg1', 'arg2', 'arg3']

import Tkinter # succeeded

import dummylibrary # succeeded

D:\New Folder\testenv>


D:\Python Projects\modules in progress\py2exe-testenv\dist>testenv arg1
2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)]

sys.path = ['D:\\Python Projects\\modules in 
\\', 'D:\\Python Projects\\modules in 
 'D:\\Python Projects\\modules in progress\\py2exe-testenv\\dist']

sys.argv = ['D:\\Python Projects\\modules in 
progress\\py2exe-testenv\\dist\\tes', 'arg1', 'arg2']

import Tkinter # succeeded

import dummylibrary # succeeded

D:\Python Projects\modules in progress\py2exe-testenv\dist>


I'm sure this can be improved in lots of ways - but already useful to 
me. Comments and suggestions for improvements welcomed. Thanks go to 
Bruno Thoorens for his suggestions so far.

Currently has issues collecting 'sub-packages', but I *think* only the 
same issues that py2exe has. Extra modules can just be included 
somewhere on sys.path.



More information about the Python-announce-list mailing list