Importing Files

Terry Hancock hancock at anansispaceworks.com
Fri Mar 3 01:51:40 EST 2006


On Thu, 2 Mar 2006 21:40:44 -0800 (PST)
anushya beauty <abbi_0382 at yahoo.com> wrote:
>       I am new to this python. I have installed python 2.4
>       in my windows xp system. 
>   
>   My issue is:
>   i have three directories with some files. 
>   ex: Dir ---> Dir1 / file1.1.py
>          "   ---> Dir2 / file2.1.py
>          "   ---> Dir3 / file3.1.py
>   These are the example directories i have. (Under
>   Dir--->Dir1, Dir2  and Dir3; Under Dir1 --->file1.1.py;
>   Dir2 ---> file2.1.py; Dir3  ----> file3.1.py)  In
>   file3.1.py, i am importing the file1.1.py and file2.1.py
>   using the command:  from file1.1 import *
>   from file2.1 import  *
>   
>   Now, my problem is, file1.1 is imported and i can access
>   the values  that are used in that file. But the file2.1
>   is not imported and  returning the error message as
>   "ImportError: No module named file2.1".  I couldn't
>   access the var used in that file. Actually, both the
>   files  are from different directories only. Then, why
>   onr file is imported and  another is not imported?.

First of all, do not use "." in module names, even in
examples. The "." is an operator in python, so it's really
confusing to read -- "import file.1" if interpreted
literally, is nonsense. At least I hope it is.

>   My Question is: How to import the files from the
>   different directory to the current file?.

Typically you will need to modify sys.path to determine
what's on the "PYTHONPATH".

Of course, you can also modify the PYTHONPATH environment
variable to add new stuff to the path.

Cheers,
Terry

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list