1

I use the pip module in a python script to automate the installation of software / modules . How can I do to check if the (remote) software / module exists? I have found nothing in the pip module that allows to do that.

My code :

class install_pip:
    def __init__(self):
        self._liste=['install']
    def install(self):
        pip.main(self._liste)
    def addsoftware(self, software):
        if type(software) is str:
            self._liste.append(software)
        if type(software) is list:
            for i in software:
                self._liste.append(i)
    def delsoftware(self, software):
        if type(software) is str:
            self._liste.remove(software)
        if type(software) is list:
            for i in software:
                self._liste.remove(i)
    def _return(self):
        return self._liste[1:len(self._liste)]
    list = property(_return)

I want check if 'software' exist. Thanks.

Edit: I tried this code:

try:
    pip.main(['install', 'nonexistentpackage'])
except pip.InstallationError as err:
    print(echec)

But I dont get any error...

4 Answers 4

3

I would do this:

import requests
response = requests.get("http://pypi.python.org/pypi/{}/json"
                        .format(module_name))
if response.status_code == 200:
    # OK, the json document exists so you can
    # parse the module details if you want
    # by using data = response.json()
    #
    # anyway, here you know that the module exists!
    ...
Sign up to request clarification or add additional context in comments.

Comments

0

I realize this is an old question, but this works for me.

from importlib import util

found = True if util.find_spec('packagename') is not None else False
if found:
    import packagename
else:
    pip.main(['install', packagename])
    import packagename

Comments

0

async function check() {
  const packages = document.querySelector('textarea').value.split('\n')
  const settled = await Promise.allSettled(packages.map(n=>fetch(`https://pypi.org/pypi/${n}/json`, {method: 'HEAD'})))
  document.all.output.innerHTML = settled.map((s, i) => s.status === 'fulfilled' ? `<font color="green"><b>${packages[i]}</b></font>` : packages[i]).join('\n')
}
check()
<textarea oninput="check()" rows="5">requests&#10;nonexistentpackage</textarea>
<pre id=output></pre>

Comments

-2

The following code will attempt to import a package (package is of type 'str'). If it is unable to import the package (i.e. it isn't installed) it will call Pip and attempt to install the package.

import pip

def import_or_install(package):
    try:
        __import__(package)
        print (package, "exists, and was successfully imported.")
    except ImportError:
        pip.main(['install', package])

import_or_install("name of package")

1 Comment

Thank but that does not test if the remote package exists, is there a Pythonic way to access the cache of installable software ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.