
Hallo,
ich importeire derzeit Dateien mit z.B.
from xyz import *
Dabei sucht u.a. im aktuellen VErzeichnis. Wie kann ich aber eine Datei in dem Unterordner classes des aktuellen Verzeichnisses importieren?
MfG
Stefan
_______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de

Hallo,
Stefan Miefert wrote:
ich importeire derzeit Dateien mit z.B.
from xyz import *
Das wird, soweit ich weiß, nicht empfohlen. Besser ist:
import xyz
Dabei sucht u.a. im aktuellen VErzeichnis. Wie kann ich aber eine Datei in dem Unterordner classes des aktuellen Verzeichnisses importieren?
import sys sys.path.append('/pfad/zu/deinem/unterordner') import foo
Damit sucht Python auch in dem Unterordner nach Dateien zum Importieren.
Gruß, Henrik
_______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
die hacker lösung:
import sys, os sys.path.append( os.path.join( os.getcwd, "classes" ) )
from xyz import *
für paranoiker gibts noch: sys.path.insert( 0, os.path.join( os.getcwd, "classes" ) )
die saubere lösung: im Verzeichnis "classes" ein module "__init__.py" anlegen, damit wird classes zu einem paket und man kann einfach
from classes.xyz import *
schreiben.
HTH, Gerald
PS: Schneller gehst auf irc://irc.freenode.net#python.de
Stefan Miefert schrieb: | Hallo, | | ich importeire derzeit Dateien mit z.B. | | from xyz import * | | Dabei sucht u.a. im aktuellen VErzeichnis. Wie kann ich aber eine Datei | in dem Unterordner classes des aktuellen Verzeichnisses importieren? | | MfG | | Stefan | | | _______________________________________________ | python-de maillist - python-de@python.net | http://python.net/mailman/listinfo/python-de
- -- GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
_______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de

Gerald Klix schrieb:
für paranoiker gibts noch: sys.path.insert( 0, os.path.join( os.getcwd, "classes" ) )
Scheidert, wenn das CWD sonstwo ist. Tipp: Modul 'inspect'

sowieso! aber das war ja nicht das Problem, nachdem gefragt wurde.
Mir scheint im Fall der Fälle die Lösung relativ zum aktuellen Modul am sinnvollsten.
import os, sys sys.path.insert( 0, os.path.abspath( os.path.join( os.path.dirname( __file__ ), "classes" ) ) )
HTH, Gerald
Hartmut Goebel schrieb:
Gerald Klix schrieb:
für paranoiker gibts noch: sys.path.insert( 0, os.path.join( os.getcwd, "classes" ) )
Scheidert, wenn das CWD sonstwo ist. Tipp: Modul 'inspect'

Gerald Klix schrieb:
für paranoiker gibts noch: sys.path.insert( 0, os.path.join( os.getcwd, "classes" ) )
Scheidert, wenn das CWD sonstwo ist. Tipp: Modul 'inspect'
Einfacher wäre doch sys.path.insert( 0, os.path.join( __file__, "classes" ) ) ?
Gruß, Karl.
-- Schönen Gruß - Regards Hartmut Goebel
| Hartmut Goebel | IT-Security -- effizient | | h.goebel@goebel-consult.de | www.goebel-consult.de |
python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
_______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
participants (5)
-
Gerald Klix
-
Hartmut Goebel
-
Henrik Ronellenfitsch
-
Karl Scalet
-
Stefan Miefert