Skip to content

inject command uses wrong python interpreter for local packages #956

@lecardozo

Description

@lecardozo

Describe the bug
While trying to pipx inject a local wheel with specific Python version constraints into a compatible environment (i.e. with a valid interpreter for those constraints), I keep getting the following error:

ERROR: Package 'my-local-package' requires a different Python: 3.11.1 not in '<3.11,>=3.8'

Here are the relevant versions and constraints 👇

  • package (to be injected) interpreter constraints: <3.11, >=3.8
  • environment interpreter: 3.8
  • pipx interpreter: 3.11.1

After seeing the error, it became clear that somehow in the middle of the injection process, pipx was trying to install the package with a different interpreter for some reason. After some debugging, I noticed that when the package is a local distribution we need to install it (without its dependencies) into a temporary venv in order to extract its name. However, when we do it, we end up using a default Python interpreter, instead of the one associated with the venv here 👇

# package_spec is anything pip-installable, including package_name, vcs spec,
# zip file, or tar.gz file.
if package_name is None:
package_name = package_name_from_spec(
package_spec, venv.python, pip_args=pip_args, verbose=verbose
)

I locally replaced venv.python (which maps to a default interpreter in this case) with venv.python_path (which maps to venv-specific interpreter) and it worked 😃

How to reproduce

Verbose logs for the inject command

pipx >(_parsed_package_to_package_or_url:128): cleaned package spec: /path/to/my-local-package-1.0.0-py3-none-any.whl
creating virtual environment...
pipx >(run_subprocess:173): running /opt/homebrew/Cellar/pipx/1.1.0/libexec/bin/python3.11 -m venv --without-pip /var/folders/vj/chbx1tms2c93nmnnp2gw4wh80000gq/T/tmpe5g_foll
pipx >(run_subprocess:173): running /var/folders/vj/chbx1tms2c93nmnnp2gw4wh80000gq/T/tmpe5g_foll/bin/python -c import sysconfig; print(sysconfig.get_path('purelib'))
pipx >(run_subprocess:173): running /Users/me/.local/pipx/shared/bin/python -c import sysconfig; print(sysconfig.get_path('purelib'))
pipx >(run_subprocess:173): running /var/folders/vj/chbx1tms2c93nmnnp2gw4wh80000gq/T/tmpe5g_foll/bin/python --version
determining package name from '/path/to/my-local-package-1.0.0-py3-none-any.whl'...
pipx >(run_subprocess:173): running /var/folders/vj/chbx1tms2c93nmnnp2gw4wh80000gq/T/tmpe5g_foll/bin/python -m pip list --format=json
pipx >(run_subprocess:173): running /var/folders/vj/chbx1tms2c93nmnnp2gw4wh80000gq/T/tmpe5g_foll/bin/python -m pip install --no-dependencies /path/to/my-local-package-1.0.0-py3-none-any.whl
Looking in indexes: https://pypi.org/simple
Processing /path/to/my-local-package-1.0.0-py3-none-any.whl
ERROR: Package 'my-local-package' requires a different Python: 3.11.1 not in '<3.11,>=3.8'
pipx >(subprocess_post_check:207): '/var/folders/vj/chbx1tms2c93nmnnp2gw4wh80000gq/T/tmpe5g_foll/bin/python -m pip install --no-dependencies /path/to/my-local-package-1.0.0-py3-none-any.whl' failed
Cannot determine package name from spec '/path/to/my-local-package-1.0.0-py3-none-any.whl'. Check package spec for errors.

Expected behavior

pipx should be able to inject dependencies into existing environments if their interpreters are compatible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions