problem with import / namespace
Laszlo Nagy
gandalf at shopzeus.com
Wed May 21 15:15:52 EDT 2008
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
More information about the Python-list
mailing list