简体   繁体   中英

importing module without importing that models imports

I am trying to import a python module without importing the imports of that module. I was digging around a bit, but the only way to exclude any command from being run when a file is being imported is the if __name__ == "__main__": But the module is also imported by various other modules that ened that modules imports, so I cant place the imports below the if __name__ == "__main__": Any idea how to solve that?

The reason why I dont want to import this modules imports that those modules get run also from a jar jython envioronment and import java.lang functions. I just need to access a few functions in that file without the whole and importing those modules break make script. The functions that I am trying to access dont need any dependencies that module ahs.

I import via 'from moduleX import f1,f2,f3'

You may be able to use import hooks to work around the issue. The sys.meta_path variable is a list of importers that can override the normal import processing. You can use them to stub out modules that only exist in Jython. Here's an example:

import types, sys

class Stub(object):
    def __getattr__(self, name):
        return self

class StubModule(types.ModuleType):
    def __getattr__(self, name):
        return Stub()

class StubJavaImporter(object):
    def find_module(self, fullname, path = None):
        if fullname == 'java' or fullname.startswith('java.'):
            return self
        else:
            return None

    def load_module(self, fullname):
        mod = sys.modules.setdefault(fullname, StubModule(fullname))
        mod.__file__ = "<%s>" % self.__class__.__name__
        mod.__loader__ = self
        mod.__path__ = []
        mod.__package__ = fullname
        return mod

sys.meta_path.append(StubJavaImporter())

After running this code you can import any module starting with java. and access any attribute path within. For example:

In [2]: from java.lang import Object

In [3]: Object.equals
Out[3]: <__main__.Stub at 0x14d8b90>

In [4]: Object
Out[4]: <__main__.Stub at 0x14d8b90>

In [5]: Object.foo.bar.baz
Out[5]: <__main__.Stub at 0x14d8b90>

This might be enough to make things work. It is a nasty, nasty hack, but if you really can't modify the offending module this might be the best you can do.

If you want to use from module import * and not include everything imported within module then you should define the variable __all__ in module . This variable should be a list of strings naming the classes, variables, modules, etc. that should be imported.

From the python documentation

If the list of identifiers is replaced by a star ( * ), all public names defined in the module are bound in the local namespace of the import statement

and

The public names defined by a module are determined by checking the module's namespace for a variable named __all__ ; if defined, it must be a sequence of strings which are names defined or imported by that module. The names given in __all__ are all considered public and are required to exist. If __all__ is not defined, the set of public names includes all names found in the module's namespace which do not begin with an underscore character ( _ ) [...] It is intended to avoid accidentally exporting items that are not part of the API (such as library modules which were imported and used within the module) . (emphasis mine)

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