tags:

views:

112

answers:

3

If I write a python script using only python standard libraries, using Python 2.6 will it work on all Operating Systems as long as python 2.6 is installed?

A: 

yes, unless you are using modules that are os dependent.

Edit : My reply seemed short and not too the point based on comments

I am not addressing portable programming in general.

That would mean taking care of binary data packing and manipulation, c extension issues, paths as in windows/unix, "\r\n" in windows text and many others.

But with regard to portability of the python modules, there is no question.

They are portable.

How ever, there are modules that are available on specific platform only and if you use them, then your portability will be curtailed.

pyfunc
-1: Isn't that just like saying that everything will work except for the bits that won't?
Lasse V. Karlsen
@Lasse V. Karlsen : Not really. I might have made a pithy, short reply. But it meant exactly the same as answer provided by Amber above. There are os dependent modules which if you use in the scripts, it will not make it portable across other OS. These OS specific available modules are well documented.
pyfunc
Try the "magic number 26" example in my answer -- it uses only built-in functions.
John Machin
@John Machin: +1 Thats perfect and I was not even looking in the direction. In fact, The question asked about portability of using python library in general and not portable programming in general. That was my distraction.
pyfunc
+9  A: 

Depends. There are a few parts of the Python standard libraries that are only available on certain platforms. These parts are noted in the Python documentation.

You also need to be careful of how you handle things like file paths - using os.path.join() and such to make sure paths are formatted in the right way.

Amber
Upvoted for mentioning paths.
kindall
+6  A: 

You need to be careful when you are reading binary files. Always use 'rb', 'wb', etc file opening modes. You can get away with 'r' etc on Unix/Linux/etc, but it really matters on Windows. Unintuitively, CSV files are binary.

Instructive exercise: work out why this code produces 26 on Windows instead of the 128 that it would produce on a non-Windows box:

>>> s = ''.join(map(chr,range(128)))
>>> len(s)
128
>>> f = open('junk.txt', 'w')
>>> f.write(s)
>>> f.close()
>>> len(open('junk.txt').read())
26

Avoid hard-coding file paths.

Don't assume that you can splat unicode (or utf8-encoded unicode) at the console and have it rendered legibly or at all.

Some Python modules are not automatically installed on some Linux distros ... you need a separate "dev" package.

Not exactly an operating system problem, but some operating systems run on bigendian boxes so if you are doing any work writing/reading binary formats, you need to take endianness into account.

John Machin
Hint (for the exercise): try manually walking through the input, and take a look at the ASCII table.
Pablo Alejandro Costesich