简体   繁体   English

使用 execv 运行时指定 Python 解释器的跨平台方式

[英]Cross-platform way to specify Python interpreter when running with execv

I am currently running a Python scripts both on Linux and Windows 7. The file is executed in an execv style with which I mean that the interpreter is defined in the beginning of the file in a command.我目前正在 Linux 和 Windows 7 上运行 Python 脚本。该文件以 execv 样式执行,我的意思是解释器是在命令的文件开头定义的。

In Windows system, the interpreter specification is:在 Windows 系统中,解释器规范为:

#!C:\Python26\python.exe

However in Linux this needs to be但是在 Linux 中,这需要

#!/usr/bin/python

I would like to run this script in both systems without having to change this line again and again.我想在两个系统中都运行这个脚本,而不必一次又一次地更改这一行。

I have tried out the following:我已经尝试了以下内容:

#!C:\Python26\python.exe
#!/usr/bin/python

as well as:也:

#!C:\Python26\python.exe;/usr/bin/python

So: is there any way I could specify multiple interpreters?那么:有什么方法可以指定多个解释器?

Depending on what you're trying to do, this might be a bit heavy-weight, but 0install can run your program will the appropriate Python interpreter for your platform.根据您要执行的操作,这可能有点重,但 0install 可以运行您的程序,并为您的平台提供适当的 Python 解释器。 In your program's XML description, do something like this (eg if you want Python >= 2.6, < 3):在你的程序的 XML 描述中,做这样的事情(例如,如果你想要 Python >= 2.6, < 3):

<command name="run" path="myprog.py">
  <runner interface="http://repo.roscidus.com/python/python">
    <version not-before="2.6" before="3"/>
  </runner>
</command>

See: http://www.0install.net/local-feeds.html见: http ://www.0install.net/local-feeds.html

This will also make 0install download a suitable version of Python if the user doesn't have it already.如果用户还没有它,这也将使 0install 下载合适的 Python 版本。

Note that you may want to do this even if you're only targetting Linux, because with Python 3 there is no single #!请注意,即使您只针对 Linux,您也可能希望这样做,因为在 Python 3 中没有单一的 #! line that works on all platforms (some platforms, eg Arch, require "python2" not "python", while others, eg Debian, don't provide "python2", only "python").适用于所有平台的行(某些平台,例如 Arch,需要“python2”而不是“python”,而其他平台,例如 Debian,不提供“python2”,只提供“python”)。

#!/usr/bin/env python

That will call the env program to search your PATH for a Python executable.这将调用env程序在您的 PATH 中搜索 Python 可执行文件。

If you need to ensure a specific version of Python you can do eg:如果您需要确保特定版本的 Python,您可以执行以下操作:

#!/usr/bin/env python3.11

Is there any way I could specify multiple interpreters ?有什么办法可以指定多个解释器?

You don't need to.你不需要。 On Windows (at least as long as you don't have CygWin or similar installed), the Shebang line is treated as a normal Python comment;在 Windows 上(至少只要您没有安装 CygWin 或类似软件),Shebang 行被视为普通的 Python 注释; that means, it is ignored.这意味着,它被忽略了。 Windows knows that it should run .py and .pyw files with the Python interpreter, because it is told that upon installation of Python. Windows 知道它应该使用 Python 解释器运行.py.pyw文件,因为它在安装 Python 时被告知。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM