sys.executable如何确定解释器路径? [英] How does sys.executable determine the interpreter path?

查看:1043
本文介绍了sys.executable如何确定解释器路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Mac上用自制软件安装了python.一些工具(例如pipenv)有一些麻烦,因为它们尝试写入/lib/python3.7/site-packages/,而在Mac下是不允许的.经过一番调查,我发现他们启动了一个在sys.executable中发现的新python解释器,该解释器实际上与自制软件安装的python路径不一致.

I've installed python on mac with homebrew. Some tools (pipenv for example) have some troubles because they try to write to /lib/python3.7/site-packages/, not permitted under mac. After some investigation I found that they start a new python interpreter found in sys.executable that is effectively inconsistent with the python path installed by homebrew.

$ which python
/usr/local/bin/python
$ python -c "import sys; print(sys.executable)"
/usr/local/opt/python/bin/python3.7

我希望这些路径指向同一二进制文件,为什么不是这样呢?如何控制sys.executable?

I would expect that these path points to the same binary, why is it not so? How can I control sys.executable?

推荐答案

在运行时确定(由

It is determined at runtime (by the calculate_program_full_path() function in Module/getpath.c, to be exact. It is usually based of the argv[0] value that the OS passed in.

您可以通过设置 PYTHONEXECUTABLE环境变量来设置其他值.

You can set an alternative value by setting the PYTHONEXECUTABLE environment variable.

但是,在自制软件上,还有更多工作要做.自制软件会强制执行此问题,并直接在 sitecustomize.py模块 <在安装时生成的href ="https://github.com/Homebrew/homebrew-core/blob/master/Formula/python.rb#L317-L333" rel ="noreferrer"> :

However, on homebrew builds, a bit more is going on. Homebrew forces the issue and sets sys.executable directly in a sitecustomize.py module generated at install time:

$ tail -n2 /usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sitecustomize.py
    # Set the sys.executable to use the opt_prefix
    sys.executable = '/usr/local/opt/python/bin/python3.7'

即使已设置,它也会忽略PYTHONEXECUTABLE.

This blithely ignores PYTHONEXECUTABLE even if set.

那是怎么回事,为什么自制酿造物sys.executable?

So what is going on, why does homebrew clobber sys.executable?

Homebrew Python是 MacOS框架构建,可以使用此Python二进制文件运行GUI应用程序.苹果公司对框架束中的二进制文件提出了非常严格的要求,包括如何使用它,包括允许将可执行文件名设置为二进制文件.要解决这些问题,框架二进制文件实际上是包装器)转换为更好的路径,设置__PYVENV_LAUNCHER__环境变量并启动位于Resources/Python.app/Contents/MacOS/Python actual python二进制文件,然后使用__PYVENV_LAUNCHER__环境变量来通知.

Homebrew Python is a MacOS framework build, so you can run GUI apps with this Python binary. A binary inside a framework bundle is placed under very strict requirements by Apple as to what you can do with it, including what the executable name is allowed to be set to. To work around these the framework binary is actually a wrapper ) that translates takes to a better path, sets the __PYVENV_LAUNCHER__ environment variable and launches the actual python binary located at Resources/Python.app/Contents/MacOS/Python, which then uses the __PYVENV_LAUNCHER__ environment variable to inform sys.executable.

包装器设置的路径在目录名称中的任何符号链接 已解析.由于homebrew使/usr/local/opt/python成为指向特定Python瓶子目录的符号链接,因此运行/usr/local/opt/python/bin/python3会导致sys.executable设置为链接的瓶子路径:

The path the wrapper sets has any symlinks in the directory name resolved. Since homebrew makes /usr/local/opt/python a symlink to a specific Python bottle directory, running /usr/local/opt/python/bin/python3 results in sys.executable being set to the linked bottle path:

$ /usr/local/opt/python/bin/python3 -S -c 'import sys; print(sys.executable)'
/usr/local/Cellar/python/3.7.0/bin/python3

违反了符号链接的目的,并且每次自制软件对Python公式进行次要版本更新时,都可能导致损坏的pip安装脚本.

which defeats the purpose of the symlink, and can lead to broken pip-installed scripts each time homebrew makes a minor version update to the Python formula.

我希望自制软件至少在这里检查是否设置了PYTHONEXECUTABLE .您可以直接设置sys.executable来自己强行解决问题:

I'd like for homebrew to at least check if PYTHONEXECUTABLE is set here. You can force the issue yourself by just setting sys.executable directly:

import os, sys

if 'PYTHONEXECUTABLE' in os.environ and :
    sys.executable = os.environ['PYTHONEXECUTABLE']

我已经打开了一个报告以请求对PYTHONEXECUTABLE的自制Python公式检查包含了建议的修复程序.该修补程序已于2018年11月28日发布,因此只需更新Python软件包即可获取新版本,并使Homebrew Python再次获得PYTHONEXECUTABLE荣誉.

I've opened a report to request the homebrew Python formula checks for PYTHONEXECUTABLE and included a suggested fix. The fix was landed on November 28, 2018, so just an update of your Python packages should get you the new version and make Homebrew Python honour PYTHONEXECUTABLE once again.

这篇关于sys.executable如何确定解释器路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