Mork is a library designed for installing and querying python packages inside virtual environments.
>>> import mork >>> venv = mork.VirtualEnv.from_project_path('/home/user/git/pipenv') >>> dists = venv.get_distributions() >>> [dist for dist in dists][:3] [wheel 0.31.1 (/home/user/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages), Werkzeug 0.14.1 (/home/user/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages), vistir 0.1.4 (/home/user/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages)]
>>> from requirementslib.models.requirements import Requirement >>> r = Requirement.from_line("requests") >>> venv.install(r, editable=False)
>>> pkg = "pytz" >>> with venv.uninstall(pkg, auto_confirm=True) as uninstall: if uninstall.paths: cleaned = pkg >>> print("Removed package: %s" % cleaned)
>>> venv.python '/home/user/.virtualenvs/pipenv-MfOPs1lW/bin/python' >>> venv.python_version '3.7'
>>> cmd = venv.run("env") >>> [line for line in cmd.out.splitlines() if line.startswith("VIRTUAL_ENV")] ['VIRTUAL_ENV=/user/hawk/.virtualenvs/pipenv-MfOPs1lW'] >>> cmd = venv.run_py(["import os; print(os.environ.get('VIRTUAL_ENV'))"]) Deactivating virtualenv... >>> cmd.out '/home/user/.virtualenvs/pipenv-MfOPs1lW\n' >>> with venv.activated(): print(os.environ["VIRTUAL_ENV"]) /home/hawk/.virtualenvs/pipenv-MfOPs1lW