Welcome to morkβs documentation!ΒΆ
mork: A project for installing packages across the virtualenv boundary.ΒΆ
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
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.
-
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'}
-
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_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_setup_install_args
(pkgname, setup_py, develop=False)[source]ΒΆ Get setup.py install args for installing the supplied package in the virtualenv
Parameters: Returns: The installation arguments to pass to the interpreter when installing
Return type:
-
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
-
initial_working_set
ΒΆ
-
install
(req, editable=False, sources=[])[source]ΒΆ Install a package into the virtualenv
Parameters: Returns: A return code, 0 if successful
Return type:
-
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
ΒΆ
-
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
)- dist (
-
run
(cmd, cwd='.')[source]ΒΆ Run a command with
Popen
in the context of the virtualenvParameters: Returns: A finished command object
Return type:
-
run_py
(cmd, cwd='.')[source]ΒΆ Run a python command in the virtualenv context.
Parameters: Returns: A finished command object
Return type:
-
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:
-
sys_path
ΒΆ The system path inside the environment
Returns: The sys.path
from the environmentReturn 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.
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.
-
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'}
-
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_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_setup_install_args
(pkgname, setup_py, develop=False)[source]ΒΆ Get setup.py install args for installing the supplied package in the virtualenv
Parameters: Returns: The installation arguments to pass to the interpreter when installing
Return type:
-
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
-
initial_working_set
ΒΆ
-
install
(req, editable=False, sources=[])[source]ΒΆ Install a package into the virtualenv
Parameters: Returns: A return code, 0 if successful
Return type:
-
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
ΒΆ
-
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
)- dist (
-
run
(cmd, cwd='.')[source]ΒΆ Run a command with
Popen
in the context of the virtualenvParameters: Returns: A finished command object
Return type:
-
run_py
(cmd, cwd='.')[source]ΒΆ Run a python command in the virtualenv context.
Parameters: Returns: A finished command object
Return type:
-
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:
-
sys_path
ΒΆ The system path inside the environment
Returns: The sys.path
from the environmentReturn 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)
-