如何使python或perl脚本可移植到Linux和Windows? [英] how to make a python or perl script portable to both linux and windows?

查看:166
本文介绍了如何使python或perl脚本可移植到Linux和Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使python脚本可移植到Linux和Windows?

I was wondering how to make a python script portable to both linux and windows?

我看到的一个问题是shebang。

One problem I see is shebang. How to write the shebang so that the script can be run on both windows and linux?

除了shebang我还应该知道其他问题吗?

Are there other problems besides shebang that I should know?

perl脚本的解决方案是否相同?

Is the solution same for perl script?

谢谢!!

推荐答案

Windows只会忽略shebang(毕竟是注释);在Windows中,您需要将 .py 扩展名与注册表中的Python可执行文件相关联,但是您可以完全放开shebang,它在那里完全是无害的。

Windows will just ignore the shebang (which is, after all, a comment); in Windows you need to associate the .py extension to the Python executable in the registry, but you can perfectly well leave the shebang on, it will be perfectly innocuous there.

有很多点是特定于平台的(许多仅在Unix上存在, msvcrt 仅在Windows上存在)因此如果您想携带便携式设备,则应该放弃这些;有些有些微妙的不同(例如 subprocess.Popen mmap 的详细精确行为)-都相当先进资料和文档将指导您到那里。如果您正在执行(通过子进程或其他方式)外部命令,则最好确保它们同时存在于两个平台上,或者检查您所处的平台并

There are many bits and pieces which are platform-specific (many only exist on Unix, msvcrt only on Windows) so if you want to be portable you should abstain from those; some are subtly different (such as the detailed precise behavior of subprocess.Popen or mmap) -- it's all pretty advanced stuff and the docs will guide you there. If you're executing (via subprocess or otherwise) external commands you'd better make sure they exist on both platforms, of course, or check what platform you're in and use different external commands in each case.

请记住始终使用 / \ ,作为路径分隔符(正斜杠在两个平台上均有效,反斜杠仅适用于Windows),并且对于要打开的每个文件是二进制文件还是二进制文件都应谨慎文本。

Remember to always use /, not \, as path separator (forward slash works in both platforms, backwards slash is windows-only), and be meticulous as to whether each file you're opening is binary or text.

我想就是这样...

这篇关于如何使python或perl脚本可移植到Linux和Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