problem with import / namespace

ohad frand ohad911 at gmail.com
Sat May 24 07:57:37 EDT 2008


Hi Laszlo

Thanks for your reply but it stil didnt work:
i opened python shell, changed active directory to \\one and imported tmp1.
now the correct file is loaded.
now i deleted tmp1
i dir os.chdir(\\two) and imported tmp1 again. now the incorrect file is
loaded (the same one as before).
i did one more trial with sys.path insted of os.chdir() and it didnt work
either (sys.path.append(\\one), import tmp1, del tmp1, sys.path.remove(\\one),
sys.path.append(\\two), import tmp1)
can you please try to do so and see for yourself that something is wrong
here.

The reason why i am not using other names is because i am triyng to build
something that will execute external sw and for that i have no control of
the names of the modules, but this is not important, because python must
give us a way to clear previously imported module

Thanks again
Ohad


On 5/21/08, Laszlo Nagy <gandalf at shopzeus.com> wrote:
>
> ohad frand wrote
>
>> Hi
>> Thanks for the answer.
>> I probably didnt write the problem accurately but it is not as you
>> described.
>> (i already read before the section that you pointed and it didnt help me)
>> the problem is that i dont want to import a file from different directory
>> but only from the same directory.
>> \\1\tmp2.py imports \\1\tmp1.py
>> \\2\tmp2.py imports \\2\tmp2.py
>> but when i execute the following script in the interpreter i get that the
>> second tmp2.py file imports not the file from the same directory but the
>> file that was already imported by the first executed tmp2.py file.
>>
> I think that using absolute names DOES solve the problem.
>
> import two.tmp2  # This will import tmp2.py in "two" folder for sure!
>
> >>> execfile("tmp2.py") <- here the executaion is OK
>> >>> os.chdir("c:\\2")
>> >>> execfile("tmp2.py") <- here the execution is not ok because tmp2.py
>> file imports the tmp1.py file from c:\\1 which is not OK
>>
> Hmm looks like your tmp2.py file is not a module but a whole program. No
> wonder I could not understand you - I thought that "tmp2.py" is a module,
> not a program.
>
> The answer in this case: if tmp2.py is a program then you should either
> manipulate sys.path or chdir to the containing dir, as I told in my former
> post. ( os.split(os.abspath(__file...))) -> then chdir or
> sys.path.insert(0,mydir) )
>
> in between those two execfile commands i tried to do a lot of things but
>> every time python imported the incorrect file for the second execution. (i
>> am not building a package and those names are just examples for the problem,
>> i am not really using 1 and 2 names as dirs)
>>
> Why do you need execfile?
>
>  Laszlo
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080524/68571d11/attachment.html>


More information about the Python-list mailing list