Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for virtualenv #909

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/pipx/shared_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,27 @@ def site_packages(self) -> Path:

def create(self, verbose: bool = False) -> None:
if not self.is_valid:
with animate("creating shared libraries", not verbose):
create_process = run_subprocess(
[DEFAULT_PYTHON, "-m", "venv", "--clear", self.root]
)
subprocess_post_check(create_process)

try:
with animate("creating shared libraries using venv", not verbose):
create_process = run_subprocess(
[DEFAULT_PYTHON, "-m", "venv", "--clear", self.root]
)
subprocess_post_check(create_process)
except Exception:
# try to create shared libs using virtualenv if venv is not available
with animate("creating shared libraries using virtualenv", not verbose):
create_process = run_subprocess(
[
DEFAULT_PYTHON,
"-m",
"virtualenv",
"--clear",
"--creator",
"venv",
self.root,
]
)
subprocess_post_check(create_process)
# ignore installed packages to ensure no unexpected patches from the OS vendor
# are used
self.upgrade(pip_args=["--force-reinstall"], verbose=verbose)
Expand Down
23 changes: 19 additions & 4 deletions src/pipx/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,25 @@ def main_package_name(self) -> str:
return self.pipx_metadata.main_package.package

def create_venv(self, venv_args: List[str], pip_args: List[str]) -> None:
with animate("creating virtual environment", self.do_animation):
cmd = [self.python, "-m", "venv", "--without-pip"]
venv_process = run_subprocess(cmd + venv_args + [str(self.root)])
subprocess_post_check(venv_process)
cmd = [self.python, "-m", "venv", "--without-pip"]
try:
with animate("creating virtual environment using venv", self.do_animation):
venv_process = run_subprocess(cmd + venv_args + [str(self.root)])
subprocess_post_check(venv_process)
except Exception:
with animate(
"creating virtual environment using virtualenv", self.do_animation
):
cmd = [
self.python,
"-m",
"virtualenv",
"--creator",
"venv",
"--without-pip",
]
virtualenv_process = run_subprocess(cmd + venv_args + [str(self.root)])
subprocess_post_check(virtualenv_process)

shared_libs.create(self.verbose)
pipx_pth = get_site_packages(self.python_path) / PIPX_SHARED_PTH
Expand Down