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?
Module importieren
- 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
importieren.
Code: Alles auswählen
import modules.module
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:
Weil dieser Ordner nicht in scripts liegt.
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')
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.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.)
Könnte mir jemand auf die Sprünge helfen?
Bitte...
-
- Python-Forum Veteran
- Beiträge: 16025
- Registriert: Freitag 20. Juni 2003, 16:30
- Kontaktdaten:
Wenn du den ``sys.path`` anpassen musst, dann ist deine Ordnerstruktur nicht so durchdacht wie du dir das denken würdest.Crazed hat geschrieben:Ich habe eine (meiner Meinung nach) relativ gut durchdachte Ordnerstruktur.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Du muss einfach die Umgebungsvariable PYTHONPATH um den Pfad zu deinen Modulen erweitern. Das geht zum Beispiel indem duCrazed hat geschrieben: Tut mir Leid, ich verstehe es nicht wirklich.
Könnte mir jemand auf die Sprünge helfen?
Bitte...
in deine .bashrc schreibst (unter Linux).PYTHONPATH="$PYTHONPATH:Pfad_zu_den_Modulen"
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')
Zuletzt geändert von epsilon am Dienstag 22. Juli 2008, 14:47, insgesamt 1-mal geändert.
- 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.
Dann sag mir bitte einfach wie ich den anderen Pfad inkludieren soll.
Ich muss ja erstmal eine Ebene höher:
also
oO
Ich muss ja erstmal eine Ebene höher:
also
Code: Alles auswählen
sys.path.append('../modules/')