It depends on what exactly you have as a "module (precompiled)". Suppose this is exactly the contents of a .pyc file, such as ciao.pyc , as built:
$ cat>'ciao.py' def ciao(): return 'Ciao!' $ python -c'import ciao; print ciao.ciao()' Ciao!
IOW, by building ciao.pyc , let's say what you are doing now:
$ python Python 2.5.1 (r251:54863, Feb 6 2009, 19:02:12) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> b = open('ciao.pyc', 'rb').read() >>> len(b) 200
and your goal is to go from that line of byte b to the imported ciao module. Here's how:
>>> import marshal >>> c = marshal.loads(b[8:]) >>> c <code object <module> at 0x65188, file "ciao.py", line 1>
this way you get the code object from the binary contents of .pyc . Edit : if you're interested, the first 8 bytes ā the āmagic numberā and the timestamp ā are not needed here (unless you want them to check them and make exceptions if warranted, but that is beyond the scope of the question, marshal.loads will grow anyway if it detects a damaged string).
Then:
>>> import types >>> m = types.ModuleType('ciao') >>> import sys >>> sys.modules['ciao'] = m >>> exec c in m.__dict__
ie: create a new module object, set it in sys.modules , populate it by executing the code object in __dict__ . Edit : the order in which you insert sys.modules and exec matters if and only if you can have circular import, but this is the usual order used by Python import so itās better to mimic it (which has no specific flaws).
You can ācreate a new module objectā in several ways (for example, from functions in standard library modules such as new and imp ), but ācall the type to get the instanceā is the usual Python way these days, and the normal place to get the type is ( if it doesnāt have a built-in name or you donāt have it already) from the standard types library module, so I recommend it.
Now finally:
>>> import ciao >>> ciao.ciao() 'Ciao!' >>>
... you can import a module and use its functions, classes, etc. Other import (and from ) statements will then find the module as sys.modules['ciao'] , so you wonāt need to repeat this sequence of operations (really, you donāt need this last import statement here if all you want is the module to be accessible for import from other sources - I add it only to show that it works ;-).
Change If you absolutely need to import packages and modules from them in this way, rather than āsimple modulesā, as I just showed, this is also possible, but a little more complicated. Since this answer is already quite long, and I hope you can simplify your life by sticking to simple modules for this purpose, I am going to dodge this part of the answer; -).
Also note that this may or may not do what you want, in cases of āloading the same module from memory several timesā (each time it rebuilds the module, you can check sys.modules and just skip everything if the module already exists), and in particular, when such a repeated "loading from memory" occurs from several threads (requiring locks), but the best architecture should have a separate dedicated thread dedicated to the task, with other modules communicating with it through the Queue).
Finally, there is no discussion of how to set this functionality as a transparent āimport hookā, which is automatically included in the mechanisms of the internal elements of the import statement itself - itās also possible, but not quite what you are asking about it, so here I hope that you simplify your life by doing the same in a simple way, as this answer says.