Python Embedding Importing relative modules

Thomas Jollans thomas at jollans.com
Tue Jul 6 17:34:40 EDT 2010


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.



More information about the Python-list mailing list