You are not logged in.

#1 2009-09-18 00:00:50

B4RR13N705
Member
Registered: 2009-06-08
Posts: 87

Cant import a file! (Python)

Hi, i was programming... but i got stucked.
I cant import a file into my script.
I tried import, from file import 1,2,3,4,5,etc
but no luck.
The code is about 350 lines. And one function, need to access some variables that are in a file called META.py.

def import():
        dir = os.popen("ls -Ct1 --group-directories-first | head -n 1").read().strip()
        os.chdir(dir)
        if os.access('META.py', os.F_OK) == False:
                print "> FATAL: The META file doesnt exists."
                print "> QUITTING..."
                exit()
        import META
        from META import archtype,name,version,dependencies,configoptions,buildoptions  # AND OTHERS

But when executing this function i get:

Traceback (most recent call last):
  File "test.py", line 349, in <module>
    inx = input('>>> ')
  File "<string>", line 1, in <module>
  File "test.py", line 152, in import
    import META
ImportError: No module named META

Ive checked and im on the correct directory. Also, the weird thing is that this piece of code worked a few times before. But now, i get nothing...
What can i do?

Last edited by B4RR13N705 (2009-09-18 00:01:19)


OS -----> Arch Linux     DE -----> KDE4
CPU ---> 2.66GHz         RAM ---> 512 MB
SWAP -> 2 G                / -------> 10 G
/home -> 50 G             /boot ---> 64 MB

Offline

#2 2009-09-18 15:51:36

N30N
Member
Registered: 2007-04-08
Posts: 273

Re: Cant import a file! (Python)

You need to add the directory to the PYTHONPATH before you can import from it.

import sys
sys.path.insert(0, dir)
try:
    import META
except ImportError:
    print("> FATAL: The META file doesnt exists.")
    print("> QUITTING...")
    exit()

Offline

Board footer

Powered by FluxBB