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)