Skip to content

python: Odd behaviour with sys.executable #30445

@asottile

Description

@asottile

Please note we will close your issue without comment if you delete, do not read or do not fill out the issue checklist below and provide ALL the requested information. If you repeatedly fail to use the issue template, we will block you from ever submitting issues to Homebrew again.

To help us debug your issue please explain:

  • What you were trying to do (and why)
  • What happened (include command output)
  • What you expected to happen
  • Step-by-step reproduction instructions (by running brew install commands)

Here's a script which reproduces my problem:

#!/usr/bin/env bash
set -euxo pipefail

brew install python

rm -rf venv bin

BREW_PYTHON="/usr/local/bin/python3"
VENV="${PWD}/venv"
EXE="${VENV}/bin/python"

: make a virtualenv to have a separate python executable that acts normal
curl --silent https://asottile.github.io/get-virtualenv.py |
    "${BREW_PYTHON}" - "${VENV}" >& /dev/null

: write a small script which just prints what executable it ran from
mkdir bin
echo -e '#!'"${EXE}\nimport sys;print(sys.executable)" > bin/t
chmod +x bin/t

export PATH="${PWD}/bin:${PATH}"

: normal invocation of 't' works
test "$(t)" = "${EXE}"

: running t from a python subprocess is different though?
test "$(/usr/local/opt/python/libexec/bin/python -c 'import subprocess; subprocess.call(("t",))')" = "${EXE}"

Despite my script bin/t having a full shebang (here's what it ends up looking like):

#!/tmp/t/venv/bin/python
import sys;print(sys.executable)

When run as a subprocess from brew's python it is reporting an executable which is entirely different from the one being used.

I suspect this is due to this

Here's the output on my machine (script returns 1)

$ bash test.sh
+ brew install python
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
No changes to formulae.

Warning: python 3.7.0 is already installed and up-to-date
To reinstall 3.7.0, run `brew reinstall python`
+ rm -rf venv bin
+ BREW_PYTHON=/usr/local/bin/python3
+ VENV=/tmp/t/venv
+ EXE=/tmp/t/venv/bin/python
+ : make a virtualenv to have a separate python executable that acts normal
+ curl --silent https://asottile.github.io/get-virtualenv.py
+ /usr/local/bin/python3 - /tmp/t/venv
+ : write a small script which just prints what executable it ran from
+ mkdir bin
+ echo -e '#!/tmp/t/venv/bin/python\nimport sys;print(sys.executable)'
+ chmod +x bin/t
+ export 'PATH=/tmp/t/bin:/Users/asottile/.cargo/bin:/Users/asottile/bin:/usr/local/opt/findutils/libexec/gnubin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public'
+ PATH='/tmp/t/bin:/Users/asottile/.cargo/bin:/Users/asottile/bin:/usr/local/opt/findutils/libexec/gnubin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public'
+ : normal invocation of t works
++ t
+ test /tmp/t/venv/bin/python = /tmp/t/venv/bin/python
+ : running t from a python subprocess is different 'though?'
++ /usr/local/opt/python/libexec/bin/python -c 'import subprocess; subprocess.call(("t",))'
+ test /usr/local/Cellar/python/3.7.0/libexec/bin/python = /tmp/t/venv/bin/python

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedPR was locked due to age

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions