如何找到 Python 在 Unix 上的位置?

发布于 2024-10-06 12:31:41 字数 256 浏览 0 评论 0原文

我正在为新工作场所开发一台新服务器,并尝试重用今年早些时候用 Python 编写的 CGI 脚本。我的 CGI 脚本以

#!/local/usr/bin/python

但是当我在新服务器上运行它时,它抱怨没有这样的文件夹。显然,Python 保存在这个盒子上的不同位置,但我不知道在哪里。

我以前没有做过太多的unix,只是足以四处走动,所以如果有一些巧妙的技巧我应该知道这里我会很感激:)

谢谢!

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

#!/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.

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 :)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

时光磨忆 2024-10-13 12:31:41

尝试:

which python

在终端中。

Try:

which python

in a terminal.

弄潮 2024-10-13 12:31:41

正是出于这个原因,建议您将 shebang 行更改为与路径无关:

#!/usr/bin/env python

请参阅 此邮件列表消息了解更多信息:

考虑在不同的机器上,python 可能安装在 /usr/bin/python/bin/python 在这些情况下,# !/usr/local/bin/python 将失败。
对于这些情况,我们可以使用参数调用 env 可执行文件,它将通过在 $PATH 中搜索来确定参数路径并正确使用它。

(env 几乎总是位于 /usr/bin/ 中,因此不必担心 env 不存在于 中/usr/bin。)

For this very reason it is recommend that you change your shebang line to be more path agnostic:

#!/usr/bin/env python

See this mailing list message for more information:

Consider the possiblities that in a different machine, python may be installed at /usr/bin/python or /bin/python in those cases, #!/usr/local/bin/python will fail.
For those cases, we get to call the env executable with argument which will determine the arguments path by searching in the $PATH and use it correctly.

(env is almost always located in /usr/bin/ so one need not worry that env is not present at /usr/bin.)

乱了心跳 2024-10-13 12:31:41
# which python
/usr/local/bin/python

更新

我读错了。将标头替换为

#!/usr/bin/env python

这将从运行脚本环境设置的用户处获取 python 位置

# which python
/usr/local/bin/python

Update:

I misread. Replace your header with

#!/usr/bin/env python

This will pull in the python location from the user that runs the script's environmental settings

指尖上的星空 2024-10-13 12:31:41

尝试:which pythonwhereis python

Try: which python or whereis python

青芜 2024-10-13 12:31:41

对 Python 头脚本使用反引号是个好主意:

`which python`

It is a good idea to use backticks for header Python script:

`which python`
梦萦几度 2024-10-13 12:31:41

解决此问题的正确方法是

#!/usr/bin/env python

允许在 shebang 的 PATH 中使用二进制文件。

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.

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