0

I want to allow user to make module file, if name isn't defined in Py3k already. (In it's portable installation). For ex, if user enters "ppp" i must check first, if module "ppp" exists in Py installation (it exists here!) and disallow this name. If he enters "my_name" i must allow it and if enters "ppp" or "json" I must disallow it.

Any way doing this check without doing "import" first? such import may take much memory. I only want to check if module name can be imported.

Prog1020
  • 4,530
  • 8
  • 31
  • 65

1 Answers1

1

You can use the pkgutil module, something like this:

import pkgutil

def module_exists(m):
    for module_loader, name, ispkg in pkgutil.iter_modules():
        if name == m:
            return True
    return False
Tom Leese
  • 19,309
  • 12
  • 45
  • 70