0

I have read the solutions from other answers and implemented them, but I'm still getting a "attempted relative import beyond top-level package" error. Here is my directory structure:

lab (has init)
  examples (has init)
    static (has init)
      experiment.py (I'm in this file in static, and I want to access samples)
 objects (has init)
   components (has init)
     Samples.py (the file I want to import)

The following line is causing the error, but it makes sense logically.

from ...lab.objects.components import Samples

yields

ValueError: attempted relative import beyond top-level package

What's wrong exactly?

For added clarity: My current directory is: lab/examples/static and I'm in experiment.py

I want to import: lab/objects/components/Samples.py

"lab" is the top-most level package directory.

Quinty
  • 189
  • 1
  • 1
  • 12
  • as it says, you are attempting to import somethign beyond the top level of your package. is `lab` your root folder of the package? if so, all you need is `from lab.objects.components import Samples` – crookedleaf Jun 15 '18 at 19:33
  • @crookedleaf Thank you for the response. lab should be the root folder of the package. The line above yields a different error. (" No module named 'lab'") – Quinty Jun 15 '18 at 19:36
  • if you are getting that error, than lab isn't the top level of your package. create a new file in `lab/objects/components` that only does: `import os; import sys; print os.path.dirname(sys.modules['__main__'].__file__)` and let me know what it says. – crookedleaf Jun 15 '18 at 20:08
  • @crookedleaf C:/Users/myuser/Documents/GitHub/lab/objects/components – Quinty Jun 15 '18 at 20:12
  • Strangely enough, the objects and lab folder contain an __init__.py file – Quinty Jun 15 '18 at 20:13
  • is it `init.py` or `__init__.py`...? – crookedleaf Jun 15 '18 at 20:18
  • @crookedleaf it's "__ init __.py" for all the folders – Quinty Jun 15 '18 at 20:23
  • 1
    i'm guessing if you run `from objects.components import Samples` it will work since `lab` is acting as your projects directory, and not it's top level. does your directory `GitHub` have an `__init__.py` file, by some weird chance? – crookedleaf Jun 15 '18 at 20:34
  • @crookedleaf That worked. Thanks!! I'm not really sure why lab is not acting as the top level. – Quinty Jun 15 '18 at 20:42

0 Answers0