1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Issue with module hierarchy in Python

Discussão em 'Python' iniciado por Stack, Dezembro 21, 2024.

  1. Stack

    Stack Membro Participativo

    mt-kart
    |
    --> src/data_kart
    | |
    | --> apis
    | | |
    | | --> __init__.py
    | | --> endpoints.py
    | |
    | --> models
    | | |
    | | --> __init__.py
    | | --> modelx.py
    | |
    | --> defaults.py
    | --> main.py
    --> tests
    |
    --> __init__.py
    |
    --> conftest.py
    |
    --> test_others.py


    In main.py I get module not found error when I try to refer:

    from defaults import func1

    But this works:

    from .defaults import func1

    It should work without "." right?

    2.

    From endpoints.py how should I refer the modelx.py? My previous developer had it like below:

    from data_kart.models.modelx import model1

    But it complains when I run some tests using pytest from mt-kart directory. Then the below worked for pytest. But i am afraid it may give error in some different scenario:

    from src.data_kart.models.modelx import model1

    The project uses FastAPI and OpenAPI Code generators

    Edit

    In conftest.py I added src to sys.path and the paths are working fine for my issue 2. sys.path.append(os.getcwd()+"/src")

    Continue reading...

Compartilhe esta Página