Rens Oliemans
Rens Oliemans

Reputation: 106

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.

Upvotes: 3

Views: 4416

Answers (3)

FakeFootball
FakeFootball

Reputation: 477

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.

Upvotes: 1

Augustindou
Augustindou

Reputation: 1

looking around for the same question (credits to this post), the easiest is probably to use

setuptools.find_packages()

some optional arguments:

  • where="src" for package structures where the files are in src/
  • exclude=["*-old"] to exclude some packages (in my case, the my_package-old that I want to keep until my refactoring is done)

Upvotes: 0

Feelx234
Feelx234

Reputation: 330

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

Upvotes: 5

Related Questions