Python pip.get_installed_distributions() Examples

The following are 30 code examples of pip.get_installed_distributions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pip , or try the search function .
Example #1
Source File: environment.py    From modelforge with Apache License 2.0 6 votes vote down vote up
def collect_loaded_packages() -> List[Tuple[str, str]]:
    """
    Return the currently loaded package names and their versions.
    """
    dists = get_installed_distributions()
    get_dist_files = DistFilesFinder()
    file_table = {}
    for dist in dists:
        for file in get_dist_files(dist):
            file_table[file] = dist
    used_dists = set()
    # we greedily load all values to a list to avoid weird
    # "dictionary changed size during iteration" errors
    for module in list(sys.modules.values()):
        try:
            dist = file_table[module.__file__]
        except (AttributeError, KeyError):
            continue
        used_dists.add(dist)
    return sorted((dist.project_name, dist.version) for dist in used_dists) 
Example #2
Source File: backend.py    From aetros-cli with MIT License 6 votes vote down vote up
def collect_environment(self, overwrite_variables=None):
        import socket
        import os
        import pip
        import platform

        env = {}

        if not overwrite_variables:
            overwrite_variables = {}

        import aetros
        env['aetros_version'] = aetros.__version__
        env['python_version'] = platform.python_version()
        env['python_executable'] = sys.executable

        env['hostname'] = socket.gethostname()
        env['variables'] = dict(os.environ)
        env['variables'].update(overwrite_variables)

        if 'AETROS_SSH_KEY' in env['variables']: del env['variables']['AETROS_SSH_KEY']
        if 'AETROS_SSH_KEY_BASE64' in env['variables']: del env['variables']['AETROS_SSH_KEY_BASE64']

        env['pip_packages'] = sorted([[i.key, i.version] for i in pip.get_installed_distributions()])
        self.set_system_info('environment', env) 
Example #3
Source File: dep_check.py    From datasploit with GNU General Public License v3.0 6 votes vote down vote up
def check_dependency():
    list_deps = []
    missing_deps = []

    with open('requirements.txt') as f:
        list_deps = f.read().splitlines()

    pip_list = sorted([(i.key) for i in pip.get_installed_distributions()])

    for req_dep in list_deps:
        if req_dep not in pip_list:
            missing_deps.append(req_dep)

    if missing_deps:
        print "You are missing a module for Datasploit. Please install them using: "
        print "pip install -r requirements.txt"
        sys.exit() 
Example #4
Source File: debug_bundler.py    From f5-openstack-agent with Apache License 2.0 6 votes vote down vote up
def _save_pip_list(self, tar):
        '''Dump a pip list containing packages and versions.

        :param dest: unicode -- directory of dumped pip list
        :param tar: tarfile object -- tar where pip list dump will be added
        '''

        pkgs = pip.get_installed_distributions()
        sorted_pkgs = sorted(pkgs, key=lambda pkg: pkg._key)
        sorted_pkg_names = [str(pkg) for pkg in sorted_pkgs]
        pip_list_file = os.path.join(self.tar_dest, 'pip_list.txt')
        with open(pip_list_file, 'w') as pip_file:
            pip_file.write('\n'.join(sorted_pkg_names))
        self._add_file_to_tar(self.tar_dest, 'pip_list.txt', tar)
        os.remove(pip_list_file)
        return sorted_pkgs 
Example #5
Source File: debug_bundler.py    From f5-openstack-agent with Apache License 2.0 6 votes vote down vote up
def _save_pip_list(self, tar):
        '''Dump a pip list, containing packages and versions.

        :param dest: unicode -- directory of dumped pip list
        :param tar: tarfile object -- tar where pip list dump will be added
        '''

        pkgs = pip.get_installed_distributions()
        sorted_pkgs = sorted(pkgs, key=lambda pkg: pkg._key)
        sorted_pkg_names = [str(pkg) for pkg in sorted_pkgs]
        pip_list_file = os.path.join(self.tar_dest, 'pip_list.txt')
        with open(pip_list_file, 'w') as pip_file:
            pip_file.write('\n'.join(sorted_pkg_names))
        self._add_file_to_tar(self.tar_dest, 'pip_list.txt', tar)
        os.remove(pip_list_file)
        return sorted_pkgs 
