2024-04-28 11:48:21 发布
网友
我认为在使Python应用程序可移植时,编译一个需要注意的事项列表是个好主意。在可移植性方面有很多微妙的“问题”,只有通过经验和彻底的测试才能发现;需要有某种列表来解决更常见的问题。在
请张贴一个gotcha(及其修复)每条评论。在
如果您在Python中处理二进制文件格式,请注意struct和array模块使用依赖于机器的大小和endianness。struct可以通过在格式字符串中始终使用<或{}来进行移植。array不能。它可能对于字节数组是可移植的,但是文档没有这样的保证。在
struct
array
<
由于可移植性的原因,'Universal newline support'(如PEP278中所述)可以派上用场。 {python{1>确保它只得到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'
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'
有些模块不是跨平台的。想到的两个是curses(Linux)和{}(Windows)。解决这个简单问题的方法就是不使用它们,而是找到一种替代方法。在
curses
如果您在Python中处理二进制文件格式,请注意}来进行移植。
struct
和array
模块使用依赖于机器的大小和endianness。struct
可以通过在格式字符串中始终使用<
或{array
不能。它可能对于字节数组是可移植的,但是文档没有这样的保证。在由于可移植性的原因,'Universal newline support'(如PEP278中所述)可以派上用场。
{python{1>确保它只得到
有些模块不是跨平台的。想到的两个是}(Windows)。解决这个简单问题的方法就是不使用它们,而是找到一种替代方法。在
curses
(Linux)和{相关问题 更多 >
编程相关推荐