Skip to content

UnboundLocalError in ensure_python SESSION_IS_INTERACTIVE=False, using pyenv, and python version in Pipfile not available #6387

@Ksurender

Description

@Ksurender

Issue description

Starting today in some environments that pull the latest pipenv I am observing a hard error during pipenv install

  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/utils/virtualenv.py", line 150, in ensure_virtualenv
    python = ensure_python(project, python=python)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/utils/virtualenv.py", line 323, in ensure_python
    if not (project.s.PIPENV_YES or Confirm.ask("".join(s), default=True)):
                                                        ^
UnboundLocalError: cannot access local variable 's' where it is not associated with a value

I am able to reproduce this locally and found it requires the following:

  • Using pyenv to manage python version
  • Only having a Python version that does not match the Python version in the Pipfile. For example my environment has Python 3.11.1 but the Pipfile specifies 3.11.4
  • Running in an environment where environments.SESSION_IS_INTERACTIVE is False

Looking at the code there is a codepath where 's' will never be defined if SESSION_IS_INTERACTIVE=False and PIPENV_YES=False

https://github.com/pypa/pipenv/blob/main/pipenv/utils/virtualenv.py#L323

Expected result

Yesterday I observed that pipenv just accepted this version discrepancy and moved forward with creating the venv. I realize there is a problem on my end with version mismatch, but I don't think a hard error is the intention of the code in virtualenv.py. Going back to this would be preferred for me, but if the right thing to do is produce a useful error message that is better as well.

Actual result

Here is the output for nohup python install --verbose