Example #6
Source File: findpip.py    From conda-manager with MIT License 5 votes vote down vote up
def main():
    """Use pip to find pip installed packages in a given prefix."""
    pip_packages = {}
    for package in pip.get_installed_distributions():
        name = package.project_name
        version = package.version
        full_name = "{0}-{1}-pip".format(name.lower(), version)
        pip_packages[full_name] = {'version': version}
    data = json.dumps(pip_packages)
    print(data) 
Example #7
Source File: Rigol_functions.py    From DS1054Z_screen_capture with GNU General Public License v2.0 5 votes vote down vote up
def log_running_python_versions():
    logging.info("Python version: " + str(sys.version) + ", " + str(sys.version_info))  # () required in Python 3.

    installed_packages = pip.get_installed_distributions()
    installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])
    logging.info("Installed Python modules: " + str(installed_packages_list)) 
Example #8
Source File: setup.py    From resilient-python-api with MIT License 5 votes vote down vote up
def check_deps():
    # Fail if the 'co3' module is installed, this supersedes it
    packages = get_installed_distributions(local_only=True)
    # For each EggInfoDistribution, find its metadata
    for pkg in packages:
        try:
            distro = get_distribution(pkg.project_name)
            if distro.project_name == 'co3':
                print("This package replaces the 'co3' distribution.  Please 'pip uninstall co3' first.")
                sys.exit(1)
        except DistributionNotFound:
            pass 
Example #9
Source File: libscores.py    From automl_gpu with MIT License 5 votes vote down vote up
def show_version(scoring_version):
	''' Python version and library versions '''
	swrite('\n=== VERSIONS ===\n\n')
 	# Scoring program version
	swrite("Scoring program version: " + str(scoring_version) + "\n\n")
	# Python version
	swrite("Python version: " + version + "\n\n")
	# Give information on the version installed
	swrite("Versions of libraries installed:\n")
	map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()])) 
Example #10
Source File: data_io.py    From automl_gpu with MIT License 5 votes vote down vote up
def show_version():
	# Python version and library versions
	swrite('\n=== VERSIONS ===\n\n')
	# Python version
	swrite("Python version: " + version + "\n\n")
	# Give information on the version installed
	swrite("Versions of libraries installed:\n")
	map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()])) 
Example #11
Source File: libscores.py    From AutoML with MIT License 5 votes vote down vote up
def show_version(scoring_version):
	''' Python version and library versions '''
	swrite('\n=== VERSIONS ===\n\n')
 	# Scoring program version
	swrite("Scoring program version: " + str(scoring_version) + "\n\n")
	# Python version
	swrite("Python version: " + version + "\n\n")
	# Give information on the version installed
	swrite("Versions of libraries installed:\n")
	map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()])) 
Example #12
Source File: data_io.py    From AutoML with MIT License 5 votes vote down vote up
def show_version():
	# Python version and library versions
	swrite('\n=== VERSIONS ===\n\n')
	# Python version
	swrite("Python version: " + version + "\n\n")
	# Give information on the version installed
	swrite("Versions of libraries installed:\n")
	map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()])) 
Example #13
Source File: Settings.py    From neo-python with MIT License 5 votes vote down vote up
def check_depdendencies():
    """
    Makes sure that all required dependencies are installed in the exact version
    (as specified in requirements.txt)
    """
    # Get installed packages
    installed_packages = pip.get_installed_distributions(local_only=False)
    installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])

    # Now check if each package specified in requirements.txt is actually installed
    deps_filename = os.path.join(ROOT_INSTALL_PATH, "requirements.txt")
    with open(deps_filename, "r") as f:
        for dep in f.read().split():
            if not dep.lower() in installed_packages_list:
                raise SystemCheckError("Required dependency %s is not installed. Please run 'pip install -e .'" % dep) 
Example #14
Source File: dl_image_toolbox_utils.py    From dataiku-contrib with Apache License 2.0 5 votes vote down vote up
def can_use_gpu():
    # Check that 'tensorflow-gpu' is installed on the current code-env
    import pip
    installed_packages = pip.get_installed_distributions()
    return "tensorflow-gpu" in [p.project_name for p in installed_packages]


###################################################################################################################
## FILES LOGIC
################################################################################################################### 
Example #15
Source File: CheckDependencies.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def Check(self):
        try:
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                module = __import__(self.module)
        except ImportError:
            raise MissingDependency
        if self.attr and hasattr(module, self.attr):
            version = getattr(module, self.attr)
        elif hasattr(module, "__version__"):
            version = module.__version__
        elif hasattr(module, "VERSION"):
            version = module.VERSION
        elif hasattr(module, "version"):
            version = module.version
        else:
            result = [
                p.version
                for p in pip.get_installed_distributions()
                if str(p).startswith(self.name + " ")
            ]
            if result:
                version = result[0]
            else:
                raise Exception("Can't get version information")
        if not isinstance(version, basestring):
            version = ".".join(str(x) for x in version)
        if CompareVersion(version, self.version) < 0:
            raise WrongVersion 
Example #16
Source File: views.py    From pywebdriver with GNU General Public License v3.0 5 votes vote down vote up
def system():
    pywebdriver_info = []
    pywebdriver_info.append({
        'name': _('CORS allowed origins'),
        'value': config.get('flask','cors_origins')
    })
    system_info = []
    system_info.append({
        'name': _('OS - System'), 'value': platform.system()})
    system_info.append({
        'name': _('OS - Distribution'),
        'value': platform.linux_distribution()})
    system_info.append({
        'name': _('OS - Release'), 'value': platform.release()})
    system_info.append({
        'name': _('OS - Version'), 'value': platform.version()})
    system_info.append({
        'name': _('Machine'), 'value': platform.machine()})
    system_info.append({
        'name': _('Python Version'), 'value': platform.python_version()})
    installed_python_packages = get_installed_distributions()
    installed_python_packages = sorted(
        installed_python_packages, key=lambda package: package.key)
    return render_template(
        'system.html',
        pywebdriver_info=pywebdriver_info,
        system_info=system_info,
        installed_python_packages=installed_python_packages
    ) 
Example #17
Source File: 1_basic_commands.py    From wharfee with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def step_cli_installed(context):
    """
    Make sure wharfee is in installed packages.
    """
    dists = set([di.key for di in pip.get_installed_distributions()])
    assert 'wharfee' in dists 
Example #18
Source File: check.py    From douban.fm with MIT License 5 votes vote down vote up
def is_latest(package_name):
    pypi = xmlrpc_client.ServerProxy('https://pypi.python.org/pypi')
    for dist in pip.get_installed_distributions():
        if dist.project_name == package_name:
            available = pypi.package_releases(dist.project_name)
            if available[0] != dist.version:
                return False
            return True 
Example #19
Source File: py.py    From lux with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _safe_dist():
    for p in pip.get_installed_distributions():
        try:
            yield p.key, p.version
        except Exception:   # pragma    nocover
            pass 
Example #20
Source File: data_io.py    From automl-phase-2 with MIT License 5 votes vote down vote up
def show_version():
	# Python version and library versions
	swrite('\n=== VERSIONS ===\n\n')
	# Python version
	swrite("Python version: " + version + "\n\n")
	# Give information on the version installed
	swrite("Versions of libraries installed:\n")
	map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()])) 
Example #21
Source File: libscores.py    From automl-phase-2 with MIT License 5 votes vote down vote up
def show_version(scoring_version):
	''' Python version and library versions '''
	swrite('\n=== VERSIONS ===\n\n')
 	# Scoring program version
	swrite("Scoring program version: " + str(scoring_version) + "\n\n")
	# Python version
	swrite("Python version: " + version + "\n\n")
	# Give information on the version installed
	swrite("Versions of libraries installed:\n")
	map(swrite, sorted(["%s==%s\n" % (i.key, i.version) for i in lib()])) 
Example #22
Source File: util_cplat_packages.py    From ibeis with Apache License 2.0 5 votes vote down vote up
def get_pip_installed():
    try:
        import pip
        pypkg_list = [item.key for item in pip.get_installed_distributions()]
        return pypkg_list
    except ImportError:
        #out, err, ret = shell('pip list')
        #if ret == 0:
        #    pypkg_list = [_.split(' ')[0] for  _ in out.split('\n')]
        return [] 
Example #23
Source File: win32bootstrap.py    From ibeis with Apache License 2.0 5 votes vote down vote up
def get_uninstalled_project_names():
    try:
        import pip
        pkg_set = set(KNOWN_PKG_LIST)
        pathmeta_list = pip.get_installed_distributions()
        installed_set = set([meta.project_name for meta in pathmeta_list])
        uninstalled_set = pkg_set.difference(installed_set)
        uninstalled_list = list(uninstalled_set)
    except Exception as ex:
        print(ex)
        uninstalled_list = KNOWN_PKG_LIST
    return uninstalled_list 
Example #24
Source File: validator_info_tool.py    From indy-plenum with Apache License 2.0 5 votes vote down vote up
def _generate_software_info(self):
        os_version = self._prepare_for_json(platform.platform())
        installed_packages = [self._prepare_for_json(pack) for pack in pip.get_installed_distributions()]
        output = self._run_external_cmd("dpkg-query --list | grep indy")
        indy_packages = output.split(os.linesep)
        return {
            "Software": {
                "OS_version": os_version,
                "Installed_packages": installed_packages,
                # TODO add this field
                "Indy_packages": self._prepare_for_json(indy_packages),
            }
        } 
Example #25
Source File: dl_image_toolbox_utils.py    From dataiku-contrib with Apache License 2.0 5 votes vote down vote up
def can_use_gpu():
    # Check that 'tensorflow-gpu' is installed on the current code-env
    import pip
    installed_packages = pip.get_installed_distributions()
    return "tensorflow-gpu" in [p.project_name for p in installed_packages]


###################################################################################################################
## FILES LOGIC
################################################################################################################### 
Example #26
Source File: __init__.py    From indy-node with Apache License 2.0 4 votes vote down vote up
def setup_plugins():
    import sys
    import os
    import pip
    import importlib    # noqa
    from importlib.util import module_from_spec, spec_from_file_location    # noqa: E402
    from indy_common.config_util import getConfigOnce   # noqa: E402
    import indy_node.server.plugin     # noqa: E402

    def find_and_load_plugin(plugin_name, plugin_root, installed_packages):
        if plugin_name in installed_packages:
            # TODO: Need a test for installed packages
            plugin_name = plugin_name.replace('-', '_')
            plugin = importlib.import_module(plugin_name)
        else:
            plugin_path = os.path.join(plugin_root.__path__[0],
                                       plugin_name, '__init__.py')
            spec = spec_from_file_location('__init__.py', plugin_path)
            plugin = module_from_spec(spec)
            spec.loader.exec_module(plugin)

        return plugin

    # TODO: Refactor to use plenum's setup_plugins
    # TODO: Should have a check to make sure no plugin defines any conflicting ledger id or request field
    global PLUGIN_LEDGER_IDS
    global PLUGIN_CLIENT_REQUEST_FIELDS
    global PLUGIN_CLIENT_REQ_OP_TYPES

    config = getConfigOnce(ignore_external_config_update_errors=True)

    plugin_root = config.PLUGIN_ROOT
    try:
        plugin_root = importlib.import_module(plugin_root)
    except ImportError:
        raise ImportError('Incorrect plugin root {}. No such package found'.
                          format(plugin_root))
    sys.path.insert(0, plugin_root.__path__[0])
    enabled_plugins = config.ENABLED_PLUGINS
    installed_packages = {p.project_name: p for p in pip.get_installed_distributions()}
    for plugin_name in enabled_plugins:
        plugin = find_and_load_plugin(plugin_name, plugin_root, installed_packages)
        plugin_globals = plugin.__dict__
        # The following lines are idempotent so loading the same plugin twice is not the problem.
        if 'LEDGER_IDS' in plugin_globals:
            PLUGIN_LEDGER_IDS.update(plugin_globals['LEDGER_IDS'])
        if 'CLIENT_REQUEST_FIELDS' in plugin_globals:
            PLUGIN_CLIENT_REQUEST_FIELDS.update(plugin_globals['CLIENT_REQUEST_FIELDS'])
        if 'REQ_OP_TYPES' in plugin_globals:
            PLUGIN_CLIENT_REQ_OP_TYPES.update(plugin_globals['REQ_OP_TYPES'])

    # Reloading message types since some some schemas would have been changed
    import indy_common.types
    importlib.reload(indy_common.types) 
Example #27
Source File: systemdata.py    From smarthome with GNU General Public License v3.0 4 votes vote down vote up
def getpackages(self):
        """
        returns a list with the installed python packages and its versions
        """

        # check if pypi service is reachable
        if self.pypi_timeout <= 0:
            pypi_available = False
#            pypi_unavailable_message = translate('PyPI Prüfung deaktiviert')
            pypi_unavailable_message = 'PyPI Prüfung deaktiviert'
        else:
            pypi_available = True
            try:
                import socket
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.settimeout(self.pypi_timeout)
                #                sock.connect(('pypi.python.org', 443))
                sock.connect(('pypi.org', 443))
                sock.close()
            except:
                pypi_available = False
#                pypi_unavailable_message = translate('PyPI nicht erreichbar')
                pypi_unavailable_message = 'PyPI nicht erreichbar'

        import pip
        import xmlrpc
        installed_packages = pip.get_installed_distributions()
        #        pypi = xmlrpc.client.ServerProxy('https://pypi.python.org/pypi')
        pypi = xmlrpc.client.ServerProxy('https://pypi.org/pypi')
        packages = []
        for dist in installed_packages:
            package = {}
            package['key'] = dist.key
            package['version_installed'] = dist.version
            if pypi_available:
                try:
                    available = pypi.package_releases(dist.project_name)
                    try:
                        package['version_available'] = available[0]
                    except:
                        package['version_available'] = '-'
                except:
#                    package['version_available'] = [translate('Keine Antwort von PyPI')]
                    package['version_available'] = ['Keine Antwort von PyPI']
            else:
                package['version_available'] = pypi_unavailable_message
            packages.append(package)

        sorted_packages = sorted([(i['key'], i['version_installed'], i['version_available']) for i in packages])
        return sorted_packages 
Example #28
Source File: pipdeptree.py    From pipdeptree with MIT License 4 votes vote down vote up
def main():
    args = _get_args()

    pkgs = get_installed_distributions(local_only=args.local_only,
                                       user_only=args.user_only)

    tree = PackageDAG.from_pkgs(pkgs)

    is_text_output = not any([args.json, args.json_tree, args.output_format])

    return_code = 0

    # Before any reversing or filtering, show warnings to console
    # about possibly conflicting or cyclic deps if found and warnings
    # are enabled (ie. only if output is to be printed to console)
    if is_text_output and args.warn != 'silence':
        conflicts = conflicting_deps(tree)
        if conflicts:
            render_conflicts_text(conflicts)
            print('-'*72, file=sys.stderr)

        cycles = cyclic_deps(tree)
        if cycles:
            render_cycles_text(cycles)
            print('-'*72, file=sys.stderr)

        if args.warn == 'fail' and (conflicts or cycles):
            return_code = 1

    # Reverse the tree (if applicable) before filtering, thus ensuring
    # that the filter will be applied on ReverseTree
    if args.reverse:
        tree = tree.reverse()

    show_only = set(args.packages.split(',')) if args.packages else None
    exclude = set(args.exclude.split(',')) if args.exclude else None

    if show_only is not None or exclude is not None:
        tree = tree.filter(show_only, exclude)

    if args.json:
        print(render_json(tree, indent=4))
    elif args.json_tree:
        print(render_json_tree(tree, indent=4))
    elif args.output_format:
        output = dump_graphviz(tree,
                               output_format=args.output_format,
                               is_reverse=args.reverse)
        print_graphviz(output)
    else:
        render_text(tree, args.all, args.freeze)

    return return_code 
