Trouble with module import

Forgive me… i’m a Python very beginner…
I’m following the book but i’m facing a problem in executing this (at page 76…) :

from analog_daq import AnalogDaq

I’m getting the following problem :


from …Controller import SimpleDaq
ImportError: attempted relative import with no known parent package

I’m not going to find a solution…

Hello,
I’m not sure I could follow your question completely. Regarding imports in Python, I wrote quite a detailed guide, which you can read to see if it allows you to solve the problem.

With the little information I have, I would say you are probably either running the program from the wrong folder, or you don’t have the same folder structure than the book suggests. What you can do, is put all the Python files in the same folder, and update the import statements. So, for example instead of doing:

from ...Controller import SimpleDaq

you can simply do from simple_daq import SimpleDaq.

I am not sure where the ... before Controller are coming from in this context…

May be my folder structure is slightly different than the one on the book…
I think the python interpreter does not have any information about the package the module belong…

Sure i’ll follow you guide !

Thanks.

Also this feedback is great to improve the book and avoid confusions with other readers!

When a Python script attempts to perform a relative import but does not have a recognized parent package. In Python, relative imports are used to import modules or subpackages within the same package or subpackage. To fix this error “no known parent package”, you need to ensure that your script is being executed from within a proper Python package structure. This means that your script should reside inside a directory that is recognized as a Python package, typically with an init.py file. If the script is intended to be run as a standalone script, you may need to refactor the imports to use absolute import statements instead of relative imports. Alternatively, if the script is meant to be part of a package, ensure that the package structure is correctly set up, and the script is being executed from the appropriate location within the package hierarchy.