Reputation: 4559
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?
Upvotes: 3
Views: 1156
Reputation: 66709
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.
Upvotes: 0
Reputation: 82924
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.
Upvotes: 7
Reputation: 526473
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.
Upvotes: 8