[Python] import modules from external directories

Discussão em 'Python' iniciado por Stack, Janeiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a python app in which I have the following structure:

    project/
    ├── src/
    │ ├── main.py
    │ ├── pdfs/
    │ │ ├── create_pdf1.py
    │ │ ├── create_pdf2.py
    │ │ ├── create_pdf3.py
    │ │ ├── create_pdf4.py
    │ │ ├── create_pdf5.py
    │ │ └── merge_pdfs.py
    │ ├── entities/
    │ │ ├── entitie1.py
    │ ├── utils/
    │ │ └── helpers.py
    ├── aux/
    │ ├── pdf_aux1.pdf
    │ └── pdf_aux2.pdf
    ├── out/
    ├── result1.pdf
    └── result2.pdf


    I am now working on the create_pdf1.py module and trying to import a class from the entitie1.py module but I keep getting the following error when I run the code:

    Traceback (most recent call last):
    File "c:\Users\ricar\project\src\pdfs\create_pdf1.py", line 3, in <module>
    from entities.entitie1 import Legajo, Persona, Domicilio, Empresa
    ModuleNotFoundError: No module named 'entities'


    I tried using from ..entities.entitie1 import Legajo, Persona, Domicilio, Empresa but I get this error ImportError: attempted relative import with no known parent package

    One thing I have to say us that up until yesterday I didn't have the code distributed like this. All the modules where in the src folder and I could import them without any trouble. It was when I re-organized everything that I run the code and this errors started appearing.

    Continue reading...

Compartilhe esta Página