Skip to content

PYTHONPATH affects isolated builds #405

@sanderr

Description

@sanderr

Introduction

I've found that setting the PYTHONPATH environment variable affects isolated builds, to the point of breaking functionality in some cases. I'll describe steps to reproduce, and propose a fix, though I should disclaim that my experience with both this package and the PYTHONPATH environment variable are limited, so I might be missing something. This ticket follows from the assumption that setting the PYTHONPATH environment variable should not affect build behavior (as seems to be confirmed by #373 (comment)). If this assumption is wrong, the ticket is invalid, though I'd be interested to know why this is the case.

Environment

Python 3.6.15. The same steps produce an error for 3.9 as well, but it is a different one (similar to the error in #266) so I'll create a separate ticket for that if it is agreed upon that this is a bug.

Steps to reproduce

# clone the sample project
git clone https://github.com/pypa/sampleproject.git
cd sampleproject
# create a new virtual environment
python3.6 -m venv .env
.env/bin/pip install -U pip build
# build the sample project with the env's libs dir in PYTHONPATH
PYTHONPATH=.env/lib/python3.6/site-packages/ .env/bin/python -m build --sdist

Expected behavior

An sdist is built

Observed behavior

* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools>=40.8.0, wheel)

Usage:   
  /tmp/build-env-a0xlzsq3/bin/python -m pip install [options] <requirement specifier> [package-index-options] ...
  /tmp/build-env-a0xlzsq3/bin/python -m pip install [options] -r <requirements file> [package-index-options] ...
  /tmp/build-env-a0xlzsq3/bin/python -m pip install [options] [-e] <vcs project url> ...
  /tmp/build-env-a0xlzsq3/bin/python -m pip install [options] [-e] <local project path> ...
  /tmp/build-env-a0xlzsq3/bin/python -m pip install [options] <archive url/path> ...

no such option: --use-pep517

Traceback (most recent call last):
  File "/home/sander/documents/projects/sampleproject/.env/lib/python3.6/site-packages/build/__main__.py", line 373, in main
    args.srcdir, outdir, distributions, config_settings, not args.no_isolation, args.skip_dependency_check
  File "/home/sander/documents/projects/sampleproject/.env/lib/python3.6/site-packages/build/__main__.py", line 202, in build_package
    out = _build(isolation, builder, outdir, distribution, config_settings, skip_dependency_check)
  File "/home/sander/documents/projects/sampleproject/.env/lib/python3.6/site-packages/build/__main__.py", line 140, in _build
    return _build_in_isolated_env(builder, outdir, distribution, config_settings)
  File "/home/sander/documents/projects/sampleproject/.env/lib/python3.6/site-packages/build/__main__.py", line 108, in _build_in_isolated_env
    env.install(builder.build_system_requires)
  File "/home/sander/documents/projects/sampleproject/.env/lib/python3.6/site-packages/build/env.py", line 211, in install
    _subprocess(cmd)
  File "/home/sander/documents/projects/sampleproject/.env/lib/python3.6/site-packages/build/env.py", line 81, in _subprocess
    raise e
  File "/home/sander/documents/projects/sampleproject/.env/lib/python3.6/site-packages/build/env.py", line 78, in _subprocess
    subprocess.check_output(cmd, stderr=subprocess.STDOUT)
  File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "/usr/lib/python3.6/subprocess.py", line 438, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/tmp/build-env-a0xlzsq3/bin/python', '-Im', 'pip', 'install', '--use-pep517', '--no-warn-script-location', '-r', '/tmp/build-reqs-ont40sbg.txt']' returned non-zero exit status 2.

ERROR Command '['/tmp/build-env-a0xlzsq3/bin/python', '-Im', 'pip', 'install', '--use-pep517', '--no-warn-script-location', '-r', '/tmp/build-reqs-ont40sbg.txt']' returned non-zero exit status 2.

Likely cause

IsolatedEnvBuilder sets up an isolated environment to perform the build using create_isolated_env_venv (virtualenv is not installed for this scencario). Package installation within this environment is then handled by _IsolatedEnvVenvPip.install, which passes Python's -I flag to ensure isolation:

build/src/build/env.py

Lines 200 to 209 in 90dbd8b

cmd = [
self.executable,
'-Im',
'pip',
'install',
'--use-pep517',
'--no-warn-script-location',
'-r',
os.path.abspath(req_file.name),
]

However, this same flag is absent during setup: create_isolated_env_venv performs a pip install pip (simplified) to ensure a sufficiently recent pip is installed:
_subprocess([executable, '-m', 'pip', 'install', f'pip>={minimum_pip_version}'])

As a result, the pip install pip during the set up of the environment still takes the PYTHONPATH environment variable into account, deciding that a matching pip is already installed. As a result the outdated pip in the isolated environment is not updated, which causes the failure seen above.

Proposed fix

Add the -I flag to both pip install commands in create_isolated_env_venv. I've confirmed that this resolves this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions