Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python: how can I import this?

NameChanger

 -main.py
 -__init__.py

 ui
   -__init__.py
   -utils.py

 utils
   -__init__.py
   -file.py

I want to import NameChanger/utils/file.py in NameChanger/ui/utils.py.

so I tried this code from NameChanger.utils import file but this error occured ModuleNotFoundError: No module named 'NameChanger'

and I also tried from ...NameChanger.utils import file and this error ValueError: attempted relative import beyond top-level package

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

how can I solve this?
thank you for reading and sorry for my bad english

>Solution :

In ui/utils.py, use a relative import.

from . import utils.file

NameChanger itself is likely not in your search path (nor does it need to be). But since NameChanger is a package, the relative import in a module contained in a subpackage of NameChanger should work.


I originally suggested

from .utils import file

which would only work if utils really had file as a module-level attribute, which is generally not the case for a package.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading