Skip to content

Instantly share code, notes, and snippets.

@davidlatwe
Last active May 20, 2021 17:31
Show Gist options
  • Save davidlatwe/e13475a06f86895e8dfd750a39f432f1 to your computer and use it in GitHub Desktop.
Save davidlatwe/e13475a06f86895e8dfd750a39f432f1 to your computer and use it in GitHub Desktop.
Virtual env based python rez package (filesystem combined package)

Virtual env based python rez package

This is a combined package, which cannot be built but simply copying python.py into package repository. And it will provide clean, re-usable virtual env that created from system installed python that it could find.

$ rez-search python
python-2.7-venv
python-3.6-venv
python-3.7-venv
python-3.8-venv

requirement

These (pip) packages should be installed in Rez production env:

Caveat

If regular python package (dir) and python.py combined pacakge both exists in same filesystem repository, only dir package will be iterated, so you have to place the combined one in another repository. e.g. ~/rez/package/combined

from pythonfinder import Finder
name = "python"
description = "Python interpreter provided from virtual env"
uuid = "python.virtualenv"
versions = [
# e.g. "2.7-venv"
py.py_version.name + "-venv"
for py in Finder().find_all_python_versions()
]
def commands():
import os
import virtualenv
from pythonfinder import Finder
finder = Finder()
python_exec = finder.find_python_version(this.version.major,
minor=this.version.minor)
if python_exec is None:
stop("No python installed in system.")
venv_dir = python_exec.path.parent / "rez.venv"
args = [
"--python", str(python_exec.path),
"--no-seed", # no pip, no wheel, no setuptools
str(venv_dir)
]
# compute venv
session = virtualenv.session_via_cli(args)
if not os.path.isdir(str(session.creator.bin_dir)):
print("\tCreating virtual env..")
session = virtualenv.cli_run(args)
env.PATH.prepend(str(session.creator.bin_dir))
@davidlatwe
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment