42

I want to check if a module exists, if it doesn't I want to install it.

How should I do this?

So far I have this code which correctly prints f if the module doesn't exist.

try:
    import keyring
except ImportError:
    print 'f'
Lii
  • 11,553
  • 8
  • 64
  • 88
dotty
  • 40,405
  • 66
  • 150
  • 195
  • 2
    This will work in a script and check whether a module exists, but installing the module is a different case altogether. – user225312 Dec 24 '10 at 17:35
  • 2
    turns out using os.system() works. – dotty Dec 24 '10 at 17:37
  • subprocess.Popen is preferred to os.system. – Jason R. Coombs Dec 24 '10 at 17:53
  • Does this answer your question? [How to check if a module is installed in Python and, if not, install it within the code?](https://stackoverflow.com/questions/44210656/how-to-check-if-a-module-is-installed-in-python-and-if-not-install-it-within-t) – alelom Apr 13 '23 at 15:24
  • This question is very old, and most answers, including the accepted ones and the trending ones, are outdated. I think [the best, most up-to-date answer is in a duplicate of this question](https://stackoverflow.com/a/44210735/3873799). For this reason, I chose to flag this question as duplicate instead of the newer one. People should be forwarded to the newer copy. – alelom Apr 13 '23 at 15:25

12 Answers12

52
import pip

def import_or_install(package):
    try:
        __import__(package)
    except ImportError:
        pip.main(['install', package])       

This code simply attempt to import a package, where package is of type str, and if it is unable to, calls pip and attempt to install it from there.

ewil
  • 529
  • 1
  • 4
  • 4
22

Here is how it should be done, and if I am wrong, please correct me. However, Noufal seems to confirm it in another answer to this question, so I guess it's right.

When writing the setup.py script for some scripts I wrote, I was dependent on the package manager of my distribution to install the required library for me.

So, in my setup.py file, I did this:

package = 'package_name'
try:
    return __import__(package)
except ImportError:
    return None

So if package_name was installed, fine, continue. Else, install it via the package manager which I called using subprocess.

kmonsoor
  • 7,600
  • 7
  • 41
  • 55
user225312
  • 126,773
  • 69
  • 172
  • 181
  • This is useful in principle, however it does not highlight a possible implementation of `__import__()`. See this answer for it: https://stackoverflow.com/a/44210735/3873799 – alelom Apr 13 '23 at 15:30
12

NOTE: Ipython / Jupyter specific solution.

While using notebooks / online kernels, I usually do it using systems call.

try:
  import keyring
except:
  !pip install keyring
  import keyring

P.S. One may wish to call conda install or mamba install instead.

Divyanshu Srivastava
  • 1,379
  • 11
  • 24
  • 1
    what is `!pip install pulp` ? – Jingnan Jia Oct 28 '20 at 15:28
  • IPython runs any command starting with `!` on the shell .. Read this [link](https://jakevdp.github.io/PythonDataScienceHandbook/01.05-ipython-and-shell-commands.html#Shell-Commands-in-IPython). Thus, `!pip install pulp` will install pulp using pip from within jupyter – Divyanshu Srivastava Oct 28 '20 at 18:24
  • This question is not about IPython/Jupyer, and even then a better solution is to invoke with the `!` a `subprocess` command, as described in https://stackoverflow.com/a/44210735/3873799 – alelom Apr 13 '23 at 15:29
11

This approach of dynamic import work really well in cases you just want to print a message if module is not installed. Automatically installing a module SHOULDN'T be done like issuing pip via subprocess. That's why we have setuptools (or distribute).

We have some great tutorials on packaging, and the task of dependencies detection/installation is as simple as providing install_requires=[ 'FancyDependency', 'otherFancy>=1.0' ]. That's just it!

But, if you really NEED to do by hand, you can use setuptools to help you.

from pkg_resources import WorkingSet , DistributionNotFound
working_set = WorkingSet()

# Printing all installed modules
print tuple(working_set)

# Detecting if module is installed
try:
    dep = working_set.require('paramiko>=1.0')
except DistributionNotFound:
    pass

# Installing it (anyone knows a better way?)
from setuptools.command.easy_install import main as install
install(['django>=1.2'])
BrDaHa
  • 5,138
  • 5
  • 32
  • 47
Frangossauro
  • 4,446
  • 4
  • 23
  • 15
  • Here's a [snapshot](https://web.archive.org/web/20140715201828/http://guide.python-distribute.org/) of the dead link. – MCL Dec 03 '19 at 21:23
4

You can use os.system as follows:

import os

package = "package_name"

try:
    __import__package
except:
    os.system("pip install "+ package)
lsr729
  • 752
  • 2
  • 11
  • 25
1

You can launch pip install %s"%keyring in the except part to do this but I don't recommend it. The correct way is to package your application using distutils so that when it's installed, dependencies will be pulled in.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
1

Not all modules can be installed so easily. Not all of them have easy-install support, some can only be installed by building them.. others require some non-python prerequisites, like gcc, which makes things even more complicated (and forget about it working well on Windows).

So I would say you could probably make it work for some predetermined modules, but there's no chance it'll be something generic that works for any module.

Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
1

I made an import_neccessary_modules() function to fix this common issue.

# ======================================================================================
# == Fix any missing Module, that need to be installed with PIP.exe. [Windows System] ==
# ======================================================================================
import importlib, os
def import_neccessary_modules(modname:str)->None:
    '''
        Import a Module,
        and if that fails, try to use the Command Window PIP.exe to install it,
        if that fails, because PIP in not in the Path,
        try find the location of PIP.exe and again attempt to install from the Command Window.
    '''
    try:
        # If Module it is already installed, try to Import it
        importlib.import_module(modname)
        print(f"Importing {modname}")
    except ImportError:
        # Error if Module is not installed Yet,  the '\033[93m' is just code to print in certain colors
        print(f"\033[93mSince you don't have the Python Module [{modname}] installed!")
        print("I will need to install it using Python's PIP.exe command.\033[0m")
        if os.system('PIP --version') == 0:
            # No error from running PIP in the Command Window, therefor PIP.exe is in the %PATH%
            os.system(f'PIP install {modname}')
        else:
            # Error, PIP.exe is NOT in the Path!! So I'll try to find it.
            pip_location_attempt_1 = sys.executable.replace("python.exe", "") + "pip.exe"
            pip_location_attempt_2 = sys.executable.replace("python.exe", "") + "scripts\pip.exe"
            if os.path.exists(pip_location_attempt_1):
                # The Attempt #1 File exists!!!
                os.system(pip_location_attempt_1 + " install " + modname)
            elif os.path.exists(pip_location_attempt_2):
                # The Attempt #2 File exists!!!
                os.system(pip_location_attempt_2 + " install " + modname)
            else:
                # Neither Attempts found the PIP.exe file, So i Fail...
                print(f"\033[91mAbort!!!  I can't find PIP.exe program!")
                print(f"You'll need to manually install the Module: {modname} in order for this program to work.")
                print(f"Find the PIP.exe file on your computer and in the CMD Command window...")
                print(f"   in that directory, type    PIP.exe install {modname}\033[0m")
                exit()


import_neccessary_modules('art')
import_neccessary_modules('pyperclip')
import_neccessary_modules('winsound')
David Buck
  • 3,752
  • 35
  • 31
  • 35
1

Here is my approach. The idea is loop until python has already installed all modules by built in module as "pip" .

import pip

while True:
    
    try:
        #import your modules here. !
        import seaborn
        import bokeh

        break

    except ImportError as err_mdl:
        
        print((err_mdl.name))
        pip.main(['install', err_mdl.name])
0

I tried installing transformers using the below method and it worked fine. Similarly, you can just replace your library name instead of "transformers".


import pip
try:
    from transformers import pipeline
except ModuleNotFoundError:
    pip.main(['install', "transformers"])
    from transformers import pipeline
Subhamp7
  • 29
  • 5
0

I tried this in a new virtual envoirnment with no packages installed and it installed the necessary package i.e. opencv-python Example is given below

import os

try:
    import cv2
except ImportError:
    os.system('pip install opencv-python')
0

To reproduce this code, you must first install the instld library.

try:
    import some_module
except ImportError:
    import installed
    with installed('some_package'):
        import some_module

Here we first try to import the module, and in case of an error, install it directly at runtime and then import it.

Evgeniy Blinov
  • 351
  • 3
  • 3