Python Embedding Importing relative modules

Aahz aahz at pythoncraft.com
Tue Jul 6 17:56:53 EDT 2010


In article <mailman.327.1278452148.1673.python-list at python.org>,
Thomas Jollans  <thomas at jollans.com> wrote:
>On 07/06/2010 09:11 PM, Aahz wrote:
>> In article <4a3f0ca7-fef0-4f9c-b265-5370e61edc16 at d8g2000yqf.googlegroups.com>,
>> moerchendiser2k3  <googler.1.webmaster at spamgourmet.com> wrote:
>>> Aahz:
>>>>
>>>> Set sys.path to include each script's base dir before running it, then
>>>> restore after each script.
>>>
>>> That works, but doesnt solve the problem.
>>>
>>> ScriptA.py has a module in its directory called 'bar.py'
>>> ScriptB.py has a module in its directory called 'bar.py'
>>>
>>> Imagine the 'bar.py' modules dont have the same content, so they are
>>> not equal.
>>>
>>> Now when the first bar.py is imported, the second import for a "import
>>> bar" imports the first one, because its already stored in sys.modules.
>> 
>> Good point, you'll need to save/restore sys.modules, too.  That gets you
>> 90-95% of complete namespace separation; if you need more than that, your
>> best bet is to use separate processes.  Full-blown namepace isolation is
>> a *hard* problem, just look at all the past attempts to create secure
>> Python (and what you're trying to do is roughly equivalent).
>
>I believe Python (at least in the 3.x series) supports multiple
>interpreter instances per process.

Perhaps things have changed in 3.x, but although 2.x supposedly supported
multiple interpreter instances per process, the impression I got was that
most people would rather poke their eyes out with a dull stick than try
to make multiple interpreters per process actually work.

Multiple processes are easy, OTOH.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"If you don't know what your program is supposed to do, you'd better not
start writing it."  --Dijkstra



More information about the Python-list mailing list