如何找到 Python 在 Unix 上的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4373428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find where Python is located on Unix?
提问by Adam
I'm working on a new server for a new workplace, and I'm trying to reuse a CGI script I wrote in Python earlier this year. My CGI script starts off with
我正在为一个新的工作场所开发一个新服务器,我正在尝试重用我今年早些时候用 Python 编写的 CGI 脚本。我的 CGI 脚本开始于
#!/local/usr/bin/python
But when I run this on the new server, it complains that there's no such folder. Obviously Python's kept in a different place on this box, but I've got no idea where.
但是当我在新服务器上运行它时,它抱怨没有这样的文件夹。显然 Python 保存在这个盒子的不同位置,但我不知道在哪里。
I haven't done much unix before, just enough to get around, so if there's some neat trick I should know here I'd appreciate it :)
我以前没有做过太多的 unix,刚好够用,所以如果有一些巧妙的技巧我应该在这里知道,我会很感激:)
Thanks!
谢谢!
采纳答案by John Kugelman
For this very reason it is recommend that you change your shebang line to be more path agnostic:
出于这个原因,建议您将 shebang 行更改为与路径无关:
#!/usr/bin/env python
See this mailing list messagefor more information:
有关更多信息,请参阅此邮件列表消息:
Consider the possiblities that in a different machine, python may be installed at
/usr/bin/pythonor/bin/pythonin those cases,#!/usr/local/bin/pythonwill fail. For those cases, we get to call theenvexecutable with argument which will determine the arguments path by searching in the$PATHand use it correctly.(
envis almost always located in/usr/bin/so one need not worry thatenvis not present at/usr/bin.)
考虑在不同的机器上安装 python
/usr/bin/python或/bin/python在这些情况下#!/usr/local/bin/python会失败的可能性。对于这些情况,我们可以env使用参数调用可执行文件,该参数将通过在 中搜索$PATH并正确使用来确定参数路径。(
env几乎总是位于,/usr/bin/所以不必担心env不在/usr/bin。)
回答by Falmarri
# which python
/usr/local/bin/python
Update:
更新:
I misread. Replace your header with
我看错了。将您的标题替换为
#!/usr/bin/env python
#!/usr/bin/env python
This will pull in the python location from the user that runs the script's environmental settings
这将从运行脚本环境设置的用户那里拉入 python 位置
回答by icyrock.com
Try:
尝试:
which python
in a terminal.
在一个终端。
回答by SiegeX
Try: which pythonor whereis python
尝试:which python或whereis python
回答by Josh Lee
The proper way to solve this problem is with
解决这个问题的正确方法是
#!/usr/bin/env python
which allows for the use of a binary in the PATH in a shebang.
这允许在shebang的PATH中使用二进制文件。
回答by Yuda Prawira
It is a good idea to use backticks for header Python script:
对头 Python 脚本使用反引号是个好主意:
`which python`

