"ImportError: attempted relative import with no known parent package", Relative path does exists

Viewed 4261

I'm having trouble doing a relative import in python. I have a directory of which i'm trying to import a module inside a subdirectory.

My layout is the following is attached as a file.https://i.stack.imgur.com/oV4fo.png

Inside the Math folder I'm trying to import Vec.py into matrix.py with use of "from .Vec import Vec2D", where Vec2D is a class. However I get an import error "ImportError: attempted relative import with no known parent package".

Using sys.path I can see that I should have access to this file however this is not the case.

Would appreciate some help, and I'm running Python 3.8 :)

1 Answers

You should create a folder containing the Vec2d.py package and then call the import from your matrix.py.

The new structure will be as follows:

Math
 -matrix.py
 -mypackages
   -__init__.py
   -Vec.py

With this structure you can now import from matrix like this:

from mypackages.Vec import Vec2D
Related