Unable to import package and module KIVY

Question:

Any idea what I am missing here? I am trying to import functions from function.py to dashboard.py using " from .db_functions.function import * ".

It looks like function.py cannot be found and then dashboard.kv cannot be imported to the main view.

Here is my NEW folder structure for this dashboard view, same error:

enter image description here

enter image description here

EDIT:
I changed the file structure and now I am using the import as suggested in the comments:" from function import general_insert "

Asked By: Michal

||

Answers:

I think the issue you’re having is as follows, (this is not a totally accurate situation but just stay with me for now) you run the package dashboard in another script from your home directory (~/dashboard/__init__.py for example from ~) and that package imports another package relative to it self (.dashboard) so it works as it then imports from as the directory ~/dashboard/* as it is relative to the script __init__, but when dashboard.py tries to import functions.py, the import path isn’t relative to the dashboard.py but uses the default path ., which is set to the current directory it was started in (~), thas why the error occurs.

(Sorry if it’s confusing, comment if you need clarification or if i said anything incorrect).

home/    # '~' in this made-up situation
└─ dashboard/
   ├─ __init__.py
   ├─ dashboard.py
   └─ functions.py

# Not full step-by-step path for the import process
"""
... Script runs before hand ...

The dashboard package is run from home directory

'~/dashboard/__init__.py' is executed from '~'
'~/dashboard/__init__.py' imports '.dashboard' relatively

'~/dashboard' package is directly searched for 'dashboard.py': FOUND

'~/dashboard/dashboard.py' is executed from '~'
'~/dashboard/dashboard.py' imports 'functions' universally

'~' is searched for 'functions.py': NOT FOUND
'sys.path' is searched for 'functions.py': NOT FOUND

'~/dashboard/dashboard.py' complains module is not found
Error is raised
"""

A useful source where it explains a bit more on python’s imports:

https://realpython.com/absolute-vs-relative-python-imports/#how-imports-work:~:text=The%20first%20thing,is%20searched%20first.

The first thing Python will do is look up the name abc in sys.modules. This is a cache of all modules that have been previously imported.

If the name isn’t found in the module cache, Python will proceed to search through a list of built-in modules. These are modules that come pre-installed with Python and can be found in the Python Standard Library. If the name still isn’t found in the built-in modules, Python then searches for it in a list of directories defined by sys.path. This list usually includes the current directory, which is searched first.

EDIT: Crude work-around

Python searches in a specific way to import modules and to ensure it finds the module in your case, you can directly put the directory containing the script into the search path to make sure it is found and imported when needed.

# Add this to the beginning of dashboard.py
import sys
sys.path.insert(0, "C:\RFQMS\views\dashboard\")
Answered By: MOStudios
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.