Module importieren

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Crazed
User
Beiträge: 171
Registriert: Sonntag 18. Mai 2008, 11:44

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?
Benutzeravatar
cofi
Python-Forum Veteran
Beiträge: 4432
Registriert: Sonntag 30. März 2008, 04:16
Wohnort: RGFybXN0YWR0

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.
Crazed
User
Beiträge: 171
Registriert: Sonntag 18. Mai 2008, 11:44

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.
epsilon
User
Beiträge: 71
Registriert: Freitag 20. Juni 2008, 19:48

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.)
Crazed
User
Beiträge: 171
Registriert: Sonntag 18. Mai 2008, 11:44

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...
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

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.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
epsilon
User
Beiträge: 71
Registriert: Freitag 20. Juni 2008, 19:48

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.
Zuletzt geändert von epsilon am Dienstag 22. Juli 2008, 14:47, insgesamt 1-mal geändert.
Benutzeravatar
cofi
Python-Forum Veteran
Beiträge: 4432
Registriert: Sonntag 30. März 2008, 04:16
Wohnort: RGFybXN0YWR0

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.
Crazed
User
Beiträge: 171
Registriert: Sonntag 18. Mai 2008, 11:44

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
Antworten