2

This is my file structure: enter image description here

I am trying to get a function (getFreq()) out of the utils.py file.

However, when doing this in the part1.py file:

from ..utils.utils import getFreq

print(getFreq(["R8","U5","L5","D3"]))

It just says: ImportError: attempted relative import with no known parent package.

How would I fix this and import it?

Note: the __init__.py file is empty in the utils folder to make Python think it is a module (What I have been told to do on Google).

Psuedodoro
  • 177
  • 2
  • 14
  • Does the parent directory of `day3` (presumably `2021`) have an `__init__.py`, so it is considered as a Python module? – Pierre Dec 05 '21 at 18:03
  • Think so - at least that is what google have said :) – Psuedodoro Dec 05 '21 at 18:04
  • When I look at your screenshot I am under the impression that there is only a `.gitignore` but no `__init__.py` inside the `2021` directory. – Pierre Dec 05 '21 at 18:05
  • Hmm, will that fix it? – Psuedodoro Dec 05 '21 at 18:05
  • No, it doesn't fix it... – Psuedodoro Dec 05 '21 at 18:05
  • This is asked constantly, and `__init__.py` files [aren't relevant to this particular problem](https://stackoverflow.com/questions/448271/what-is-init-py-for). The problem is that when you tell Python to run `part1.py` directly, it has, per the rules, no reason to treat the `2021` folder as a package root. The leading `.`s don't traverse the folder hierarchy on disk (after all, there are other ways to import a module than reading a .py file!); they traverse a *package* hierarchy. Please see the duplicates I linked for details. – Karl Knechtel Dec 05 '21 at 18:08
  • That said, you are still going to run into problems, because the implied, fully-qualified name of your module within its package is `2021.day3.part1`, which is not a valid identifier. You will want to rename your `2021` folder. – Karl Knechtel Dec 05 '21 at 18:11

0 Answers0