How do I check whether a module is installed in Python, and install it if needed?
In terminal, after I start Python, how will I know what are the modules present in python? Suppose I need to learn the modules NumPy and SciPy.
- How will I install it if it is not installed?
- How will I know if it is already installed?
How to know if a python module is installed or not in the system: You can do a very easy test in terminal,
$ python -c "import math"
$ echo $?
0 # math module exists in system
$ python -c "import numpy"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named numpy
$ echo $?
1 # numpy module does not exist in system
How will I install it if it is not installed
You can install specific module by downloading respective packages from repository, for example you can install scipy
as,
sudo apt-get install python-scipy ## for Python2
sudo apt-get install python3-scipy ## for Python3
Alternately You can also install a python module using python-pip
as suggested by Zack Titan in the comment below, To install numpy
you can use
pip install numpy
Warning: It is highly recommended to install python-modules using official Ubuntu repository only and not to use the pip
method as superuser(i.e., as root
or using sudo
). In some cases it may leave your system unusable by breaking system python.
How to install packages using pip
into local virtual environment.
In case we do not want to unwantedly import a module in question (which would happen in a try
statement) we can make use of sys.modules
to test modules that are installed and were imported before.
In the python shell issue:
>>> import sys
Then test for installed modules:
>>> 'numpy' in sys.modules
True
>>> 'scipy' in sys.modules
False
Note that only those modules that were imported before give True
on this test, all other modules (even if installed) result in False.
Another alternative to try
an import
statement in the python console is calling the inbuilt help()
function. This will not give a documentation for non-installed modules, e.g.
>>> help('scipy')
no Python documentation found for 'scipy'
The output of very long help documents of installed modules can be interrupted with Q.
Now to install missing modules it is recommended to use the Ubuntu package management (and not the Python pip way) because we need root access and also to prevent messing up our heavily Python-dependend system. For the module in question this would e.g. be:
sudo apt-get install python-scipy ## for Python2
sudo apt-get install python3-scipy ## for Python3
After installation we then can add them to the sys.modules
dictionary by importing them once.
Another way is the pkgutil
module. Works with both Python 2 & 3:
python -c 'import pkgutil; print(1 if pkgutil.find_loader("module") else 0)'
You need to replace module
with the name of your module, example:
$ python -c 'import pkgutil; print(1 if pkgutil.find_loader("math") else 0)'
1
I know the OP originally asked for a solution after starting Python, but outside of python I use pip
. On ubuntu: sudo apt-get install python-pip
, if it's not already installed.
Then to see what third party modules are available, just run:
pip freeze
Or even
pip list
And both will show you all modules installed and their versions.
If the module you're looking for is not installed, most of the time you can easily install it with pip:
pip install <module-name>
If you're not sure of whether a module exists or what its PyPI name is, use pip search
:
pip search <keyword>
You could put the code inside try
, except
block.
$ python3 -c "\
try:
import cow
print('\nModule was installed')
except ImportError:
print('\nThere was no such module installed')"
There was no such module installed
$ python3 -c "\
try:
import regex
print('\nModule was installed')
except ImportError:
print('\nThere was no such module installed')"
Module was installed