From faba5398c9736255dd043271361cbc7f8803728e Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Fri, 2 Dec 2022 22:56:01 -0500 Subject: [PATCH 1/2] Fix issue with venv module not being available on some systems. --- news/5477.bugfix.rst | 1 + pipenv/core.py | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 news/5477.bugfix.rst diff --git a/news/5477.bugfix.rst b/news/5477.bugfix.rst new file mode 100644 index 0000000000..e6342dbca8 --- /dev/null +++ b/news/5477.bugfix.rst @@ -0,0 +1 @@ +Check if ``venv`` module is available and fallback to prior logic when it is not. diff --git a/pipenv/core.py b/pipenv/core.py index af1e79255e..7d36c73151 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -989,15 +989,26 @@ def do_create_virtualenv(project, python=None, site_packages=None, pypi_mirror=N err=True, ) - cmd = [ - Path(sys.executable).absolute().as_posix(), - "-m", - "virtualenv", - "--creator=venv", - f"--prompt={project.name}", - f"--python={python}", - project.get_location_for_virtualenv(), - ] + try: + import venv + cmd = [ + Path(sys.executable).absolute().as_posix(), + "-m", + "virtualenv", + "--creator=venv", + f"--prompt={project.name}", + f"--python={python}", + project.get_location_for_virtualenv(), + ] + except (ImportError, ModuleNotFoundError) as error: + cmd = [ + Path(sys.executable).absolute().as_posix(), + "-m", + "virtualenv", + f"--prompt={project.name}", + f"--python={python}", + project.get_location_for_virtualenv(), + ] # Pass site-packages flag to virtualenv, if desired... if site_packages: From 35cd3f1030a0169cbe553f707b54d6e5a1e9a1cf Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Fri, 2 Dec 2022 23:31:22 -0500 Subject: [PATCH 2/2] Fix lint --- pipenv/core.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pipenv/core.py b/pipenv/core.py index 7d36c73151..771e8eea7b 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -990,7 +990,8 @@ def do_create_virtualenv(project, python=None, site_packages=None, pypi_mirror=N ) try: - import venv + import venv # noqa + cmd = [ Path(sys.executable).absolute().as_posix(), "-m", @@ -1000,7 +1001,7 @@ def do_create_virtualenv(project, python=None, site_packages=None, pypi_mirror=N f"--python={python}", project.get_location_for_virtualenv(), ] - except (ImportError, ModuleNotFoundError) as error: + except ImportError: cmd = [ Path(sys.executable).absolute().as_posix(), "-m",