简体   繁体   中英

How do I import all modules in a package that contains a dot in its name?

This linked answer tells me how to import a single module with a dot in its name, but how do I import all modules from a package with a dot in its name:

from package.with.dot.in.name import *

where my files look something like:

package.with.dot.in.name/
    __init__.py
    module_1.py
    module_2.py

I know that having dots in a package name is wrong. It's there because Sikuli requires your "project" to be named "{project}.sikuli".

Based on this answer and some of the comments, I was able to do:

name = 'package.with.dot.in.name'
pathname, description = imp.find_module(name)[1:]
package = imp.load_module(name, None, pathname, description)
locals().update(package.__dict__)

While in no way I would encourage this behavior, you can do this by updating your locals() via a reference to it with the internal dictionary of attributes from the module you imported:

>>> r = __import__('requests')
>>> l = locals()
>>> l.update(r.__dict__)
>>> locals()['cookies']
<module 'requests.cookies' from '/usr/local/lib/python2.7/site-packages/requests/cookies.pyc'>

Or, put another way:

>>> cookies
<module 'requests.cookies' from '/usr/local/lib/python2.7/site-packages/requests/cookies.pyc'>

Edit: using Jace's self-answer below, the following will work for filenames with dots:

name = 'package.with.dot.in.name'
pathname, description = imp.find_module(name)[1:]
package = imp.load_module(name, None, pathname, description)
locals().update(package.__dict__)

Well, like almost everything in Python, the import system is hack-able. You just need to create a custom loader and register it at sys.meta_path (for details see PEP 302 ).

Lets say you want to hack the import system in order to load "foo.bar" if you import "foo_dot_bar":

# search folder "foo.bar" and load it as a package
from foo_dot_bar import *     

Be warned: this is just a starting point for you, it is not a fully tested solution; in fact it is way beyond my wizardry level!

# stupid_dot_importer.py
import os
import imp
import sys

class StupidDotPackageLoader(object):
    @staticmethod
    def _get_real_name(name):
        return ".".join(name.split('_dot_'))
    def find_module(self, name, path=None):
        try:
            imp.find_module(self._get_real_name(name))
        except ImportError:
            return None
        return self
    def load_module(self, name):
        _, pathname, description = imp.find_module(self._get_real_name(name))
        return imp.load_module(self._get_real_name(name), None, pathname, description)

Suppose you have the following structure:

foo.bar
   |
   +--- __init__.py 
   |
   +--- module1.py
   |
   +--- module2.py

And:

$ cat foo.bar/__init__.py
from module1 import *
from module2 import *

$ cat foo.bar/module1.py
foo = 'bar'

$ cat foo.bar/module2.py
spam = 'eggs'

Then magic:

>>> from stupid_dot_importer import *
>>> sys.meta_path = [StupidDotPackageLoader()]
>>> from foo_dot_bar import *
>>> foo
'bar'
>>> spam
'eggs'
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM