Django view: Can't import module from another directory

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.

Your issue is probably with how you're importing modules inside the package.

When you write:

from core_project.python_code.object_refresh import *

You're only importing what's defined in _init_.py, not the actual files like refresh.py or config.py. So, refresh isn't visible by default.

To fix it, go to the _init_.py file inside the object_refresh/ folder and add:

from . import refresh
from . import config
Вернуться на верх