简体   繁体   English

如何使Python脚本可移植到具有不同位置的解释器的机器上?

[英]How to make Python script portable to machines with interpreters in different locations?

I'm sure this is well documented somewhere, but I can't find it! 我敢肯定这在某处有很好的记录,但我找不到它! I want to make my scripts portable to machines that may not have their Python interpreters in the same location. 我想使脚本可移植到可能在同一位置没有Python解释器的计算机。 For that reason, I thought that I could just code the first line as #!python3 rather than with the absolute path to the interpreter, like #!/usr/local/bin/python3 . 因此,我认为我可以将第一行编码为#!python3而不是使用解释器的绝对路径,例如#!/usr/local/bin/python3

No doubt most of you understand why this doesn't work, but I have no idea. 毫无疑问,你们中的大多数人都理解为什么这行不通,但是我不知道。 Although my lab mates aren't complaining about having to recode my scripts to reflect the absolute path to the interpreter on their own machines, this seems like it shouldn't be necessary. 尽管我的实验室同事没有抱怨必须重新编码我的脚本以反映到他们自己计算机上解释器的绝对路径,但这似乎没有必要。 I'd be perfectly happy with a response providing a link to the appropriate documentation. 我很高兴收到提供相应文档链接的回复。 Thanks in advance. 提前致谢。

The path given after #! #!之后给出的路径#! in the shebang line is an absolute path, so simply python3 does not work. 在shebang行中是绝对路径,因此python3根本不起作用。 You should use 你应该用

#!/usr/bin/env python3

to look up python3 in the PATH on a POSIX machine. 在POSIX机器上的PATH查找python3 Of course this will only find the Python interpreter if it is in some directory given in the PATH environment variable. 当然,只有在PATH环境变量给定的某个目录中,它才会找到Python解释器。

env is a program that handles these sort of things. env是处理这类事情的程序。 You should pretty much always use something like #! /usr/bin/env python3 您应该几乎总是使用类似#! /usr/bin/env python3东西#! /usr/bin/env python3 #! /usr/bin/env python3 as your shebang line rather than specifying an absolute path. #! /usr/bin/env python3作为您的shebang行,而不是指定绝对路径。

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

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