Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


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

Re: Paket Modul import Problem.

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups de.comp.lang.python
Subject Re: Paket Modul import Problem.
Date 18 Jun 2025 17:30:09 GMT
Organization Stefan Ram
Lines 66
Expires 1 Jun 2026 11:59:58 GMT
Message-ID <Thema-20250618182220@ram.dialup.fu-berlin.de> (permalink)
References <ld7taaFa0pcU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de l8WndhtS7DCqtS2cxjddBgunHJgnXPrTbcNWfBmJ8jhaXn
Cancel-Lock sha1:kBP8O4jzI857f9mazj8BUelbZFs= sha256:FzscILE5SwGfHoQAVvwtbGUZw3KHsPtxdYvJYaovVL4=
X-Copyright (C) Copyright 2025 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed.
X-No-Archive Yes
Archive no
X-No-Archive-Readme "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access.
X-No-Html yes
Content-Language de-DE-1901
Xref csiph.com de.comp.lang.python:6300

Show key headers only | View raw


Hermann Riemann <nospam.ng@hermann-riemann.de> schrieb oder zitierte:
>Gegeben 3 Dateien im Verzeichnis /irgendwas
>test.py mit Inhalt:
>print("test start")
>import grr
>print("test ende")

>grr/__init__.py mit Inhalt:
>print("start init grr")
>import bla
>print("ende  init grr")

>und grr/bla/__init__.py mit Inhalt:
>print("in init bla")

>   File "/irgendwas/grr/__init__.py", line 2, in <module>
>     import bla
>Warum wird da bla nicht gefunden?

  Ich hatte vielleicht schon einmal darauf geantwortet, aber finde
  dieses Thema immer wieder interessant, so daß ich hier noch einmal
  darauf antworten möchte, wobei ich mich vielleicht wiederhole.

  Wir haben

x/test.py
x/grr/__init__.py
x/grr/bla/__init__.py

  . Beim Aufruf

python test.py

  aus dem Verzeichnis "x" heraus ist "x" im Systempfad. "grr"
  wird von "x" aus gefunden. "bla" wird aber von "x" aus /nicht/
  gefunden. Daher kommt die Fehlermeldung. Man muß daher in
  "grr/__init__.py" so schreiben, als wäre man in "x", also

import grr.bla

  . Man sollte alle Skripte, die direkt ausgeführt werden und
  die "grr" nutzen sollen, nach "x" legen oder "grr" richtig
  installieren oder "x" zum Systempfad hinzufügen.

  Nun finde ich es umständlich, daß ich "grr/__init__.py" jetzt nicht
  mehr direkt ausführen kann. Man könnte aber für diesen Zweck am
  Anfang Code einfügen, der "x/grr" aus dem Suchpfad entfernt und dafür
  "x" hinzufügt. Etwas wie (so nicht getestet, nur symbolischer Code):

if __name__=='__main__': # am Anfang der Datei, vor den anderen imports
    import sys
    from pathlib import Path
    current_module_dir = Path(__file__).resolve().parent
    oberverzeichnis = str(current_module_dir.parent)
    sys.path =[ oberverzeichnis ]+\
    [ path for path in sys.path if not path.endswith( r'grr' ) ]

  Hier sollte durch die letzte Zeile das "...x/grr" aus
  dem Suchpfad entfernt werden und durch die vorletzte das
  Oberverzeichnis hinzugefügt werden (unter der Annahme, daß
  es nur einen Suchpfad gibt, der mit "grr" endet, sonst müßte
  man es noch anpassen). Dann kann man "x/grr/__init__.py" aus
  "x/grr" heraus als Skript starten, aber die Importe verhalten
  sich dann so, als habe man es aus "x" heraus aufgerufen.

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


Thread

Paket Modul import Problem. Hermann Riemann <nospam.ng@hermann-riemann.de> - 2024-06-16 12:32 +0200
  Re: Paket Modul import Problem. ram@zedat.fu-berlin.de (Stefan Ram) - 2025-06-18 17:30 +0000

csiph-web