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?

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

1 Comment

Hint (for the exercise): try manually walking through the input, and take a look at the ASCII table.
0

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.

4 Comments

-1: Isn't that just like saying that everything will work except for the bits that won't?
@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.
Try the "magic number 26" example in my answer -- it uses only built-in functions.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.