Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

have python script check for missing modules and install if missing

Tags:

python

I am seeking to make a script I have written more portable.

Several of the tools used in my script require that certain modules be installed. I have been able to have the script install the modules on its own, but would like to have a mechanism that would check if they are already installed and only attempt to install if missing.

I am sure this is possible, but I cannot seem to find a solution.

like image 928
nate barrett Avatar asked Oct 29 '25 11:10

nate barrett


1 Answers

The standard solution to this is to make your script into a module, and declare its dependencies in the setup.py of the module.

from setuptools import setup

setup(name='foo',
  ...
  install_requires=['dependency', 'other', ...])

A supporting, weaker convention is to keep a list in requirements.txt so you can

pip install -r requirements.txt

Having your own script do these chores is not usually useful or necessary; the existing packaging infrastructure already provides what you need, and is the natural point to manipulate and manage package dependencies.

Historically, there was some turmoil as different packaging regimes competed for dominance in the Python ecosystem, but things seem to have pretty much settled on setuptools and pip now.

like image 144
tripleee Avatar answered Nov 01 '25 01:11

tripleee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!