klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Find out if there is broken or missed matched imports

Tue Mar 14, 2017 8:42 pm

Hello
I would like to batch check my imports to find out if there is broken or missed matched imports.
the "import ("%s" % X)" did not work.
PIP_adds is a list of 25 imports that I would like to know if they are there and if not run my def.

What is the magic code for the 4th line down?

Code: Select all

try:
    for X in PIP_adds:
        try:
            import ("%s" % X)
        except ImportError:
            print(X + '  Did not import')
            """ dif to load file goes here """
except KeyboardInterrupt:
    pass

Thanks
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Find out if there is broken or missed matched imports

Tue Mar 14, 2017 11:03 pm

Hello,
I looked around for 'python dynamic module import' and found some solutions. My preferred one is

Code: Select all

ilist = [ 'time', 'sys']

for i in ilist:
    s = "import {module:s}\n".format( module = i)
    exec  s
   
print( time.time() )
print( sys.version_info.major )
Regards,
Gerhard

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Find out if there is broken or missed matched imports

Tue Mar 14, 2017 11:40 pm

You can use importlib,

Code: Select all

import importlib

PIP_adds = ["sys", "time", "gtfre"]

for module in PIP_adds:
  try:
    print("Importing " + module)
    globals()[module] = importlib.import_module(module)
    importlib.invalidate_caches()
  except ImportError:
    print(module + " did not import")

# Show we correctly imported time
print(time.asctime())

Code: Select all

pi@rpi3:~/Programming/pyth $ python3 imp.py
Importing sys
Importing time
Importing gtfre
gtfre did not import
Tue Mar 14 23:43:33 2017
Last edited by Paeryn on Wed Mar 15, 2017 12:16 am, edited 1 time in total.
She who travels light — forgot something.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: Find out if there is broken or missed matched imports

Tue Mar 14, 2017 11:48 pm

Hello all,
Just got back to the Forum.
I guess that asking the question some time triggers a better search.

I looked for a couple hours and found nothing. Ask the question in the Forum then back to google and found this.
__import__()

Code: Select all

PIPS = []
try:
    for X in PIP_adds:
        print("\n")
        XL = X.lower()
        XT = X.title()
        try:
            #import ("%s" % X)
           __import__(X)
        except ImportError:
            print(X + "  Did not work")
            try:
               __import__(XL)
            except ImportError:
                print(XL + "  Did not work")
                try:
                    __import__(XT)
                except ImportError:
                    PIPS.append(X)
                    print(XT + "  Did not work")
    print('\n\n')
    for y in PIPS:
        print(y)
except KeyboardInterrupt:
    pass


thanks ghp
thanks all
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Find out if there is broken or missed matched imports

Wed Mar 15, 2017 12:15 am

klintkrossa wrote:Hello all,
Just got back to the Forum.
I guess that asking the question some time triggers a better search.

I looked for a couple hours and found nothing. Ask the question in the Forum then back to google and found this.
__import__()

Code: Select all

PIPS = []
try:
    for X in PIP_adds:
        print("\n")
        XL = X.lower()
        XT = X.title()
        try:
            #import ("%s" % X)
           __import__(X)
        except ImportError:
            print(X + "  Did not work")
            try:
               __import__(XL)
            except ImportError:
                print(XL + "  Did not work")
                try:
                    __import__(XT)
                except ImportError:
                    PIPS.append(X)
                    print(XT + "  Did not work")
    print('\n\n')
    for y in PIPS:
        print(y)
except KeyboardInterrupt:
    pass


thanks ghp
thanks all
You need to bind the return value to an object else you can't use the imported module/package as you'll have no name to access it by (that's why I entered it into the globals() dictionary in my example). Just noticed I forgot a line in my example (the invalidate_caches() call).

importlib.import_module() is recommended over __import__() unless there is a compelling reason not to.
She who travels light — forgot something.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: Find out if there is broken or missed matched imports

Wed Mar 15, 2017 2:05 pm

Hello,
The import is to see what is missing, broken or needs some more research. For this application and the amount of imports it works fine.
I will keep that in mind for when I have to batch load a lot of files. I know it coming. :D :D
Paeryn wrote:
klintkrossa wrote:Hello all,
Just got back to the Forum.
I guess that asking the question some time triggers a better search.

I looked for a couple hours and found nothing. Ask the question in the Forum then back to google and found this.
__import__()

Code: Select all

PIPS = []
try:
    for X in PIP_adds:
        print("\n")
        XL = X.lower()
        XT = X.title()
        try:
            #import ("%s" % X)
           __import__(X)
        except ImportError:
            print(X + "  Did not work")
            try:
               __import__(XL)
            except ImportError:
                print(XL + "  Did not work")
                try:
                    __import__(XT)
                except ImportError:
                    PIPS.append(X)
                    print(XT + "  Did not work")
    print('\n\n')
    for y in PIPS:
        print(y)
except KeyboardInterrupt:
    pass


thanks ghp
thanks all
You need to bind the return value to an object else you can't use the imported module/package as you'll have no name to access it by (that's why I entered it into the globals() dictionary in my example). Just noticed I forgot a line in my example (the invalidate_caches() call).

importlib.import_module() is recommended over __import__() unless there is a compelling reason not to.
Thanks
Klint
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

Return to “Python”