2

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 :)

Aristotes
  • 41
  • 1
  • 4
  • Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Philipp H. Jun 10 '20 at 11:15

1 Answers1

1

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
IMB
  • 519
  • 4
  • 19
  • Thanks! I'll try this, it worked with absolute imports, and i've thought of using pypi to upload and use it in that sense – Aristotes Jun 11 '20 at 16:52