0

I have written a script for XBMC which optionally downloads a dll and then imports a module that depends on that dll if the download was successful. However, placing the import inside a function generates a Python syntax warning. Simplified example:

1  def importIfPresent():
2      if chkFunction() is True:
3          from myOptionModule import *

Line 3 generates the warning, but doesn't stop the script. I can't place this code at the start outside of a function because I need to generate dialog boxes to prompt the download and then hash the file once it is downloaded to check success. I also call this same code at startup in order to check if the user has already downloaded the dll.

Is there a different/better way to do this without generating the syntax warning? Or should I just ignore the warning and leave it as is?


Thank you! Using the useful responses below, I now have:

import importlib
myOptionalModule = None

def importIfPresent():
    if chkFunction is True:
        try:
            myOptionalModule = importlib.import_module('modulex')
        except ImportError:
            myOptionalModule = None
...
importIfPresent()
...
def laterFunction():
    if myOptionalModule != None:
        myParam = 'something expected'
        myClass = getattr(myOptionalModule, 'importClassName')
        myFunction = getattr(myClass, 'functionName')
        result = myFunction(myClass(), myParam)
    else:
        callAlternativeMethod()

I am posting this back mainly to share with other beginners like myself the way I learned through the discussion to use the functionality of a module imported this way instead of the standard import statement. I'm sure that there are more elegant ways of doing this that the experts will share as well...

KenV99
  • 195
  • 2
  • 9

2 Answers2

2

You're not getting the warning for doing an import inside a function, you're getting the warning for using from <module> import * inside a function. Doing a In Python3, this actually becomes a SyntaxError, not a SyntaxWarning. See this answer for why wildcard imports like this in general, and expecially inside functions are discouraged.

Also, this code isn't doing what you think it does. When you do an import inside a function, the import only takes affect inside the function. You're not importing that module into the global namespace of the file, which I believe is what you're really trying to do.

As suggested in another answer importlib can help you here:

try: import myOptionModule as opt except ImportError: opt = None

def importIfPresent():
  global opt
  if chkFunction() is True:
      opt = importlib.import_module("myOptionModule")
Community
  • 1
  • 1
dano
  • 91,354
  • 19
  • 222
  • 219
  • My apologies for not stating it further, but indeed in the beginning of the script I set myOptionalModule = None and then have a 'global myOptionalModule' in the function. I have a working version using the importlib option. I will post back with what I learned about the syntax of calling a function in a class in a module imported like this in the hope that it will help someone else in the future. – KenV99 Jun 17 '14 at 15:15
0

I beleive you need to use the importlib library to facilitate this.

The code would be at the top of the mod:

import importlib

then replace "from myOptionModule import *" with "module = importlib.import_module(myOptionModule)". You can then import the defs/classes you want or import them all by using getattr(module,NAME(S)TOIMPORT).

See if that works.

Check out chapter 30 and 31 of Learning Python by Lutz for more info.

user3047520
  • 45
  • 2
  • 7