Example #29
Source File: dep_check.py    From Belati with GNU General Public License v2.0 4 votes vote down vote up
def check_dependency(self):
        list_deps = []
        missing_deps = []

        with open('requirements.txt') as f:
            list_deps = f.read().splitlines()

	dists = [d for d in pkg_resources.working_set]
	pip_list = sorted([(i.key) for i in dists])
        #pip_list = sorted([(i.key) for i in pip.get_installed_distributions()])

        for req_dep in list_deps:
            compare_char = ["==", ">=", "<=", ">", "<", "!="]
            for c in compare_char:
                if c in req_dep:
                    pkg = req_dep.split(c)
                    if pkg[0] not in pip_list:
                        missing_deps.append(req_dep)
                        break
                    else:
                        installed_ver = pkg_resources.get_distribution(pkg[0]).version
                        if self.get_truth(installed_ver, c, pkg[1]):
                            break
                        else:
                            missing_deps.append(req_dep)                            
                else:
                    if req_dep not in pip_list:
                        # Why this package is not in get_installed_distributions ?
                        if str(req_dep) == "argparse":
                            pass
                        else:
                            missing_deps.append(req_dep)

        missing_deps = set(missing_deps)
        if missing_deps:
            missing_deps_warning ="""
            You are missing a module required for Belati. In order to continue using Belati, please install them with:

            {}`pip install --upgrade --force-reinstall -r requirements.txt`{}

            or manually install missing modules with:

            {}`pip install --upgrade --force-reinstall {}`{}

            """

            log.console_log(missing_deps_warning.format(Y, W, Y, ' '.join(missing_deps), W))
            sys.exit() 
Example #30
Source File: pkg.py    From marvin-python-toolbox with Apache License 2.0 4 votes vote down vote up
def update(ctx, install, install_all):
    base_path = ctx.obj['base_path']
    repos = get_repos_from_requirements(base_path)
    required_versions = get_tag_from_repo_url(repos)
    latest_versions = get_latest_tags_from_repos(repos)
    installed_pkgs = pip.get_installed_distributions()
    install_list = ['-e .']
    click.echo('')
    for repo in repos:
        latest = latest_versions[repo]
        required = required_versions[repo]
        try:
            pkg_name = repo.split('egg=')[1]
        except IndexError:
            continue
        pkg_name_normalized = pkg_name.lower().replace('_', '-')
        installed = 'unknown'
        installed_list = [
            pkg.version
            for pkg in installed_pkgs
            if pkg.key in [pkg_name_normalized, pkg_name_normalized + '-lib']
        ]
        if installed_list:
            installed = 'v{}'.format(installed_list[0])

        if LooseVersion(required) < LooseVersion(latest):
            click.echo('Updating {} from {} to {}...'.format(pkg_name, required, latest))
            new_repo = update_repo_tag(repo, latest, path=base_path)
            if LooseVersion(installed) < LooseVersion(latest):
                install_list.append(new_repo)
        elif LooseVersion(installed) < LooseVersion(required):
            install_list.append(repo)
    if install_all:
        install = True
        install_list = ['-r requirements.txt']
    if install:
        for new_repo in install_list:
            new_repo = new_repo.strip()
            click.echo('')
            click.echo('Running `pip install -U {}` ...'.format(new_repo))
            command = ['pip', 'install', '-U'] + new_repo.split(' ')
            exitcode = subprocess.call(command, cwd=base_path)
            if exitcode == 0:
                click.echo('Done.')
            else:
                click.echo('Failed.')
                sys.exit(exitcode)