python: cross-platform?

Noah Spurrier noah at noah.org
Tue May 7 14:53:47 EDT 2002


Check the docs. They tell what modules work on which systems.
You can put an exception wrapper around your entire script
and trap "exception ImportError". That will get thrown if you
try to import a module that does not exist. This way you can quickly
build up a list of troublesome modules during development.

Some modules may exist on many platforms, but may not bahave exactly the
same way.
This is a difficult to solve. I found the higher-level calls in Python to be
pretty consistent.
Generally POSIX functions behave the same.
The lower the level of the call, the less sure you can be.
You must create a test suite to test functions for which you are unsure.
This is the nature of the world. Even with Java the threads didn't behave
the same way on all systems (I'm not sure if they got around this problem in
the latest versions with green threads or something).

You can manually check for a specific os or a group of os's by
using os.name. That will give you the name of the OS for which
the os module was built (which is the name of the os that Python is running
under).
The following names have currently been registered:
    'posix', 'nt', 'dos', 'mac', 'os2', 'ce', 'java', 'riscos'
I hate writing code that checks for an OS, but at least it's easy to read
and easy to maintain.

Finally, when dealing with file be sure to use os.path to manipulate file
names.
And look at os.sep and os.pathsep if you need to know exactly what
characters are used to build file paths. Generally you don't need to know
if you use os.path module. Always, always, always use os.path.
But even in my own scripts I find that I sometimes thoughtless hardcode
something like:
    fullpath = '/tmp/' + filename
os.abspath() is always helpful in cleaning up path names.

Yours,
Noah


----- Original Message -----
From: "Eric Texier" <erict at millfilm.co.uk>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Tuesday, May 07, 2002 8:48 AM
Subject: python: cross-platform?


> Is there a guide line for writing cross-platform code in python. I am
> dealing with only one os for now but I would like to be sure that I am
> using only cross platform code.
> Thanks in advance
>
>






More information about the Python-list mailing list