Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > de.comp.lang.python > #6056

Re: [Python-de] Re: Pfade, Modulnamen und import-Statements

From "Peter J. Holzer" <hjp-usenet4@hjp.at>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] Re: Pfade, Modulnamen und import-Statements
Date 2024-05-05 12:38 +0200
Organization LUGA
Message-ID <slrnv3eocq.16cpt.hjp-usenet4@trintignant.hjp.at> (permalink)
References <ZjYYBgoIiqLkxkB3@torres.zugschlus.de> <slrnv3dkgq.14mcq.hjp-usenet4@trintignant.hjp.at> <ZjdCdg94EQs3zPul@torres.zugschlus.de>

Show all headers | View raw


On 2024-05-05 08:25, Marc Haber <mh+python-de@zugschlus.de> wrote:
> On Sun, May 05, 2024 at 02:26:02AM +0200, Peter J. Holzer wrote:
>> On 2024-05-04 11:12, Marc Haber <mh+python-de@zugschlus.de> wrote:
>> > ich hab mal eine Verständnisfrage.
>> >
>> > Gegeben sei ein Verzeichnis . und ein Programm ./keks.
>> > Weiterhin sei die Klasse MyClass1 in d/mc1.py und die Klasse MyClass2 in
>> > d/mc2.py definierrt.
>> >
>> > Wenn ich nun in keks die Klasse MyClass2 benutzen möchte, schreibe ich
>> > "from d.mc2 import MyClass2". Wnn ich in d/mc1.py dasselbe tun möchte,
>> > muss ich dort "from mc2 import MyClass2" schreiben,
>> 
>> Nein, das funktioniert nicht:
>> 
>> #v+
>> % ./keks
>> Traceback (most recent call last):
>>   File "/home/hjp/tmp/marc/./keks", line 3, in <module>
>>     from d.mc1 import MyClass1
>>   File "/home/hjp/tmp/marc/d/mc1.py", line 1, in <module>
>>     from mc2 import MyClass2
>> ModuleNotFoundError: No module named 'mc2'
>> #v-
>
> Mein mc1.py hat eingebauten testcode:
>
> #v+
> if __name__ == "__main__":
>     zdns = Zg2DNSQuery(timeout=2, debug=True)
>     
>     rootservers = DNSRootDataParser()
>     logger.debug(f"{rootservers.get_ipv4_servers()=}")
> #v-

Ah, dachte ich mir, dass da eine wichtige Information fehlt ;-).


> verhält sich der vielleicht anders als wenn man nur das Modul
> importiert? Das wäre dann ja noch viel schlimmer ;-)

Wenn Du ein Script aufrufst, wird das Directory des Scripts in sys.path
eingefügt. Dadurch wird dann bei einem "import m" u.a. auch nach
"full_path_to_script_dir/m.py" gesucht und damit funktioniert der
Import.

Zum Testen von komplexeren Packages ist das aber wenig hilfreich, weil
im echten Einsatz natürlich nicht irgendwelche Subdirectorys der
Package-Hierarchie im Suchpfad stehen und man, wenn man ein Modul aus
einem Package direkt aufruft, die Package-Hierarchie verliert und damit
relative Imports nicht funktionieren:

#v+
% python3 d/mc1.py
__main__ ['/home/hjp/tmp/marc/d', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/hjp/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages']
Traceback (most recent call last):
  File "/home/hjp/tmp/marc/d/mc1.py", line 4, in <module>
    from .mc2 import MyClass2
ImportError: attempted relative import with no known parent package
#v-

Unter anderem auch aus diesem Grund schreibe ich Test-Code immer in
eigene Files.

        hp

Back to de.comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

[Python-de] Pfade, Modulnamen und import-Statements Marc Haber <mh+python-de@zugschlus.de> - 2024-05-04 13:12 +0200
  [Python-de] Re: Pfade, Modulnamen und import-Statements Hartmut Goebel <h.goebel@crazy-compilers.com> - 2024-05-04 19:22 +0200
    [Python-de] Re: Pfade, Modulnamen und import-Statements Marc Haber <mh+python-de@zugschlus.de> - 2024-05-05 10:21 +0200
      Re: [Python-de] Re: Pfade, Modulnamen und import-Statements Bastian Blank <usenet@waldi.eu.org> - 2024-05-05 10:32 +0000
        [Python-de] Re: Pfade, Modulnamen und import-Statements Marc Haber <mh+python-de@zugschlus.de> - 2024-05-10 11:37 +0200
          [Python-de] Re: Pfade, Modulnamen und import-Statements Hartmut Goebel <h.goebel@crazy-compilers.com> - 2024-05-11 17:49 +0200
  Re: [Python-de] Pfade, Modulnamen und import-Statements "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-05-05 02:26 +0200
    [Python-de] Re: Pfade, Modulnamen und import-Statements Marc Haber <mh+python-de@zugschlus.de> - 2024-05-05 10:25 +0200
      Re: [Python-de] Re: Pfade, Modulnamen und import-Statements "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-05-05 12:38 +0200
        [Python-de] Re: Pfade, Modulnamen und import-Statements Marc Haber <mh+python-de@zugschlus.de> - 2024-05-10 11:39 +0200
          Re: [Python-de] Re: Pfade, Modulnamen und import-Statements "Peter J. Holzer" <hjp-usenet4@hjp.at> - 2024-05-10 19:25 +0200
  Re: [Python-de] Pfade, Modulnamen und import-Statements Hermann Riemann <nospam.ng@hermann-riemann.de> - 2024-05-05 12:02 +0200
    [Python-de] Re: Pfade, Modulnamen und import-Statements Marc Haber <mh+python-de@zugschlus.de> - 2024-05-05 12:35 +0200
  Re: [Python-de] Pfade, Modulnamen und import-Statements Hermann Riemann <nospam.ng@hermann-riemann.de> - 2024-05-10 20:05 +0200
    [Python-de] Re: Pfade, Modulnamen und import-Statements Christopher Arndt <chris@chrisarndt.de> - 2024-05-11 15:18 +0200
    [Python-de] Re: Pfade, Modulnamen und import-Statements Hartmut Goebel <h.goebel@goebel-consult.de> - 2025-01-26 21:21 +0100

csiph-web