Check if Python package is installed

What is a good way to check if a package is installed in a Python script? I know this easily from a translator, but I need to do this in a script.

I suppose I can check if the directory on the system created during installation exists, but I feel there is a better way. I am trying to make sure that Skype4Py is installed, and if not, I will install it.

My ideas for checking

  • check directory on typical installation path
  • try importing the package, and if an exception is thrown, install the package
+87
python package python-import skype
Jun 26 '09 at 20:54
source share
6 answers

If you mean a python script, just do something like this:

try: import mymodule except ImportError, e: pass # module doesn't exist, deal with it. 
+87
Jun 26 '09 at 21:00
source share

The best way to do this is:

 import pip installed_packages = pip.get_installed_distributions() 

Why is that? Sometimes there are conflicts of application names. Importing from the application namespace does not give you complete information about what is installed on the system.

As a result, you get a list of pkg_resources.Distribution objects. The following is an example:

 print installed_packages [ "Django 1.6.4 (/path-to-your-env/lib/python2.7/site-packages)", "six 1.6.1 (/path-to-your-env/lib/python2.7/site-packages)", "requests 2.5.0 (/path-to-your-env/lib/python2.7/site-packages)", ] 

Make a list:

 flat_installed_packages = [package.project_name for package in installed_packages] [ "Django", "six", "requests", ] 

Check if requests installed:

 if 'requests' in flat_installed_packages: # Do something 
+48
Dec 16 '14 at 1:29
source share

As with Python 3.3, you can use the find_spec () method

 import importlib.util import sys # For illustrative purposes. package_name = 'pandas' spec = importlib.util.find_spec(package_name) if spec is None: print(package_name +" is not installed") 
+22
Jan 23 '17 at 21:23
source share

If you want to receive a check from the terminal, you can run

 pip3 show package_name 

and if nothing is returned, the package will not be installed.

If you want to automate this check so that, for example, you can install it if it is missing, you can have the following in a bash script:

 pip3 show package_name 1>/dev/null #pip for Python 2 if [ $? == 0 ]; then echo "Installed" #Replace with your actions else echo "Not Installed" #Replace with your actions, 'pip3 install --upgrade package_name' ? fi 
+18
Sep 19 '17 at 2:31 on
source share

I would like to add some of my thoughts / conclusions on this topic. I am writing a script that checks all requirements for a user program. There are many checks with python modules.

There's a bit of a problem with

 try: import .. except: .. 

decision. In my case, one of the python modules called python-nmap , but you import it with import nmap and, as you can see, the name mismatch. Therefore, the test with the solution above returns the result False, and also imports the module on hit, but it may not be necessary to use large memory for a simple test / check.

I also found that

 import pip installed_packages = pip.get_installed_distributions() 

installed_packages will only install packages using pip . On my system, pip freeze returns through 40 python modules, and installed_packages only has 1 , the one I installed manually (python-nmap).

Another solution, below which I know it , may not be relevant to the question , but I think it’s good practice to keep a separate test function from the one that performs installing it may be useful for some.

A solution that worked for me. It is based on this answer. How to check if a python module exists without importing it

 from imp import find_module def checkPythonmod(mod): try: op = find_module(mod) return True except ImportError: return False 

NOTE. This solution cannot find a module named python-nmap either, I have to use nmap instead (easy to live with), but in this case the module will not be loaded into memory at all.

+1
Sep 07 '17 at 12:04 on
source share

Option Go # 2. If ImportError selected, then the package is not installed (or is not in sys.path ).

-one
Jun 26 '09 at 20:59
source share



All Articles