Reputation: 1247
I would like to install the modules 'mutagen' and 'gTTS' for my code, but I want to have it so it will install the modules on every computer that doesn't have them, but it won't try to install them if they're already installed. I currently have:
def install(package):
pip.main(['install', package])
install('mutagen')
install('gTTS')
from gtts import gTTS
from mutagen.mp3 import MP3
However, if you already have the modules, this will just add unnecessary clutter to the start of the program whenever you open it.
Upvotes: 76
Views: 250037
Reputation: 19
You can use the command pip list
for determine that:
import os
try:
pip_packages = os.popen("pip list").read()
except Exception:
print("Error: pip is not installed")
if pip_packages.find("lib one" and "lib two") != -1:
print("Required libs are alerty installed")
else:
os.system("pip install lib one lib two")
print("Required libs are installed")
Upvotes: 0
Reputation: 68908
For Python 3.3+ use,
import importlib
spec = importlib.util.find_spec('some_module')
if spec is not None:
print('module is installed')
Upvotes: 6
Reputation: 6210
Although @girrafish's answer might suffice, you can check package installation via importlib
too:
import importlib
packages = ['mutagen', 'gTTS']
[subprocess.check_call([sys.executable, '-m', 'pip', 'install', pkg])
for pkg in packages if not importlib.metadata.distribution(pkg)]
Upvotes: 3
Reputation: 21
You can do either of these things (Simple, really)
pip list | grep <package_name>
This only shows if a package is present or not
The other way would be to use show
,which gives more detail:
pip show <package_name>
You can embed these in a bash script; if it doesn't return the package name, you simply pip install --user <package_name>
them
Upvotes: 1
Reputation: 1162
you can use simple try/except:
try:
import mutagen
print("module 'mutagen' is installed")
except ModuleNotFoundError:
print("module 'mutagen' is not installed")
# or
install("mutagen") # the install function from the question
Upvotes: 45
Reputation: 169
You can run pip show package_name
or for broad view use pip list
Reference
Upvotes: 4
Reputation: 355
If you want to know if a package is installed, you can check it in your terminal using the following command:
pip list | grep <module_name_you_want_to_check>
How this works:
pip list
lists all modules installed in your Python.
The vertical bar | is commonly referred to as a "pipe". It is used to pipe one command into another. That is, it directs the output from the first command into the input for the second command.
grep <module_name_you_want_to_check>
finds the keyword from the list.
Example:
pip list| grep quant
Lists all packages which start with "quant" (for example "quantstrats"). If you do not have any output, this means the library is not installed.
Upvotes: 33
Reputation: 1
If you would like to preview if a specific package (or some) are installed or not maybe you can use the idle in python. Specifically :
Above is tested with python 3.9.0
Upvotes: 0
Reputation: 2482
EDIT - 2020/02/03
The pip
module has updated quite a lot since the time I posted this answer. I've updated the snippet with the proper way to install a missing dependency, which is to use subprocess
and pkg_resources
, and not pip
.
To hide the output, you can redirect the subprocess output to devnull:
import sys
import subprocess
import pkg_resources
required = {'mutagen', 'gTTS'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)
Like @zwer mentioned, the above works, although it is not seen as a proper way of packaging your project. To look at this in better depth, read the the page How to package a Python App.
Upvotes: 90
Reputation: 4055
You can check if a package is installed using pkg_resources.get_distribution
:
import pkg_resources
for package in ['mutagen', 'gTTS']:
try:
dist = pkg_resources.get_distribution(package)
print('{} ({}) is installed'.format(dist.key, dist.version))
except pkg_resources.DistributionNotFound:
print('{} is NOT installed'.format(package))
Note: You should not be directly importing the pip
module as it is an unsupported use-case of the pip
command.
The recommended way of using pip
from your program is to execute it using subprocess
:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'my_package'])
Upvotes: 11
Reputation: 278
Another solution it to put an import statement for whatever you're trying to import into a try/except block, so if it works it's installed, but if not it'll throw the exception and you can run the command to install it.
Upvotes: 1
Reputation: 517
You can use the command line :
python -m MyModule
it will say if the module exists
Else you can simply use the best practice :
pip freeze > requirements.txt
That will put the modules you've on you python installation in a file
and :
pip install -r requirements.txt
to load them
It will automatically you purposes
Have fun
Upvotes: 3