Platform independent code?

bukzor workitharder at gmail.com
Sat Jun 14 13:21:24 EDT 2008


On Jun 14, 6:28 am, "saneman" <a... at ad.com> wrote:
> I have read that Python is a platform independent language.  But on this
> page:
>
> http://docs.python.org/tut/node4.html#SECTION004220000000000000000
>
> 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
> command:
>
> $ chmod +x myscript.py
>
> Are there any guidelines (API'S) that gurantees that the python code will be
> platform independent?

The only guarantee is testing it on both yourself. Some modules are
inherently os-dependant (much of the 'os' module for example), and
some constructs just don't work on all platforms (using
os.environ['LOGNAME'] or system('cat file') as examples).

If you stick to python code that doesn't touch the OS directly, you
won't (read: shouldn't) have a problem, but when you start interacting
with the OS directly you have to think hard about what you're doing.

--Buck



More information about the Python-list mailing list