How can I make python detect missing packages or apps [closed]












0















I'm making a script for Ethical Hacking purposes and I want to have a first function that detects and shows the user what packages they need to install before they can continue. For example, I would like it to detect if the user has nmap or aircrack-ng in their Linux systems!










share|improve this question















closed as off-topic by Jeff Schaller, Olorin, Mr Shunz, jimmij, Archemar Feb 8 at 10:29



  • This question does not appear to be about Unix or Linux within the scope defined in the help center.

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • @JeffSchaller Iḿ using linux mint!

    – user270399
    Feb 7 at 19:58






  • 1





    If I import foo and module foo is not found by python, I get ab exception that says: ModuleNotFoundError: No module named 'foo' - is that not enough? If not, what more do you want?

    – NickD
    Feb 7 at 20:04













  • pip list or pip freeze will show installed Python modules. It should be enough to just compare the output what modules the user needs to run the script. As previously mentioned, if the module isn't there then they will see the error that it's not found.

    – Nasir Riley
    Feb 8 at 0:08











  • @NickD I want it detect packeges like aircrack-ng that arent python related !

    – user270399
    Feb 14 at 20:12
















0















I'm making a script for Ethical Hacking purposes and I want to have a first function that detects and shows the user what packages they need to install before they can continue. For example, I would like it to detect if the user has nmap or aircrack-ng in their Linux systems!










share|improve this question















closed as off-topic by Jeff Schaller, Olorin, Mr Shunz, jimmij, Archemar Feb 8 at 10:29



  • This question does not appear to be about Unix or Linux within the scope defined in the help center.

If this question can be reworded to fit the rules in the help center, please edit the question.

















  • @JeffSchaller Iḿ using linux mint!

    – user270399
    Feb 7 at 19:58






  • 1





    If I import foo and module foo is not found by python, I get ab exception that says: ModuleNotFoundError: No module named 'foo' - is that not enough? If not, what more do you want?

    – NickD
    Feb 7 at 20:04













  • pip list or pip freeze will show installed Python modules. It should be enough to just compare the output what modules the user needs to run the script. As previously mentioned, if the module isn't there then they will see the error that it's not found.

    – Nasir Riley
    Feb 8 at 0:08











  • @NickD I want it detect packeges like aircrack-ng that arent python related !

    – user270399
    Feb 14 at 20:12














0












0








0


2






I'm making a script for Ethical Hacking purposes and I want to have a first function that detects and shows the user what packages they need to install before they can continue. For example, I would like it to detect if the user has nmap or aircrack-ng in their Linux systems!










share|improve this question
















I'm making a script for Ethical Hacking purposes and I want to have a first function that detects and shows the user what packages they need to install before they can continue. For example, I would like it to detect if the user has nmap or aircrack-ng in their Linux systems!







linux python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 8 at 3:27









wjandrea

498413




498413










asked Feb 7 at 14:13







user270399











closed as off-topic by Jeff Schaller, Olorin, Mr Shunz, jimmij, Archemar Feb 8 at 10:29



  • This question does not appear to be about Unix or Linux within the scope defined in the help center.

If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Jeff Schaller, Olorin, Mr Shunz, jimmij, Archemar Feb 8 at 10:29



  • This question does not appear to be about Unix or Linux within the scope defined in the help center.

If this question can be reworded to fit the rules in the help center, please edit the question.













  • @JeffSchaller Iḿ using linux mint!

    – user270399
    Feb 7 at 19:58






  • 1





    If I import foo and module foo is not found by python, I get ab exception that says: ModuleNotFoundError: No module named 'foo' - is that not enough? If not, what more do you want?

    – NickD
    Feb 7 at 20:04













  • pip list or pip freeze will show installed Python modules. It should be enough to just compare the output what modules the user needs to run the script. As previously mentioned, if the module isn't there then they will see the error that it's not found.

    – Nasir Riley
    Feb 8 at 0:08











  • @NickD I want it detect packeges like aircrack-ng that arent python related !

    – user270399
    Feb 14 at 20:12



















  • @JeffSchaller Iḿ using linux mint!

    – user270399
    Feb 7 at 19:58






  • 1





    If I import foo and module foo is not found by python, I get ab exception that says: ModuleNotFoundError: No module named 'foo' - is that not enough? If not, what more do you want?

    – NickD
    Feb 7 at 20:04













  • pip list or pip freeze will show installed Python modules. It should be enough to just compare the output what modules the user needs to run the script. As previously mentioned, if the module isn't there then they will see the error that it's not found.

    – Nasir Riley
    Feb 8 at 0:08











  • @NickD I want it detect packeges like aircrack-ng that arent python related !

    – user270399
    Feb 14 at 20:12

















