OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python ModuleNotFoundError when using subpackage in my custom library

  • Thread starter Thread starter user25745772
  • Start date Start date
U

user25745772

Guest
When importing my own Python Library, a ModuleNotFoundError pops up, saying it can't find a subpackage. The project directory looks like this:

Code:
├── examples
│   └── app.py
├── makefile
├── pyproject.toml
└── src
    └── mymath
        ├── __init__.py
        ├── geometry
        │   ├── __init__.py
        │   ├── point.py
        │   └── rectangle.py
        └── lib.py

The library can be build via python -m build and installed via pip install dist/mymath-0.0.1-py3-none-any.whl. I then run the following example script in the examples/ directory which imports the library:

Code:
from mymath import RectangleService
a = RectangleService().create(0, 0, 4, 5)

This will then result in the following error:

Code:
Traceback (most recent call last):
  File "/home/user/bnr/repositories/testing/ideal-lamp/examples/app.py", line 1, in <module>
    from mymath import RectangleService
  File "/home/user/.local/lib/python3.10/site-packages/mymath/__init__.py", line 1, in <module>
    from .lib import *
  File "/home/user/.local/lib/python3.10/site-packages/mymath/lib.py", line 1, in <module>
    from .geometry.point import Point
  File "/home/user/.local/lib/python3.10/site-packages/mymath/geometry/__init__.py", line 2, in <module>
    from . import rectangle
  File "/home/user/.local/lib/python3.10/site-packages/mymath/geometry/rectangle.py", line 1, in <module>
    from geometry.point import Point
ModuleNotFoundError: No module named 'geometry'

The lib.py looks like this:

Code:
from .geometry.point import Point
from .geometry.rectangle import Rectangle

class RectangleService:
    def create(self, x1, y1, x2, y2):
        return Rectangle(Point(x1, y1), Point(x2, y2))

The geometry/rectangle.py looks like this:

Code:
from geometry.point import Point

class Rectangle:
    def __init__(self, upper_left: Point, lower_right: Point):
        self.upper_left = upper_left
        self.upper_right = Point(lower_right.x, upper_left.y)
        self.lower_right = lower_right
        self.lower_left = Point(upper_left.x, lower_right.y)

Any finally, the geometry/point.py looks like this:

Code:
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

At the end, the stacktrace indicates that the geometry/rectangle.py has troubles importing the geometry/point.py module, which is in the same directory. Changing the path to a relative import ( from geometry.point import Point --> from .point import Point) works. However, in my actual Implementation, the files in geometry will be generated (more specifically they will be python flatbuffer implementations) and I have no influence on them.

For testing, I created a script src/mymath/app.py (as seen below) and imported the geometry Classes the same way as they are imported in the src/mymath/lib.py. Executing the script with cd src/mymath/ && python app.py works then.

Code:
from geometry.point import Point
from geometry.rectangle import Rectangle

r = Rectangle(Point(0, 0), Point(1, 1))

Why does that not work in the same way when building the library? What can I do, except modifying the import paths on the generated files (src/mymath/geometry/*) manually?
<p>When importing my own Python Library, a <code>ModuleNotFoundError</code> pops up, saying it can't find a subpackage.
The project directory looks like this:</p>
<pre class="lang-bash prettyprint-override"><code>├── examples
│ └── app.py
├── makefile
├── pyproject.toml
└── src
└── mymath
├── __init__.py
├── geometry
│ ├── __init__.py
│ ├── point.py
│ └── rectangle.py
└── lib.py
</code></pre>
<p>The library can be build via <code>python -m build</code> and installed via <code>pip install dist/mymath-0.0.1-py3-none-any.whl</code>. I then run the following example script in the <code>examples/</code> directory which imports the library:</p>
<pre class="lang-py prettyprint-override"><code>from mymath import RectangleService
a = RectangleService().create(0, 0, 4, 5)
</code></pre>
<p>This will then result in the following error:</p>
<pre class="lang-bash prettyprint-override"><code>Traceback (most recent call last):
File "/home/user/bnr/repositories/testing/ideal-lamp/examples/app.py", line 1, in <module>
from mymath import RectangleService
File "/home/user/.local/lib/python3.10/site-packages/mymath/__init__.py", line 1, in <module>
from .lib import *
File "/home/user/.local/lib/python3.10/site-packages/mymath/lib.py", line 1, in <module>
from .geometry.point import Point
File "/home/user/.local/lib/python3.10/site-packages/mymath/geometry/__init__.py", line 2, in <module>
from . import rectangle
File "/home/user/.local/lib/python3.10/site-packages/mymath/geometry/rectangle.py", line 1, in <module>
from geometry.point import Point
ModuleNotFoundError: No module named 'geometry'
</code></pre>
<p>The <code>lib.py</code> looks like this:</p>
<pre class="lang-py prettyprint-override"><code>from .geometry.point import Point
from .geometry.rectangle import Rectangle

class RectangleService:
def create(self, x1, y1, x2, y2):
return Rectangle(Point(x1, y1), Point(x2, y2))
</code></pre>
<p>The <code>geometry/rectangle.py</code> looks like this:</p>
<pre class="lang-py prettyprint-override"><code>from geometry.point import Point

class Rectangle:
def __init__(self, upper_left: Point, lower_right: Point):
self.upper_left = upper_left
self.upper_right = Point(lower_right.x, upper_left.y)
self.lower_right = lower_right
self.lower_left = Point(upper_left.x, lower_right.y)
</code></pre>
<p>Any finally, the <code>geometry/point.py</code> looks like this:</p>
<pre class="lang-py prettyprint-override"><code>class Point:
def __init__(self, x, y):
self.x = x
self.y = y
</code></pre>
<p>At the end, the stacktrace indicates that the <code>geometry/rectangle.py</code> has troubles importing the <code>geometry/point.py</code> module, which is in the same directory. Changing the path to a relative import (
<code>from geometry.point import Point</code> --> <code>from .point import Point</code>) works. <strong>However</strong>, in my actual Implementation, the files in geometry will be generated (more specifically they will be python flatbuffer implementations) and I have no influence on them.</p>
<p>For testing, I created a script <code>src/mymath/app.py</code> (as seen below) and imported the geometry Classes the same way as they are imported in the <code>src/mymath/lib.py</code>. Executing the script with <code>cd src/mymath/ && python app.py</code> works then.</p>
<pre class="lang-py prettyprint-override"><code>from geometry.point import Point
from geometry.rectangle import Rectangle

r = Rectangle(Point(0, 0), Point(1, 1))
</code></pre>
<p>Why does that not work in the same way when building the library? What can I do, except modifying the import paths on the generated files (<code>src/mymath/geometry/*</code>) manually?</p>
 

Latest posts

A
Replies
0
Views
1
AgencyAnalytics
A
S
Replies
0
Views
1
Stacker Media
S
C
Replies
0
Views
1
CC.Talent
C
Top