Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find submodule within package

I feel a bit dumb asking this since there are a lot of similar questions, but I've honestly searched around a lot and could not find a solution for this. Here goes:

I have a Python package (on TestPyPi, here is the source code, note that it uses and needs python3.8) with the following structure:

paillier/
    setup.py
    test/
    paillier/
        __init__.py
        keygen.py
        util/
            __init__.py
            math_shortcuts.py

My use case is: in keygen.py, I want to use util/math_shortcuts.py.
So, in keygen.py, I have the following import: from paillier.util.math_shortcuts import generate_coprime, lcm, get_mu.
However, when I try to use my package (by doing from paillier.keygen import generate_keys), I am greeted with the Error ModuleNotFoundError: No module named 'paillier.util'

This ModuleNotFoundError is always present when I install it using pip from TestPyPi, but it doesn't happen when I build the package locally: when I run pip install -e . in the paillier/ directory (where setup.py lives), I can run from paillier.keygen import generate_keys, even when my working directory is somewhere else.

I've tried to do from .util.math_shortcuts ..., or from util.math_shortcuts ..., or from paillier.paillier.util.math_shortcuts ..., but all to no avail.


In short, when doing pip install --index-url <TestPyPi> rens-paillier my files can't seem to find the submodules.
However, when doing pip install -e . in the outer paillier/ dir, it seems to work.

like image 750
Rens Oliemans Avatar asked Apr 12 '26 08:04

Rens Oliemans


2 Answers

I ran into the same problem. (my -e installation worked, normal installation didn't) My solution is, to actually name the subpackages in the setup.py.

packages=['paillier', 'paillier.util']

While this works, I am not sure why :D

like image 65
Feelx234 Avatar answered Apr 14 '26 22:04

Feelx234


I found that removing the packages parameter from the setup function call in the setup.py file resolved a similar issue I had with my module not resolving submodules.

like image 24
FakeFootball Avatar answered Apr 14 '26 21:04

FakeFootball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!