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?
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 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
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.
sys.modules only contains modules that have already been imported, so it is not a reliable way to test whether a module has been installed. The most reliable test is to use a try/except and trap the ImportError, as several others have already suggested.
– ekhumoro
Feb 22 '15 at 19:16
sys.modules is entirely irrelevant and I think any mention of it is misleading. But maybe that's just me.
– David Z
Feb 23 '15 at 07:32
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
print() will also work fine in Python 2; It's treated as a the group-with-parens syntax, and doesn't have any side effects. Only when you want to add multiple items (as in print('a', 'b') it will be treated as a tuple, in which you do need the from __future__ import print_function, but that's not applicable in this case, you can just write forward-compatible code by adding the parens (I tested my changes in Python 2 and 3).
– Martin Tournoij
Feb 23 '15 at 01:24
exit so it can be used in shell context: python -c 'import pkgutil; exit(0 if pkgutil.find_loader("math") else 2)'
– A T
Sep 25 '19 at 11:24
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>
pip search <keyword>
It appears that pip search no longer functions. (See @AJM's comment below). I tend to use Pypi's search directly
pip show <module-name> will show whether a package is installed, e.g. pip show numpy.
– Herpes Free Engineer
Jul 04 '18 at 18:20
pip search no longer works: PyPI's XMLRPC API has been disabled. There's information on why this decision was made at https://www.theregister.com/2021/05/25/pypi_search_error/ - but that article was written when the disablement was only temporary. An email announcement was sent out in January making it permanent.
– AJM
May 09 '22 at 19:15
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
To provide another answer, for completion's sake:
You can (ab)use the -m option. From Python's manpage:
-m module-name
Searches sys.path for the named module and runs the correspond‐
ing .py file as a script.
Which will give us:
$ python2 -m numpy
/sbin/python2: No module named numpy.__main__; 'numpy' is a package and cannot be directly executed
$ python2 -m math
/sbin/python2: No code object available for math
But for non-existent modules, it will give us:
$ python2 -m doesnt_exist
/sbin/python2: No module named doesnt_exist
We could use grep to match for this:
$ python2 -m doesnt_exist |& grep -q 'No module named' && echo 'Nope' || echo 'Yup'
Nope
$ python2 -m math |& grep -q 'No module named' && echo 'Nope' || echo 'Yup'
Yup
This is slightly hack-ish, and not what -m was intended for; but it is the
method that requires the least typing if you want a quick test :-)
/dev/null over the years :-/
– Martin Tournoij
Feb 23 '15 at 01:25
I wrote an example in Python:
import pip
import sys
from bigml.api import BigML
if not 'bigml' in sys.modules.keys():
pip.main(['install', 'bigml'])
I found that in order to make my infrastructure provisioning** idempotent, I need to be able to check for a package from the shell in a oneliner. I built on @cuonglm's answer. I had to reverse the 1 and 0 because I'm producing an exit status rather than printing a string.
python -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader(sys.argv[1]) else 1)" pymongo
You could replace the sys.argv[1] with the single quoted name of your package, but for my provisioning scripts I like the readability of having it at the end.
python -c "import sys, pkgutil; sys.exit(0 if pkgutil.find_loader('pymongo') else 1)"
** I realize that chef, puppet, and ansible all have plugins for managing python packages, but you may find yourself in a situation like me where you are using an outdated version and don't want to use deprecated plugins.
From the Ubuntu Shell, by default bash, as simple as
pip list | grep <package-name-Case-Matters>
Examples
pip list | grep pywinrm
pip list | grep numpy
And, if you have doubts about the case (although I think all the package names are always lowercase):
pip list | grep [Nn]um[Pp]y # it works with numpy, Numpy, numPy, and NumPy
As of writing this, pip show ... seems to be the easiest way:
https://pip.pypa.io/en/stable/reference/pip_show/
But it is silent (ie. returns nothing) when the package is not installed.
I would do something like this:
#!/bin/bash
pymodules=(
requests
termcolor
)
for module in "${pymodules[@]}"; do
if python3 -c "import pkgutil; exit(1 if pkgutil.find_loader(\"$module\") else 0)"; then
pip3 install --user "$module"
fi
done
It will install any module thats missing from the pymodules array.
Using --user makes pip install packages in your home directory instead of a system directory like /usr/local/lib/python3.7/, this is useful as it doesn't require any special privileges and keeps your system installation clean.
One can also use pydoc modules, which can be filtered with grep to find a specific module. The output is displayed in columnated format. The only disadvantage of this approach is that it also will include python files in the current working directory. Nonetheless,I use it myself most of the time and it's one of the highly cited approaches on this related question: https://stackoverflow.com/q/739993/3701431
this is what I come up with:
if [[ ! $(pip list|grep "numpy") ]]; then
pip install numpy
fi
It will check if numpy has been installed on your system, if not, it will install numpy using pip.
This code will check if the modules in the list are installed or not. If a module is not yet installed, it will install it.
import sys
modules=['pandas','numpy'] # modules to install
for module in modules:
if module in sys.modules:
print('module: '+module+' already installed')
else:
print('install module: '+module)
!pip install module
to install or update pip packages (if they are not installed or up to date) from a terminal or shell script:
#!/usr/bin/env bash
install or update pip and yq if they are not installed or up to date
for pkg in pip yq; do
pip list --uptodate | grep "${pkg} " || pip install --upgrade ${pkg}
done
vpythonyou need to install it assudo apt-get install python-visual libgtkglextmm-x11-1.2-dev– sourav c. Feb 22 '15 at 03:48sudo apt-get install python-pipand then to install modules, just typepip install numpy– thuyein Feb 22 '15 at 08:12pipalready installed, so you can dopython -m pip installwithout having to installpipas an external program. – Bakuriu Feb 22 '15 at 08:23python -m ensurepip, to make surepipis installed there. – jfs Feb 23 '15 at 07:00sudo pip; it may break system python. Use apt-get to install packages for system python. You could usepip --useroption or virtualenv to install Python packages for yourself. – jfs Feb 23 '15 at 07:03sudo, notpip. I usepipall the time to install various packages into local virtual environments. – jfs Feb 23 '15 at 07:22rootor usingsudo. – sourav c. Feb 23 '15 at 08:37pip listcommand. – Steve Barnes Feb 28 '15 at 07:58python -c "import math"thenecho %ERRORLEVEL%. – Hans Ginzel Nov 01 '18 at 06:27