Great portability list in Python

I thought it would be nice to make a list of things to consider when creating a portable Python application. There are many subtle β€œtouches” in portability, which are revealed only through experience and rigorous testing; there must be some list addressed to the more common.

Please send a receipt message (with correction) for comment.

+4
source share
7 answers

If you work with binary file formats in Python, note that the struct and array modules use machine-dependent size and entity. struct can be used portable, always using < or > in the format string. array cannot. It will probably be portable for byte arrays, but the documentation does not offer such a guarantee.

+4
source

'Universal newline support' (as described in PEP278 ) may come in handy for mobility reasons.
This ensures that python code only gets \n :

 christophe@orion :~$ printf 'testing pep278\r\n' > test.txt christophe@orion :~$ python Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> open('test.txt').read() 'testing pep278\r\n' >>> open('test.txt','U').read() 'testing pep278\n' 
+3
source

Get rid of the syntactic side of things, I think that the most important thing to pay attention to is that when people think about python, they may not think about all the libraries that it consists in.

Many python packages depend on C libraries, which may or may not be platform compatible. In addition, Python runs on Java through Jython and .Net through IronPython. If the libraries are not written in pure python, in many cases they will not work for anything other than a C-based python version.

+2
source

Some modules are not cross-platform. Two that come to mind are curses (Linux) and msvcrt (Windows). To fix this simple task is simply not to use them, but to find an alternative.

+2
source

Unix vs. Windows: using Popen in a subprocess module will differ in behavior when shell=True . I will not propose corrections because the discussion here is so well described, but this is one of those areas that may unexpectedly bite you.

+2
source

I will begin:

Windows uses backslash for path separators -> '\'

Unix uses slashes for path separators -> '/'

The os module comes with os.sep , which contains the path separator for the current platform on which the script is running. Use os.sep instead of fast forward or rewind. os.path.join will connect to two or more path components in this way.

+1
source

There are slight differences in UCS2 and UCS4 (e.g. Windows and Linux), Python builds due to errors, conflicting or outdated standards, etc.

Example: Problem 3297

unicodetest.py :

 #-*- coding: utf-8 -*- print 'Result:', u'𐄣' == u'\U00010123' print 'Len:', len(u'𐄣'), len(u'\U00010123') print 'Repr:', repr(u'𐄣'), repr(u'\U00010123') 

Output (Python 2.6, Linux):

 Result: False Len: 2 1 Repr: u'\ud800\udd23' u'\U00010123' 
+1
source

All Articles