如何使 python 或 perl 脚本同时移植到 Linux 和 Windows?
我想知道如何使 python 脚本同时移植到 linux 和 windows 上?
我看到的一个问题是 shebang。如何编写shebang使得脚本可以在windows和linux上运行?
除了 shebang 之外,还有其他我应该知道的问题吗?
perl 脚本的解决方案是否相同?
谢谢和问候!
I was wondering how to make a python script portable to both linux and windows?
One problem I see is shebang. How to write the shebang so that the script can be run on both windows and linux?
Are there other problems besides shebang that I should know?
Is the solution same for perl script?
Thanks and regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Windows 将忽略 shebang(毕竟,这是一条注释);在 Windows 中,您需要将
.py
扩展名与注册表中的 Python 可执行文件关联起来,但您完全可以将 shebang 保留在打开状态,在那里它是完全无害的。有许多零碎的东西是特定于平台的(许多只存在于 Unix 上,
msvcrt
仅存在于 Windows 上),所以如果你想便携,你应该放弃这些;有些略有不同(例如subprocess.Popen
或mmap
的详细精确行为)——这些都是非常高级的东西,文档将指导您实现这一点。如果您正在执行(通过子进程或其他方式)外部命令,您最好确保它们存在于两个平台上,当然,或者检查您所在的平台并在每个平台上使用不同的外部命令案件。请记住始终使用
/
,而不是\
作为路径分隔符(正斜杠适用于两个平台,反斜杠仅适用于 Windows),并且请仔细确定您打开的每个文件是二进制文件还是文本文件。我想就是这样...
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.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 ofsubprocess.Popen
ormmap
) -- it's all pretty advanced stuff and the docs will guide you there. If you're executing (viasubprocess
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.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.I think that's about it...
确保您不将文件和目录作为字符串处理,而只是将它们之间用斜杠连接起来。 Perl:
请记住,Windows 有卷:
运行其他程序时,尽量避免涉及 shell。在 Perl 中,当您使用
system
函数运行命令时,您很容易出错:相反,您可以使用列表作为参数来运行系统:可执行文件和参数是单独的字符串。在这种情况下,shell 不会参与其中,并且您不会遇到有关 shell 转义或文件名中的空格的麻烦。
perlport 手册中记录了一堆可移植性问题。
Make sure you don't handle files and directories as strings and simply concatenate them with a slash in between. Perl:
Remember that Windows has volumes:
When running other programs, try to avoid involving the shell. In Perl, when you run a command with the
system
function, you can easily get it wrong with:Instead, you can run system with a list as argument: the executable and the arguments being separate strings. In that case, the shell doesn't get involved and you don't get into trouble regarding shell escaping or spaces in file names.
There's a bunch of portability nits documented in the perlport manual.
shebang 行将被 Perl 或 Python 解释为注释。唯一赋予它特殊含义的是 UNIX/Linux shell;它在 Windows 上会被忽略。 Windows 知道使用哪个解释器来运行文件的方式是通过注册表中的文件关联,这是一种完全不同的机制。
The shebang line will be interpreted as a comment by Perl or Python. The only thing that assigns it a special meaning is the UNIX/Linux shell; it gets ignored on Windows. The way Windows knows which interpreter to use to run the file is through the file associations in the registry, a different mechanism altogether.
我不知道如何评论 Python 解决这个问题的方法,所以我不会。
Perl 中的大多数功能都可以正常工作。有一些问题很容易避免。
以下是我在使用 Win32 Perl 的这些年中遇到的一些事情:
open
的 3 个参数形式。两个参数形式可能会出现路径中空格的问题。 (无论如何,您应该已经这样做了。)use warnings;
看起来可以工作,但实际上会失败。select
仅适用于 Windows 下的实际套接字。您不能在任何其他类型的手柄上使用它。File::Spec
管理文件路径。CRLF
将在读取句柄时自动转换为LF
行结尾。写入时LF
更改为CRLF
。如果您想避免这种情况,请在句柄上使用binmode
来阻止翻译。有关各个函数的更多信息,请参阅 perlport。
I don't know enough to comment on Python approaches to this problem, so I won't.
Most things in Perl will just work. There are a few gotchas that are easy to avoid.
Here are a few things I have come across in the years I've been working with Win32 Perl:
open
. The two argument form can have problems with spaces in paths. (You should already be doing this anyway.)use Warnings;
will appear to work, but in reality it will fail.select
only works on actual sockets under Windows. You can't use it on any other sort of handle.File::Spec
to manage paths to files.CRLF
will automatically be converted toLF
line endings as the handle is read.LF
is changed toCRLF
on write. If you want to avoid this, usebinmode
on the handle to prevent the translation.See perlport for more information on individual functions.