Using python: 3.11.4
Path to python: None
Warning: Python 3.11.4 was not found on your system...
Traceback (most recent call last):
  File "/home/blah/.pyenv/versions/3.11.1/bin/pipenv", line 8, in <module>
    sys.exit(cli())
             ^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/vendor/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/cli/options.py", line 52, in main
    return super().main(*args, **kwargs, windows_expand_args=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/vendor/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/vendor/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/vendor/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/vendor/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/vendor/click/decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/vendor/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/cli/command.py", line 208, in install
    do_install(
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/routines/install.py", line 273, in do_install
    ensure_project(
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/utils/project.py", line 47, in ensure_project
    ensure_virtualenv(
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/utils/virtualenv.py", line 150, in ensure_virtualenv
    python = ensure_python(project, python=python)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv/utils/virtualenv.py", line 323, in ensure_python
    if not (project.s.PIPENV_YES or Confirm.ask("".join(s), default=True)):
                                                        ^
UnboundLocalError: cannot access local variable 's' where it is not associated with a value

Steps to replicate

Use the following Pipfile:

[packages]
click = "*"

[requires]
python_version = "3.11.4"

Setup pyenv so Python 3.11.4 is not available with the latest pipenv.

pyenv uninstall 3.11.4
pyenv install 3.11.1
pyenv global 3.11.1
pip install pipenv

Now try to run pipenv install with nohup:

nohup pipenv install

the nohup.out has the error above. If you run this in an interactive shell you will instead go through the codepath where 's' gets defined and you are prompted about the version mismatch.


$ pipenv --support

Pipenv version: '2025.0.1'

Pipenv location: '/home/blah/.pyenv/versions/3.11.1/lib/python3.11/site-packages/pipenv'

Python location: '/home/blah/.pyenv/versions/3.11.1/bin/python3.11'

OS Name: 'posix'

User pip version: '25.0.1'

user Python installations found:

  • 3.11.1: /home/blah/.pyenv/versions/3.11.1/bin/python3
  • 3.11.1: /home/blah/.pyenv/versions/3.11.1/bin/python
  • 3.11.1: /home/blah/.pyenv/shims/python3
  • 3.11.1: /home/blah/.pyenv/shims/python
  • 3.10.12: /usr/bin/python3
  • 3.10.12: /bin/python3
  • 3.9.19: /home/blah/.pyenv/versions/3.9.19/bin/python3.9
  • 3.9.19: /home/blah/.pyenv/versions/3.9.19/bin/python3
  • 3.9.19: /home/blah/.pyenv/versions/3.9.19/bin/python
  • 3.6.15: /home/blah/.pyenv/versions/3.6.15/bin/python3.6m
  • 3.6.15: /home/blah/.pyenv/versions/3.6.15/bin/python3
  • 3.6.15: /home/blah/.pyenv/versions/3.6.15/bin/python
  • 3.6.15: /home/blah/.pyenv/versions/3.6.15/bin/python3.6
  • 2.7.18: /usr/bin/python2.7
  • 2.7.18: /usr/bin/python
  • 2.7.18: /usr/bin/python2
  • 2.7.18: /bin/python2.7
  • 2.7.18: /bin/python
  • 2.7.18: /bin/python2

PEP 508 Information:

{'implementation_name': 'cpython',
 'implementation_version': '3.11.1',
 'os_name': 'posix',
 'platform_machine': 'x86_64',
 'platform_python_implementation': 'CPython',
 'platform_release': '6.2.0-39-generic',
 'platform_system': 'Linux',
 'platform_version': '#40~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 16 '
                     '10:53:04 UTC 2',
 'python_full_version': '3.11.1',
 'python_version': '3.11',
 'sys_platform': 'linux'}

System environment variables:

  • SHELL
  • SESSION_MANAGER
  • PYENV_HOOK_PATH
  • QT_ACCESSIBILITY
  • COLORTERM
  • PYENV_SHELL
  • XDG_CONFIG_DIRS
  • XDG_MENU_PREFIX
  • TERM_PROGRAM_VERSION
  • GTK_IM_MODULE
  • TMUX
  • P4CONFIG
  • XMODIFIERS
  • DESKTOP_SESSION
  • EDITOR
  • PYENV_VERSION
  • GTK_MODULES
  • XDG_SEAT
  • PWD
  • XDG_SESSION_DESKTOP
  • LOGNAME
  • XDG_SESSION_TYPE
  • SYSTEMD_EXEC_PID
  • WINDOWPATH
  • HOME
  • USERNAME
  • LANG
  • XDG_CURRENT_DESKTOP
  • KITTY_WINDOW_ID
  • SSH_CONNECTION
  • CLUTTER_IM_MODULE
  • PYENV_DIR
  • XDG_SESSION_CLASS
  • TERM
  • USER
  • TMUX_PANE
  • DISPLAY
  • SHLVL
  • QT_IM_MODULE
  • XDG_VTNR
  • UBUNTU_MENUPROXY
  • XDG_SESSION_ID
  • XDG_RUNTIME_DIR
  • PYENV_ROOT
  • XDG_DATA_DIRS
  • PATH
  • GDMSESSION
  • DBUS_SESSION_BUS_ADDRESS
  • A__z
  • TERM_PROGRAM
  • PIP_DISABLE_PIP_VERSION_CHECK
  • PYTHONDONTWRITEBYTECODE

Pipenv–specific environment variables:

Debug–specific environment variables:

  • PATH: /home/blah/.pyenv/versions/3.11.1/bin:/home/blah/.pyenv/libexec:/home/blah/.pyenv/plugins/python-build/bin:/home/blah/.pyenv/plugins/pyenv-virtualenv/bin:/home/blah/.pyenv/plugins/pyenv-update/bin:/home/blah/.pyenv/plugins/pyenv-doctor/bin:/home/blah/.pyenv/shims:/home/blah/.pyenv/bin:/u/blah/dtbx:/u/blah/.local/bin/:/usr/local/gcc/4.9.4/bin:/u/blah/bin:/home/blah/.local/bin:/u/blah/dtbx:/u/blah/.local/bin/:/usr/local/gcc/4.9.4/bin:/u/blah/bin:/home/blah/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
  • SHELL: /usr/bin/ksh
  • EDITOR: emacs
  • LANG: en_US.UTF-8
  • PWD: /disk1/tmp/pyenv_issue

Contents of Pipfile ('/disk1/tmp/pyenv_issue/Pipfile'):

[packages]
click = "*"

[requires]
python_version = "3.11.4"

Metadata

Metadata

Assignees

No one assigned

    Labels

    PR: awaiting-mergeThe PR related to this issue has been reviewed and is awaiting merge.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions