3

I have this to import all modules if they dont exist, the thing is that even if i have them it also behaves has if i dont, what am i doing wrong?

listimport=["request","shutil","shutil","styless","time","tkinter","openpyxl","html","datetime","importlib","string",
 "easygui","bs4","webbrowser","glob","tarfile","webbrowser","pathlib","platform","subprocess","tkinterweb",
 "jira","numpy","matplotlib","calendar","sys","math","math","parser","pyautogui","dateutil","xlwt"]
for x_imp in listimport:
 try:
 import x_imp
 except ImportError as e:
 os.system('pip install ' + x_imp)

this always tries to install all modules even if they already exist, any ideas?

Dani Mesejo
62.2k6 gold badges57 silver badges86 bronze badges
asked Oct 8, 2021 at 13:27

2 Answers 2

3

When you import a module you do it like this:

import request

Using your program you will try to import every string like this:

import "request"

Because listimport contains strings! so you will get an error every time!

You can fix it using __import__ that do the same thing, but on a string:

import os
listimport = ["request","shutil","shutil","styless","time","tkinter","openpyxl","html","datetime","importlib","string",
 "easygui","bs4","webbrowser","glob","tarfile","webbrowser","pathlib","platform","subprocess","tkinterweb",
 "jira","numpy","matplotlib","calendar","sys","math","math","parser","pyautogui","dateutil","xlwt"]
for x_imp in listimport:
 try:
 __import__(x_imp)
 except ImportError as e:
 os.system('pip install ' + x_imp)
answered Oct 8, 2021 at 13:47
Sign up to request clarification or add additional context in comments.

5 Comments

thanks, that fix it :)
i know this is a second question but how can i install os without doing pip install os everythime i run the program in order for this script to work?
I don’t understand the question, os is installed with python by default...
it is??but when i compile the program into a portable app will it also send the os?
Yes it will send the os too.
2

You are now trying to import a string, for example import "numpy". A simple solution can be to use exec()

listimport=["request","shutil","shutil","styless","time","tkinter","openpyxl","html","datetime","importlib","string",
 "easygui","bs4","webbrowser","glob","tarfile","webbrowser","pathlib","platform","subprocess","tkinterweb",
 "jira","numpy","matplotlib","calendar","sys","math","math","parser","pyautogui","dateutil","xlwt"]
for x_imp in listimport:
 try:
 exec('import {}'.format(x_imp))
 except ImportError:
 os.system('pip install ' + x_imp)
answered Oct 8, 2021 at 14:00

3 Comments

thanks:) i know this is a second question but how can i install os without doing pip install os everythime i run the program in order for this script to work?
The os module is part of the python's standard library and therefore doesn't need pip install os. You can therefore safely assume that when python is installed, you can always use the os module.
thanks, but when i compile to a portable app will it send the os too in case the person that is going to use it dont have python installed?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.