Seite 1 von 1

Problem mit Modulimport [solved]

Verfasst: Mittwoch 24. Dezember 2008, 11:44
von ceebes
Hallo
Da ich noch ziemlich am Anfang mit Python stehe, kann ich aus folgender Fehlermeldung nicht sonderlich viel ableiten....

Meine Umgebung:
Ubuntu 8.10
Python 2.5

Den code den ich im script ausführe

Code: Alles auswählen

import optparse
Die Fehlermeldung(en):

Code: Alles auswählen

python2.5 test.py 
fii10.0
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import optparse
  File "/usr/lib/python2.5/optparse.py", line 71, in <module>
    import textwrap
  File "/usr/lib/python2.5/textwrap.py", line 32, in <module>
    class TextWrapper:
  File "/usr/lib/python2.5/textwrap.py", line 68, in TextWrapper
    whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/apport_python_hook.py", line 38, in apport_excepthook
    from apport.packaging_impl import impl as packaging
  File "/usr/lib/python2.5/site-packages/apport/__init__.py", line 1, in <module>
    from apport.report import Report
  File "/usr/lib/python2.5/site-packages/apport/report.py", line 20, in <module>
    from problem_report import ProblemReport
  File "/usr/lib/python2.5/site-packages/problem_report.py", line 17, in <module>
    from email.Encoders import encode_base64
  File "/usr/lib/python2.5/email/__init__.py", line 113, in <module>
    lower_map = string.maketrans(string.ascii_uppercase, string.ascii_lowercase)
AttributeError: 'module' object has no attribute 'maketrans'

Original exception was:
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import optparse
  File "/usr/lib/python2.5/optparse.py", line 71, in <module>
    import textwrap
  File "/usr/lib/python2.5/textwrap.py", line 32, in <module>
    class TextWrapper:
  File "/usr/lib/python2.5/textwrap.py", line 68, in TextWrapper
    whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
[/code]

Interessanterweise trat das Problem nicht von Anfang an auf...
Im Netz habe ich einiges ähnliches gefunden, jedoch keine Lösung...??
Ich hoffe ich stolpere über einen absoluten Anfägerfehler!!

Gruss und schöne Weihnachten
Adrian

Verfasst: Mittwoch 24. Dezember 2008, 12:11
von Leonidas
Hallo ceebes, willkommen im Forum,

Kann es sein dass du irgendwo eine Datei namens ``string.py[c|o]?`` angelegt hast?

Verfasst: Mittwoch 24. Dezember 2008, 13:19
von ceebes
Ja hab ich!!
File gelöscht, funktion nun ok!!!

Wurde die Datei als vermeindliche Klasse verwendet, und verursachte so Probleme?
Muss ich demnach auf eine "spezielle Konvention" achten wenn ich Dateien anlege??

Besten Dank für die Antwort!!

Verfasst: Mittwoch 24. Dezember 2008, 13:53
von Darii
Die Konvention ist einfach, dass jede Python-Datei ein Modul ist. Und beim import von Modulen wird zuerst im aktuellen Verzeichnis gesucht. Wenn du dort eine Datei hast die genauso heißt, wie ein Modul der Standardbibliothek, kommt es dabei natürlich zu Problemen.