Seite 1 von 1

Module importieren

Verfasst: Dienstag 22. Juli 2008, 11:19
von Crazed
Hallo,

Ich habe eine (meiner Meinung nach) relativ gut durchdachte Ordnerstruktur.
Ich habe jetzt aber folgendes Problem:

Ich möchte z.b aus einem Ordner

<scripts>:
scripts.py

<modules>:
module.py

über scripts.py module.py aufrufen. Aber diese liegen nicht auf der gleichen Ebene, sondern jeweils jeder in einem eigenen Ordner. Könnte mir jemand verraten wie scripts.py auf module.py zugreifen kann?

Verfasst: Dienstag 22. Juli 2008, 11:23
von cofi
Mach `modules' zu einem Package-Ordner, in dem du eine __init__.py erstellst. Wenn `modules' in deinem Pfad ist kannst du module.py dann per

Code: Alles auswählen

import modules.module
importieren.

Verfasst: Dienstag 22. Juli 2008, 11:43
von Crazed
Klar dafür muss ich es aber doch erstmal in meinem Pfad bekommen.
Und das geht ja wie oben bereits gesagt nicht.

Weil ich kann von scripts.py nicht folgendes machen:

Code: Alles auswählen

sys.path.append('modules')
Weil dieser Ordner nicht in scripts liegt.

Verfasst: Dienstag 22. Juli 2008, 12:41
von epsilon
Hier steht, was du suchst:
There are two environment variables that can modify sys.path. PYTHONHOME sets an alternate value for the prefix of the Python installation. For example, if PYTHONHOME is set to "/www/python", the search path will be set to ['', '/www/python/lib/python2.5/', '/www/python/lib/python2.5/plat-linux2', ...].

The PYTHONPATH variable can be set to a list of paths that will be added to the beginning of sys.path. For example, if PYTHONPATH is set to "/www/python:/opt/py", the search path will begin with ['/www/python', '/opt/py']. (Note that directories must exist in order to be added to sys.path; the site module removes paths that don't exist.)

Verfasst: Dienstag 22. Juli 2008, 13:50
von Crazed
epsilon hat geschrieben:Hier steht, was du suchst:
There are two environment variables that can modify sys.path. PYTHONHOME sets an alternate value for the prefix of the Python installation. For example, if PYTHONHOME is set to "/www/python", the search path will be set to ['', '/www/python/lib/python2.5/', '/www/python/lib/python2.5/plat-linux2', ...].

The PYTHONPATH variable can be set to a list of paths that will be added to the beginning of sys.path. For example, if PYTHONPATH is set to "/www/python:/opt/py", the search path will begin with ['/www/python', '/opt/py']. (Note that directories must exist in order to be added to sys.path; the site module removes paths that don't exist.)
Tut mir Leid, ich verstehe es nicht wirklich.
Könnte mir jemand auf die Sprünge helfen?

Bitte...

Re: Module importieren

Verfasst: Dienstag 22. Juli 2008, 14:06
von Leonidas
Crazed hat geschrieben:Ich habe eine (meiner Meinung nach) relativ gut durchdachte Ordnerstruktur.
Wenn du den ``sys.path`` anpassen musst, dann ist deine Ordnerstruktur nicht so durchdacht wie du dir das denken würdest.

Verfasst: Dienstag 22. Juli 2008, 14:28
von epsilon
Crazed hat geschrieben: Tut mir Leid, ich verstehe es nicht wirklich.
Könnte mir jemand auf die Sprünge helfen?

Bitte...
Du muss einfach die Umgebungsvariable PYTHONPATH um den Pfad zu deinen Modulen erweitern. Das geht zum Beispiel indem du
PYTHONPATH="$PYTHONPATH:Pfad_zu_den_Modulen"
in deine .bashrc schreibst (unter Linux).

Keine Ahnung, wie das unter Windows geht.

Ansonsten kannst du auch immer

Code: Alles auswählen

import sys
sys.path.append('Pfad_zu_den_Modulen')
schreiben, bevor du deine Module importierst.

Verfasst: Dienstag 22. Juli 2008, 14:36
von cofi
Muss mich Leonidas anschliessen. Aber warum kannst du nicht über ein Programm aus dem Verzeichnis über `scripts' und `modules' aufrufen? Beide Ordner kommen mir eher als Add-on vor, die sich eben in das Programm importieren lassen, das ich meine.

Verfasst: Dienstag 22. Juli 2008, 15:12
von Crazed
Dann sag mir bitte einfach wie ich den anderen Pfad inkludieren soll.
Ich muss ja erstmal eine Ebene höher:

also

Code: Alles auswählen

sys.path.append('../modules/')
oO