[Python] Django view: Can't import module from another directory

Discussão em 'Python' iniciado por Stack, Abril 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have a django project structure like so:

    core_project/
    App1/
    Views.py
    Models.py
    ..
    App2/
    Views.py
    Models.py
    ..

    Python_code/
    Object_refresh/
    __init.py__
    refresh.py
    config.py


    Inside views.py I have a view called "run_refresh" that will run refesh.py from python_code. But I run into a problem trying to reference other .py files inside Python_code.

    In the view I use: from python_code import * But it isn't able to see main.py. If I type out "from python_code import main" it works but then it can't find env_config.py.

    This is what I have in my view.py:

    from core_project.python_code.object_refresh import *

    ..



    And this is the error:


    newfeature = asyncio.run(object_refresh.refresh(rows_to_refresh, schema, 'tbl_name'))
    print(newfeature)


    Error:
    newfeature = asyncio.run(object_refresh.refresh(rows_to_refresh, schema, 'tbl_name'))
    ^^^^^^^^^^^^^^^^^^^
    AttributeError: module 'core_project.python_code.object_refresh' has no attribute 'refresh'



    Maybe i need to setup my init.py a certain way but i'm not sure. Any help is appreciated.

    Continue reading...

Compartilhe esta Página