How do I import all modules into a package containing a period in the 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 this:

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

      


I know that the presence of dots in the package name is not correct. It's there because Sikuli requires your "project" to be called "{project} .sikuli".

+3


source to share


3 answers


While I will not encourage this behavior in any way, you can do so by updating locals()

to reference it using the internal attribute dictionary from the imported module:

>>> 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, in another way:



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

      

Edit: Using Jace's self-diagnostic below, the following will work for dotted file names:

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__)

      

+1


source


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



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__)

      

+1


source


Well, like almost everything in Python, the import system is hacked. You just need to create a custom bootloader and register it with sys.meta_path

(see PEP 302 for details ).

Suppose you want to hack into the import system 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 only a starting point for you, this is not a fully tested solution; in fact, it is far beyond my magic 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)

      

Let's assume 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'
>>>

      

0


source







All Articles