Platform independent code?

S

saneman

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?
 
M

Matthieu Brucher

Hi,

Python is a platform independent language, period. You can always
excute a Python script with python script.py. Now, with Windows, you
can execute the script by doucle-clicking on it. With Linux, it's
different, you have to use the shebang line to execute a script with
the correct interpreter. But this has nothing to do with the fact that
Python is a platform independent language.
Some modules may not be available on all platform, for the answer to
this question, see the documentation of the module ;)

Matthieu
 
B

bukzor

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top