@JeffSchaller Iḿ using linux mint!

– user270399
Feb 7 at 19:58





@JeffSchaller Iḿ using linux mint!

– user270399
Feb 7 at 19:58




1




1





If I import foo and module foo is not found by python, I get ab exception that says: ModuleNotFoundError: No module named 'foo' - is that not enough? If not, what more do you want?

– NickD
Feb 7 at 20:04







If I import foo and module foo is not found by python, I get ab exception that says: ModuleNotFoundError: No module named 'foo' - is that not enough? If not, what more do you want?

– NickD
Feb 7 at 20:04















pip list or pip freeze will show installed Python modules. It should be enough to just compare the output what modules the user needs to run the script. As previously mentioned, if the module isn't there then they will see the error that it's not found.

– Nasir Riley
Feb 8 at 0:08





pip list or pip freeze will show installed Python modules. It should be enough to just compare the output what modules the user needs to run the script. As previously mentioned, if the module isn't there then they will see the error that it's not found.

– Nasir Riley
Feb 8 at 0:08













@NickD I want it detect packeges like aircrack-ng that arent python related !

– user270399
Feb 14 at 20:12





@NickD I want it detect packeges like aircrack-ng that arent python related !

– user270399
Feb 14 at 20:12










2 Answers
2






active

oldest

votes


















2














For Python modules I use code like this:



# Make sure we have all our necessary modules
allOK=True
for m in ['requests','json','yaml','argparse','re','traceback']:
try:
globals()[m]=importlib.import_module(m)
except Exception as e:
print 'Python module "%s" is required, please use "yum install python-%s" to install it.' % (m,m)
allOK=False

if not allOK:
print 'One or more modules missing, exiting.'
exit(1)





share|improve this answer
























  • Might be better to just catch ImportError instead of all exceptions

    – wjandrea
    Feb 8 at 1:33



















0














For packages, just trying to install them is the easiest, but something like parsing the output of rpm -qa (or equivalent for other packaging schemes) would work. If it's a Python module, you could have a try-except statement where the except clause can install the package containing the module and try the import again.



Was that the question, or are we all confused?






share|improve this answer


























  • That's ok but I steal need something that detects packages not related to python like aicrack-ng!

    – user270399
    Feb 14 at 20:14











  • Right, so only the first half of my answer is relevant. Parse the output of your package manager.

    – stolenmoment
    Feb 14 at 22:43



















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














For Python modules I use code like this:



# Make sure we have all our necessary modules
allOK=True
for m in ['requests','json','yaml','argparse','re','traceback']:
try:
globals()[m]=importlib.import_module(m)
except Exception as e:
print 'Python module "%s" is required, please use "yum install python-%s" to install it.' % (m,m)
allOK=False

if not allOK:
print 'One or more modules missing, exiting.'
exit(1)





share|improve this answer
























  • Might be better to just catch ImportError instead of all exceptions

    – wjandrea
    Feb 8 at 1:33
















2














For Python modules I use code like this:



# Make sure we have all our necessary modules
allOK=True
for m in ['requests','json','yaml','argparse','re','traceback']:
try:
globals()[m]=importlib.import_module(m)
except Exception as e:
print 'Python module "%s" is required, please use "yum install python-%s" to install it.' % (m,m)
allOK=False

if not allOK:
print 'One or more modules missing, exiting.'
exit(1)





share|improve this answer
























  • Might be better to just catch ImportError instead of all exceptions

    – wjandrea
    Feb 8 at 1:33














2












2








2







For Python modules I use code like this:



# Make sure we have all our necessary modules
allOK=True
for m in ['requests','json','yaml','argparse','re','traceback']:
try:
globals()[m]=importlib.import_module(m)
except Exception as e:
print 'Python module "%s" is required, please use "yum install python-%s" to install it.' % (m,m)
allOK=False

if not allOK:
print 'One or more modules missing, exiting.'
exit(1)





share|improve this answer













For Python modules I use code like this:



# Make sure we have all our necessary modules
allOK=True
for m in ['requests','json','yaml','argparse','re','traceback']:
try:
globals()[m]=importlib.import_module(m)
except Exception as e:
print 'Python module "%s" is required, please use "yum install python-%s" to install it.' % (m,m)
allOK=False

if not allOK:
print 'One or more modules missing, exiting.'
exit(1)






share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 8 at 0:28









xenoidxenoid

3,0751725




3,0751725













  • Might be better to just catch ImportError instead of all exceptions

    – wjandrea
    Feb 8 at 1:33



















  • Might be better to just catch ImportError instead of all exceptions

    – wjandrea
    Feb 8 at 1:33

















Might be better to just catch ImportError instead of all exceptions

– wjandrea
Feb 8 at 1:33





Might be better to just catch ImportError instead of all exceptions

– wjandrea
Feb 8 at 1:33













0














For packages, just trying to install them is the easiest, but something like parsing the output of rpm -qa (or equivalent for other packaging schemes) would work. If it's a Python module, you could have a try-except statement where the except clause can install the package containing the module and try the import again.



Was that the question, or are we all confused?






share|improve this answer


























  • That's ok but I steal need something that detects packages not related to python like aicrack-ng!

    – user270399
    Feb 14 at 20:14











  • Right, so only the first half of my answer is relevant. Parse the output of your package manager.

    – stolenmoment
    Feb 14 at 22:43


















0














For packages, just trying to install them is the easiest, but something like parsing the output of rpm -qa (or equivalent for other packaging schemes) would work. If it's a Python module, you could have a try-except statement where the except clause can install the package containing the module and try the import again.



Was that the question, or are we all confused?






share|improve this answer


























  • That's ok but I steal need something that detects packages not related to python like aicrack-ng!

    – user270399
    Feb 14 at 20:14











  • Right, so only the first half of my answer is relevant. Parse the output of your package manager.

    – stolenmoment
    Feb 14 at 22:43
















0












0








0







For packages, just trying to install them is the easiest, but something like parsing the output of rpm -qa (or equivalent for other packaging schemes) would work. If it's a Python module, you could have a try-except statement where the except clause can install the package containing the module and try the import again.



Was that the question, or are we all confused?






share|improve this answer















For packages, just trying to install them is the easiest, but something like parsing the output of rpm -qa (or equivalent for other packaging schemes) would work. If it's a Python module, you could have a try-except statement where the except clause can install the package containing the module and try the import again.



Was that the question, or are we all confused?







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 8 at 3:26









wjandrea

498413




498413










answered Feb 8 at 0:03









stolenmomentstolenmoment

1223




1223













  • That's ok but I steal need something that detects packages not related to python like aicrack-ng!

    – user270399
    Feb 14 at 20:14











  • Right, so only the first half of my answer is relevant. Parse the output of your package manager.

    – stolenmoment
    Feb 14 at 22:43





















  • That's ok but I steal need something that detects packages not related to python like aicrack-ng!

    – user270399
    Feb 14 at 20:14











  • Right, so only the first half of my answer is relevant. Parse the output of your package manager.

    – stolenmoment
    Feb 14 at 22:43



















That's ok but I steal need something that detects packages not related to python like aicrack-ng!

– user270399
Feb 14 at 20:14





That's ok but I steal need something that detects packages not related to python like aicrack-ng!

– user270399
Feb 14 at 20:14













Right, so only the first half of my answer is relevant. Parse the output of your package manager.

– stolenmoment
Feb 14 at 22:43







Right, so only the first half of my answer is relevant. Parse the output of your package manager.

– stolenmoment
Feb 14 at 22:43





Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