如何使 python 或 perl 脚本同时移植到 Linux 和 Windows?

发布于 2024-09-04 19:44:07 字数 189 浏览 4 评论 0原文

我想知道如何使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不如归去 2024-09-11 19:44:07

Windows 将忽略 shebang(毕竟,这是一条注释);在 Windows 中,您需要将 .py 扩展名与注册表中的 Python 可执行文件关联起来,但您完全可以将 shebang 保留在打开状态,在那里它是完全无害的。

有许多零碎的东西是特定于平台的(许多只存在于 Unix 上,msvcrt 仅存在于 Windows 上),所以如果你想便携,你应该放弃这些;有些略有不同(例如 subprocess.Popenmmap 的详细精确行为)——这些都是非常高级的东西,文档将指导您实现这一点。如果您正在执行(通过子进程或其他方式)外部命令,您最好确保它们存在于两个平台上,当然,或者检查您所在的平台并在每个平台上使用不同的外部命令案件。

请记住始终使用 /而不是 \ 作为路径分隔符(正斜杠适用于两个平台,反斜杠仅适用于 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 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.

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...

永不分离 2024-09-11 19:44:07

确保您不将文件和目录作为字符串处理,而只是将它们之间用斜杠连接起来。 Perl:

$path = File::Spec->catfile("dir1", "dir2", "file")

请记住,Windows 有卷:

($volume, $path, $file) = File::Spec->splitpath($full_path);
@directories = File::Spec->splitdir($path);

运行其他程序时,尽量避免涉及 shell。在 Perl 中,当您使用 system 函数运行命令时,您很容易出错:

$full_command = 'C:\Documents and Settings/program.exe "arg1" arg2'; # spaces alert!
system($full_command);

相反,您可以使用列表作为参数来运行系统:可执行文件和参数是单独的字符串。在这种情况下,shell 不会参与其中,并且您不会遇到有关 shell 转义或文件名中的空格的麻烦。

system('C:\Documents and Settings/program.exe', 'arg1', 'arg2');

perlport 手册中记录了一堆可移植性问题。

Make sure you don't handle files and directories as strings and simply concatenate them with a slash in between. Perl:

$path = File::Spec->catfile("dir1", "dir2", "file")

Remember that Windows has volumes:

($volume, $path, $file) = File::Spec->splitpath($full_path);
@directories = File::Spec->splitdir($path);

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:

$full_command = 'C:\Documents and Settings/program.exe "arg1" arg2'; # spaces alert!
system($full_command);

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.

system('C:\Documents and Settings/program.exe', 'arg1', 'arg2');

There's a bunch of portability nits documented in the perlport manual.

莫相离 2024-09-11 19:44:07

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.

一抹淡然 2024-09-11 19:44:07

我不知道如何评论 Python 解决这个问题的方法,所以我不会。

Perl 中的大多数功能都可以正常工作。有一些问题很容易避免。

以下是我在使用 Win32 Perl 的这些年中遇到的一些事情:

  • 使用 open 的 3 个参数形式。两个参数形式可能会出现路径中空格的问题。 (无论如何,您应该已经这样做了。)
  • 确保使用模块时大小写正确。 use warnings; 看起来可以工作,但实际上会失败。
  • select 仅适用于 Windows 下的实际套接字。您不能在任何其他类型的手柄上使用它。
  • 使用 File::Spec 管理文件路径。
  • 当您打开文件句柄时,CRLF 将在读取句柄时自动转换为 LF 行结尾。写入时 LF 更改为 CRLF。如果您想避免这种情况,请在句柄上使用 binmode 来阻止翻译。
  • 如果需要通过 shell 传递参数,请在每个参数两边加上双引号。这将防止由于文件名中的空格而导致的错误。

有关各个函数的更多信息,请参阅 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:

  • Use 3 argument form of open. The two argument form can have problems with spaces in paths. (You should already be doing this anyway.)
  • Make sure that the case is correct when you use a module. 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.
  • Use File::Spec to manage paths to files.
  • When you open a file handle CRLF will automatically be converted to LF line endings as the handle is read. LF is changed to CRLF on write. If you want to avoid this, use binmode on the handle to prevent the translation.
  • If you need to pass arguments through a shell, put double quotes around each argument. This will prevent errors due to spaces in file names.

See perlport for more information on individual functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文