2015-04-09 9 views
10

Ich habe ein Projekt als so strukturiert:Import schlägt fehl, wenn Python als Skript ausgeführt wird, aber nicht in iPython?

folder1 
     | 
     folder2 
      | 
      tests 

Ich habe __init__.py in jedem Ordner. Wenn ich im übergeordneten Verzeichnis von Ordner1 bin, führe ich iPython und mache

from folder1.folder2.tests.test1 import main 
main() 

alles funktioniert gut. Allerdings, wenn ich

python folder1/folder2/tests/test1.py 

laufe ich Import: Kein Modul mit dem Namen folder1.folder2.file1, wo mein Import-Anweisung in test1 ist

from folder1.folder2.file1 import class1 

Verwirrt über diese - ich vermute, bin ein Pfad Problem aber ich verstehe nicht, was falsch mit meinem Code ist (viele ähnliche Setups in anderen Ordnern) und warum es immer noch in iPython funktioniert und nicht Python als Skript ausgeführt wird.

Antwort

8

Die module search path (python 3 docu) unterscheidet sich mit und ohne Skriptdatei:

interaktive Python-Interpreter

(beide gilt für python und ipython)

$ python 
Python 2.7.3 (default, Dec 18 2014, 19:10:20) 
[GCC 4.6.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import sys 
>>> print(sys.path) 
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7/gtk-2.0', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol'] 
>>> 

Hinweis der erste Eintrag ein Wesen leerer String. Eine leere Zeichenfolge ist ein relativer Pfad, der . entspricht. Relative Pfade im Modulsuchpfad sind relativ zum aktuellen Arbeitsverzeichnis des Interpreterprozesses. Dies ist also nur das aktuelle Arbeitsverzeichnis, in dem Sie den Interpreter aufgerufen haben. (Was in Ihrem Fall passiert die Wurzel des Projekts sein.)

eine Skriptdatei ausführen

$ echo 'import sys' > /tmp/pathtest.py 
$ echo 'print(sys.path)' >> /tmp/pathtest.py 
$ python /tmp/pathtest.py 
['/tmp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7/gtk-2.0', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol'] 

Hinweis darauf, dass hier der erste Eintrag ist der absolute Pfad des Verzeichnisses, die Skriptdatei enthält, wir übergeben als ein Argument.

+3

so im Grunde hinzufügen 'import sys; sys.path = [''] + sys.path' zu Ihrem Skript – aforaudrey

+1

auch, nicht in Versuchung geraten, 'sys.path.extend ([''])' (wie ich es tat), da die Reihenfolge macht ein Unterschied. – gpano

4

Ich konfrontiert ein ähnliches Problem beim Importieren von numpy oder einer Bibliothek abhängig von numpy. Das Problem war, dass ich einen Dateinamen namens random.py in meinem Projektordner hatte.

Numpy hat random.py drin für seine zufälligen Funktionen, aber das Importieren von random.py nahm die random.py meines Projektordners.

Die beste Lösung besteht darin, keine Datei mit den Standard-Modulnamen einer Bibliothek zu benennen.

Enjoy .. :)

+0

Oh mein Gott ... Vielen Dank! Ich benutzte den Namen "Redmine" und versuchte Redmine Modul zu importieren ... – QtRoS

Verwandte Themen