Skip to content

venv base-executable path does not resolve symlinks using realpath() #2682

@mayeut

Description

@mayeut

Issue

The home & base-executable keys in pyvenv.cfg do not resolve symlinks using realpath() leading to a broken virtual environment in some cases.

c.f. python/cpython#106045

Environment

Provide at least:

  • OS: at least Linux
  • GHA reproducer:
name: Python test bitness

on:
  push:
    branches:
      - venv-reproducer
  workflow_dispatch:
    # allow manual runs on branches without a PR

jobs:
  venv:
    runs-on: ubuntu-22.04
    steps:
    - uses: actions/setup-python@v5
      with:
        python-version: "3.11"
    - name: test
      run: |
        set -x
        fail_venv=0
        fail_virtualenv=0
        sudo apt-get update &>/dev/null
        sudo apt-get install -y python3-distutils &>/dev/null
        python3 -m pip install virtualenv
        python3 -m venv cp311-1
        cat cp311-1/pyvenv.cfg
        cp311-1/bin/python -sSEc 'import sys; print(sys.path)'
        python3 -m virtualenv cp311-2
        cat cp311-2/pyvenv.cfg
        cp311-2/bin/python -sSEc 'import sys; print(sys.path)'
        ln -sf $(which python3) /usr/local/bin/python3
        /usr/local/bin/python3 -m venv cp311-3 || fail_venv=1
        cat cp311-3/pyvenv.cfg
        cp311-3/bin/python -sSEc 'import sys; print(sys.path)'
        /usr/local/bin/python3 -m virtualenv cp311-4 || fail_virtualenv=1
        cp311-4/bin/python -c 'import subprocess' || fail_virtualenv=1
        cat cp311-4/pyvenv.cfg
        cp311-4/bin/python -sSEc 'import sys; print(sys.path)'
        exit $(( $fail_venv + $fail_virtualenv ))

Output

the cp311-4 is broken & uses the following pyvenv.cfg:

home = /usr/local/bin
implementation = CPython
version_info = 3.11.7.final.0
virtualenv = 20.25.0
include-system-site-packages = false
base-prefix = /opt/hostedtoolcache/Python/3.11.7/x64
base-exec-prefix = /opt/hostedtoolcache/Python/3.11.7/x64
base-executable = /usr/local/bin/python3

/usr/local/bin shall never be referenced in this file as /usr/local/bin/python3 is just a symlink on /opt/hostedtoolcache/Python/3.11.7/x64/bin/python3

importing subprocess fails because the computed sys.path is invalid for this interpreter (/usr/lib/python3.11/lib-dynload is invalid here):

['', '/opt/hostedtoolcache/Python/3.11.7/x64/lib/python311.zip', '/opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11', '/usr/lib/python3.11/lib-dynload']

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions