¿Es posible que algo funcione en Python ide pero no en el script?

Something weird... I was working on importing files from different folders.. and was working on python ide..

so my ide code:

>>> import os
>>> os.chdir("..")
>>> os.chdir("lib")
>>> os.chdir("native")
>>> os.getcwd()
'/.../.../Programming/lib/native'
>>> from category import *

works great.. but exactly same on my python file:

import os
import sys
#get current working directory
cur_dir = os.getcwd()
#move up one level
os.chdir("..")
new_cur_dir = os.getcwd()
print new_cur_dir
#move down to native
try:
 os.chdir("lib")
 print os.getcwd()
except IOError as e:
    sys.exit("Exitting: 'lib' folder missing!!")

try:
os.chdir("native")
print os.getcwd()
from category import *
from pilottest import *
from datainstance import *
from similar import *
from collections import defaultdict
from item import *
from pilottest import *
from infernumber import *

except IOError as e:
          sys.exit("Exitting: 'native' folder missing!!")

Error:

/../../Programming
/../../Programming/lib
/../../Programminglib/native
Traceback (most recent call last):
 File "foo.py", line 25, in <module>
  from category import *
ImportError: No module named category

preguntado el 30 de enero de 12 a las 19:01

2 Respuestas

When running code in the interpreter, the first entry of sys.path is an empty string, which indicates the current directory. However, when you run code from a file the first entry of sys.path is a fully qualified path of the directory that you ran the script from.

This means that when you change directories in the interpreter you can always do imports from your current directory, but the same is not true for running from files.

If you want to always be able to import from the current directory in your scripts add the following lines to the top of your files:

import sys
sys.path.insert(0, '')

Respondido el 30 de enero de 12 a las 23:01

great great great// :) thanks for the advice.. Is there any documentation where I can look for the difference between python iterpretator and the scripts.. i always thought that whatever worked in interpretator will work in scripts as well - frazman

@Fraz - I'm not aware of any comprehensive list of differences between the interpreter and running scripts, but docs.python.org/tutorial/interpreter.html contains most of the relevant information. - Andrew Clark

@Fraz - I know, someone else did but there isn't any way to tell who. The comment wasn't to you directly, just hoping that whoever did will see my comment and give an explanation. - Andrew Clark

If you really want to change the module search path, extend the sys.path list. Changing directories is not suggested.

Respondido el 30 de enero de 12 a las 23:01

Hi.. I dont want to set up an absolute path.. as the code will be moving to different machines.. and want to do the relative setting up of path.. to find the libraries from current directory.. :( hence this attempt - frazman

Nothing precludes you from including relative directories in sys.path - Yann Ramin

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.