Python Embedding Importing relative modules

Aahz aahz at pythoncraft.com
Tue Jul 6 15:11:01 EDT 2010


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