Welcome to mork’s documentation!ΒΆ

mork: A project for installing packages across the virtualenv boundary.ΒΆ

https://img.shields.io/pypi/v/mork.svg https://img.shields.io/pypi/l/mork.svg https://api.travis-ci.com/sarugaku/mork.svg?branch=master https://ci.appveyor.com/api/projects/status/5mo40rneihk6y8po/branch/master?svg=true https://img.shields.io/pypi/pyversions/mork.svg https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg Documentation Status

SummaryΒΆ

Mork_ is a library designed for installing and querying python packages inside virtual environments.

πŸ‰ See What’s InstalledΒΆ

>>> 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)]

πŸ‰ Install A PackageΒΆ

>>> from requirementslib.models.requirements import Requirement
>>> r = Requirement.from_line("requests")
>>> venv.install(r, editable=False)

πŸ‰ Uninstall a PackageΒΆ

>>> pkg = "pytz"
>>> with venv.uninstall(pkg, auto_confirm=True) as uninstall:
        if uninstall.paths:
            cleaned = pkg
>>> print("Removed package: %s" % cleaned)

πŸ‰ Display Information about PythonΒΆ

>>> venv.python
'/home/user/.virtualenvs/pipenv-MfOPs1lW/bin/python'
>>> venv.python_version
'3.7'

πŸ‰ Run Commands Inside the VirtualenvΒΆ

>>> 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

Read the documentation.

mork packageΒΆ

class mork.VirtualEnv(prefix=None, base_working_set=None, is_venv=True)[source]ΒΆ

Bases: object

activated(include_extras=True, extra_dists=[])[source]ΒΆ

A context manager which activates the virtualenv.

Parameters:extra_dists (list) – Paths added to the context after the virtualenv is activated.
This context manager sets the following environment variables:
  • PYTHONUSERBASE
  • VIRTUAL_ENV
  • PYTHONIOENCODING
  • PYTHONDONTWRITEBYTECODE

In addition, it activates the virtualenv inline by calling activate_this.py.

add_dist(dist_name)[source]ΒΆ
base_pathsΒΆ

Returns the context appropriate paths for the environment.

Returns:A dictionary of environment specific paths to be used for installation operations
Return type:dict

Note

The implementation of this is borrowed from a combination of pip and virtualenv and is likely to change at some point in the future.

>>> from pipenv.core import project
>>> from pipenv.environment import Environment
>>> env = Environment(prefix=project.virtualenv_location, is_venv=True, sources=project.sources)
>>> import pprint
>>> pprint.pprint(env.base_paths)
{'PATH': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/bin::/bin:/usr/bin',
'PYTHONPATH': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages',
'data': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW',
'include': '/home/hawk/.pyenv/versions/3.7.1/include/python3.7m',
'libdir': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages',
'platinclude': '/home/hawk/.pyenv/versions/3.7.1/include/python3.7m',
'platlib': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages',
'platstdlib': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7',
'prefix': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW',
'purelib': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages',
'scripts': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/bin',
'stdlib': '/home/hawk/.pyenv/versions/3.7.1/lib/python3.7'}
dist_is_in_project(dist)[source]ΒΆ
classmethod filter_sources(requirement, sources)[source]ΒΆ
find_egg(egg_dist)[source]ΒΆ
classmethod from_project_path(path)[source]ΒΆ

Utility for finding a virtualenv location based on a project path

get_distributions()[source]ΒΆ

Retrives the distributions installed on the library path of the virtualenv

Returns:A set of distributions found on the library path
Return type:iterator
get_finder()[source]ΒΆ
get_installed_packages()[source]ΒΆ
get_monkeypatched_pathset()[source]ΒΆ

Returns a monkeypatched UninstallPathset for using to uninstall packages from the virtualenv

Returns:A patched UninstallPathset which enables uninstallation of venv packages
Return type:pip._internal.req.req_uninstall.UninstallPathset
get_outdated_packages()[source]ΒΆ
get_package_info()[source]ΒΆ
get_setup_install_args(pkgname, setup_py, develop=False)[source]ΒΆ

Get setup.py install args for installing the supplied package in the virtualenv

Parameters:
  • pkgname (str) – The name of the package to install
  • setup_py (str) – The path to the setup file of the package
  • develop (bool) – Whether the package is in development mode
Returns:

The installation arguments to pass to the interpreter when installing

Return type:

list

classmethod get_sys_path(python_path)[source]ΒΆ

Get the sys.path data for a given python executable.

Parameters:python_path (str) – Path to a specific python executable.
Returns:The system path information for that python runtime.
Return type:list
get_working_set()[source]ΒΆ

Retrieve the working set of installed packages for the virtualenv.

Returns:The working set for the virtualenv
Return type:pkg_resources.WorkingSet
classmethod get_workon_home()[source]ΒΆ
initial_working_setΒΆ
install(req, editable=False, sources=[])[source]ΒΆ

Install a package into the virtualenv

Parameters:
  • req (requirementslib.models.requirement.Requirement) – A requirement to install
  • editable (bool) – Whether the requirement is editable, defaults to False
  • sources (list) – A list of pip sources to consult, defaults to []
Returns:

A return code, 0 if successful

Return type:

int

is_installed(pkgname)[source]ΒΆ

Given a package name, returns whether it is installed in the virtual environment

Parameters:pkgname (str) – The name of a package
Returns:Whether the supplied package is installed in the environment
Return type:bool
libdirΒΆ
locate_dist(dist)[source]ΒΆ
classmethod normalize_path(path)[source]ΒΆ
pathsΒΆ
pythonΒΆ

Path to the environment python

python_versionΒΆ
pyversionΒΆ
classmethod resolve_dist(dist, working_set)[source]ΒΆ

Given a local distribution and a working set, returns all dependencies from the set.

Parameters:
  • dist (pkg_resources.Distribution) – A single distribution to find the dependencies of
  • working_set (pkg_resources.WorkingSet) – A working set to search for all packages
Returns:

A set of distributions which the package depends on, including the package

Return type:

set(pkg_resources.Distribution)

run(cmd, cwd='.')[source]ΒΆ

Run a command with Popen in the context of the virtualenv

Parameters:
  • cmd (str or list) – A command to run in the virtual environment
  • cwd (str) – The working directory in which to execute the command, defaults to os.curdir
Returns:

A finished command object

Return type:

Popen

run_py(cmd, cwd='.')[source]ΒΆ

Run a python command in the virtualenv context.

Parameters:
  • cmd (str or list) – A command to run in the virtual environment - runs with python -c
  • cwd (str) – The working directory in which to execute the command, defaults to os.curdir
Returns:

A finished command object

Return type:

Popen

safe_import(name)[source]ΒΆ

Helper utility for reimporting previously imported modules while inside the venv

script_basedirΒΆ

Path to the environment scripts dir

scripts_dirΒΆ
setuptools_install(chdir_to, pkg_name, setup_py_path=None, editable=False)[source]ΒΆ

Install an sdist or an editable package into the virtualenv

Parameters:
  • chdir_to (str) – The location to change to
  • setup_py_path (str) – The path to the setup.py, if applicable defaults to None
  • editable (bool) – Whether the package is editable, defaults to False
sys_pathΒΆ

The system path inside the environment

Returns:The sys.path from the environment
Return type:list
sys_prefixΒΆ

The prefix run inside the context of the environment

Returns:The python prefix inside the environment
Return type:sys.prefix
system_pathsΒΆ
uninstall(pkgname, *args, **kwargs)[source]ΒΆ

A context manager which allows uninstallation of packages from the virtualenv

Parameters:pkgname (str) – The name of a package to uninstall
>>> venv = VirtualEnv("/path/to/venv/root")
>>> with venv.uninstall("pytz", auto_confirm=True, verbose=False) as uninstaller:
        cleaned = uninstaller.paths
>>> if cleaned:
        print("uninstalled packages: %s" % cleaned)

SubmodulesΒΆ

mork.virtualenv moduleΒΆ

class mork.virtualenv.PatchedUninstaller[source]ΒΆ

Bases: object

class mork.virtualenv.VirtualEnv(prefix=None, base_working_set=None, is_venv=True)[source]ΒΆ

Bases: object

activated(include_extras=True, extra_dists=[])[source]ΒΆ

A context manager which activates the virtualenv.

Parameters:extra_dists (list) – Paths added to the context after the virtualenv is activated.
This context manager sets the following environment variables:
  • PYTHONUSERBASE
  • VIRTUAL_ENV
  • PYTHONIOENCODING
  • PYTHONDONTWRITEBYTECODE

In addition, it activates the virtualenv inline by calling activate_this.py.

add_dist(dist_name)[source]ΒΆ
base_pathsΒΆ

Returns the context appropriate paths for the environment.

Returns:A dictionary of environment specific paths to be used for installation operations
Return type:dict

Note

The implementation of this is borrowed from a combination of pip and virtualenv and is likely to change at some point in the future.

>>> from pipenv.core import project
>>> from pipenv.environment import Environment
>>> env = Environment(prefix=project.virtualenv_location, is_venv=True, sources=project.sources)
>>> import pprint
>>> pprint.pprint(env.base_paths)
{'PATH': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/bin::/bin:/usr/bin',
'PYTHONPATH': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages',
'data': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW',
'include': '/home/hawk/.pyenv/versions/3.7.1/include/python3.7m',
'libdir': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages',
'platinclude': '/home/hawk/.pyenv/versions/3.7.1/include/python3.7m',
'platlib': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages',
'platstdlib': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7',
'prefix': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW',
'purelib': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/lib/python3.7/site-packages',
'scripts': '/home/hawk/.virtualenvs/pipenv-MfOPs1lW/bin',
'stdlib': '/home/hawk/.pyenv/versions/3.7.1/lib/python3.7'}
dist_is_in_project(dist)[source]ΒΆ
classmethod filter_sources(requirement, sources)[source]ΒΆ
find_egg(egg_dist)[source]ΒΆ
classmethod from_project_path(path)[source]ΒΆ

Utility for finding a virtualenv location based on a project path

get_distributions()[source]ΒΆ

Retrives the distributions installed on the library path of the virtualenv

Returns:A set of distributions found on the library path
Return type:iterator
get_finder()[source]ΒΆ
get_installed_packages()[source]ΒΆ
get_monkeypatched_pathset()[source]ΒΆ

Returns a monkeypatched UninstallPathset for using to uninstall packages from the virtualenv

Returns:A patched UninstallPathset which enables uninstallation of venv packages
Return type:pip._internal.req.req_uninstall.UninstallPathset
get_outdated_packages()[source]ΒΆ
get_package_info()[source]ΒΆ
get_setup_install_args(pkgname, setup_py, develop=False)[source]ΒΆ

Get setup.py install args for installing the supplied package in the virtualenv

Parameters:
  • pkgname (str) – The name of the package to install
  • setup_py (str) – The path to the setup file of the package
  • develop (bool) – Whether the package is in development mode
Returns:

The installation arguments to pass to the interpreter when installing

Return type:

list

classmethod get_sys_path(python_path)[source]ΒΆ

Get the sys.path data for a given python executable.

Parameters:python_path (str) – Path to a specific python executable.
Returns:The system path information for that python runtime.
Return type:list
get_working_set()[source]ΒΆ

Retrieve the working set of installed packages for the virtualenv.

Returns:The working set for the virtualenv
Return type:pkg_resources.WorkingSet
classmethod get_workon_home()[source]ΒΆ
initial_working_setΒΆ
install(req, editable=False, sources=[])[source]ΒΆ

Install a package into the virtualenv

Parameters:
  • req (requirementslib.models.requirement.Requirement) – A requirement to install
  • editable (bool) – Whether the requirement is editable, defaults to False
  • sources (list) – A list of pip sources to consult, defaults to []
Returns:

A return code, 0 if successful

Return type:

int

is_installed(pkgname)[source]ΒΆ

Given a package name, returns whether it is installed in the virtual environment

Parameters:pkgname (str) – The name of a package
Returns:Whether the supplied package is installed in the environment
Return type:bool
libdirΒΆ
locate_dist(dist)[source]ΒΆ
classmethod normalize_path(path)[source]ΒΆ
pathsΒΆ
pythonΒΆ

Path to the environment python

python_versionΒΆ
pyversionΒΆ
classmethod resolve_dist(dist, working_set)[source]ΒΆ

Given a local distribution and a working set, returns all dependencies from the set.

Parameters:
  • dist (pkg_resources.Distribution) – A single distribution to find the dependencies of
  • working_set (pkg_resources.WorkingSet) – A working set to search for all packages
Returns:

A set of distributions which the package depends on, including the package

Return type:

set(pkg_resources.Distribution)

run(cmd, cwd='.')[source]ΒΆ

Run a command with Popen in the context of the virtualenv

Parameters:
  • cmd (str or list) – A command to run in the virtual environment
  • cwd (str) – The working directory in which to execute the command, defaults to os.curdir
Returns:

A finished command object

Return type:

Popen

run_py(cmd, cwd='.')[source]ΒΆ

Run a python command in the virtualenv context.

Parameters:
  • cmd (str or list) – A command to run in the virtual environment - runs with python -c
  • cwd (str) – The working directory in which to execute the command, defaults to os.curdir
Returns:

A finished command object

Return type:

Popen

safe_import(name)[source]ΒΆ

Helper utility for reimporting previously imported modules while inside the venv

script_basedirΒΆ

Path to the environment scripts dir

scripts_dirΒΆ
setuptools_install(chdir_to, pkg_name, setup_py_path=None, editable=False)[source]ΒΆ

Install an sdist or an editable package into the virtualenv

Parameters:
  • chdir_to (str) – The location to change to
  • setup_py_path (str) – The path to the setup.py, if applicable defaults to None
  • editable (bool) – Whether the package is editable, defaults to False
sys_pathΒΆ

The system path inside the environment

Returns:The sys.path from the environment
Return type:list
sys_prefixΒΆ

The prefix run inside the context of the environment

Returns:The python prefix inside the environment
Return type:sys.prefix
system_pathsΒΆ
uninstall(pkgname, *args, **kwargs)[source]ΒΆ

A context manager which allows uninstallation of packages from the virtualenv

Parameters:pkgname (str) – The name of a package to uninstall
>>> venv = VirtualEnv("/path/to/venv/root")
>>> with venv.uninstall("pytz", auto_confirm=True, verbose=False) as uninstaller:
        cleaned = uninstaller.paths
>>> if cleaned:
        print("uninstalled packages: %s" % cleaned)

Indices and tablesΒΆ