简体   繁体   English

不同操作系统上的Python程序

[英]Python programs on different Operating Systems

如果我只使用python标准库编写python脚本,那么只要安装了python 2.6,使用Python 2.6就能在所有操作系统上运行吗?

Depends. 要看。 There are a few parts of the Python standard libraries that are only available on certain platforms. Python标准库的一些部分仅在某些平台上可用。 These parts are noted in the Python documentation. 这些部分在Python文档中有说明。

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. 您还需要注意如何处理文件路径之类的东西 - 使用os.path.join()等,以确保路径以正确的方式格式化。

You need to be careful when you are reading binary files. 在阅读二进制文件时需要小心。 Always use 'rb', 'wb', etc file opening modes. 始终使用'rb','wb'等文件打开模式。 You can get away with 'r' etc on Unix/Linux/etc, but it really matters on Windows. 你可以在Unix / Linux /等上使用'r'等,但它在Windows上真的很重要。 Unintuitively, CSV files are binary. 毫不直观地,CSV文件是二进制文件。

Instructive exercise: work out why this code produces 26 on Windows instead of the 128 that it would produce on a non-Windows box: 教学练习:找出为什么这个代码在Windows上产生26而不是在非Windows框上产生的128:

>>> 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. 不要假设您可以在控制台上展开unicode(或utf8编码的unicode)并使其清晰或完全呈现。

Some Python modules are not automatically installed on some Linux distros ... you need a separate "dev" package. 某些Linux发行版上没有自动安装某些Python模块......您需要一个单独的“dev”包。

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. 不完全是操作系统问题,但有些操作系统在bigendian框上运行,所以如果你正在编写/读取二进制格式,你需要考虑字节序。

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. 这意味着要处理二进制数据打包和操作,c扩展问题,windows / unix中的路径,windows文本中的“\\ r \\ n”以及许多其他内容。

But with regard to portability of the python modules, there is no question. 但是关于python模块的可移植性,毫无疑问。

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. 但是,有些模块仅在特定平台上可用,如果您使用它们,那么您的可移植性将受到限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python和不同的操作系统 - Python and different Operating Systems 如何在某些操作系统上禁用Python程序? - How can I disable use of Python programs on certain operating systems? 不同操作系统中的VirtualEnv - VirtualEnv in different operating systems openpyxl 在不同操作系统中的行为不同 - openpyxl act differently in different Operating systems Python上的select模块的不同功能(例如select(),poll(),epoll())在哪些操作系统上可用? - On what operating systems are the different functions of the select module in Python, like select(), poll(), epoll() available? Python读写文件时,如何在不同的操作系统上获取相同的路径? - How to get the same path on different operating systems when reading and writing files in Python? 在Python中不同操作系统下不改变路径读取Excel文件 - Read an Excel file without changing path under different operating systems in Python 用于查询多个操作系统版本的python脚本 - python script for enquiring the version of multiple operating systems Python 3导入操作系统之间的行为差​​异 - Python 3 import behaviour differences between operating systems input() 函数在不同的操作系统上以不同的方式处理数据类型? - input() function handling data types differently across different Operating Systems?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM