I’m doing some not-so standard import of python modules. I’m adding a finder object to sys.meta_path to do things like importing a module via http/ssh/svn/etc. I’m able to successfully import the module using code like:
def load_module(self, fullname, some_path=None):
"""
fullname is something like: this.is.a.module
"""
is_package = figure_out_if_this_is_a_package(fullname)
file_contents = get_file_contents(fullname) # via http/ssh/svn, whatever
mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
mod.__file__ = "<MyFancyMagicImporter>"
mod.__loader__ = self
if is_package:
mod.__path__ = []
mod.__package__ = fullname
else:
mod.__package__ = fullname.rpartition(".")[0]
exec(file_contents, mod.__dict__)
return mod
the problem occurs when the dynamically imported module (let’s call it ssh.test_module) tries to import something. If the ssh.test_module does something like import os, it actually ends up trying to do a relative import like import ssh.test_module.os. If this relative looking import fails, the import fails completely.
Help?
Update: BTW, I’m using python 2.7
UPDATE Here’s a better example that demonstrates the problem I’m having:
#!/usr/bin/env python
import sys
import imp
class Loader(object):
def load_module(self, fullname, some_path=None):
file_contents = ""
if fullname == "loader_testing":
file_contents = "import os"
elif fullname == "loader_testing.blah":
file_contents = "import sys\nimport os\nimport loader_testing.halb"
elif fullname == "loader_testing.halb":
file_contents = "print('HALBHALBHALB')"
mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
mod.__file__ = "<%s>" % self.__class__.__name__
mod.__loader__ = self
is_package = fullname.count(".") == 0
if is_package:
mod.__path__ = []
mod.__package__ = fullname
else:
mod.__package__ = fullname.rpartition('.')[0]
mod.__path__ = []
exec(file_contents, mod.__dict__)
return mod
def find_module(self, fullname, path=None):
if fullname.find("loader_testing") == 0:
print "loader_testing, fullname: %s" % fullname
return self
return None
sys.meta_path.append(Loader())
import loader_testing.blah
I would expect the above code to output something like:
loader_testing, fullname: loader_testing
loader_testing, fullname: loader_testing.blah
loader_testing, fullname: loader_testing.halb
HALBHALBHALB
Instead, you get this:
loader_testing, fullname: loader_testing
loader_testing, fullname: loader_testing.os
loader_testing, fullname: loader_testing.blah
loader_testing, fullname: loader_testing.sys
loader_testing, fullname: loader_testing.loader_testing
loader_testing, fullname: loader_testing.loader_testing.halb
Notice what seems like relative importing with the loader_testing.loader_testing.halb and such
What ended up fixing this was importing
absolute_pathfrom future. I thought that was already active by default in python 2.7.3, but I guess I was mistaken: