Platform independent code?
larry.bates at websafe.com`
Sat Jun 14 16:36:48 CEST 2008
> I have read that Python is a platform independent language. But on this
> it seems that making a python script executable is platform dependant:
> 2.2.2 Executable Python Scripts
> On BSD'ish Unix systems, Python scripts can be made directly executable,
> like shell scripts, by putting the line
> #! /usr/bin/env python
> (assuming that the interpreter is on the user's PATH) at the beginning of
> the script and giving the file an executable mode. The "#!" must be the
> first two characters of the file. On some platforms, this first line must
> end with a Unix-style line ending ("\n"), not a Mac OS ("\r") or Windows
> ("\r\n") line ending. Note that the hash, or pound, character, "#", is used
> to start a comment in Python.
> The script can be given an executable mode, or permission, using the chmod
> $ chmod +x myscript.py
> Are there any guidelines (API'S) that gurantees that the python code will be
> platform independent?
Generally you have to stay away from platform "dependent" constructs. Esamples:
1) Use os.path methods everywhere in your code so you won't need to worry about
os.path.sep (slash or backslash).
2) Use lowest common denominator when naming files to make your scripts work
3) Stay away from os-specific calls/interfaces when possible. Manipulating file
permissions, etc is always os-specific.
4) Use config files instead of relying on registry in Windows.
5) Stay away from code that depends on 32/64 bit objects or endian-ness of
storage (struct objects). This is machine specific not OS specific but still
6) If you have GUI, use something like wxWindows which provides cross platform
7) Be careful using non-Python libraries (C-libraries) should be done with care
to make sure that the library is available for all target operating systems.
8) using os.system or the subsystem module must be done with much care and
probably won't be cross platform compatible.
Hope the suggestions help.
More information about the Python-list