Problem with namespace packages: ModuleNotFoundError

Question:

I am using python3.7 and trying to get namespace packages to work, but having some module import issues. It is not clear to me how to fix this. I have followed the instructions given here (1st method): Packaging namespace packages

My project is organized as follows:

scripts/
  python_pkgs/
       a/
          __init__.py     <-- Contents of this file are name='a'
          a.py
          b.py
       utils/
          __init__.py     <-- Contents of this file are name='utils'
          util.py
  setup.py

Contents of setup.py are:

from setuptools import setup, find_namespace_packages

setup(
    packages=find_namespace_packages(include=['python_pkgs.*'])
)

Now a.py is an executable script. I do the following in there:

#/path/to/python3 -B

from b import foo
from python_pkgs.utils import util

The first import works OK because b.py is in the same folder as a.py. However, I am getting an error trying to import util.py:

        from pythong_pkgs.utils import util, misc
    ModuleNotFoundError: No module named 'pythong_pkgs'

What am I doing wrong?

Asked By: user4979733

||

Answers:

If pythong_pkgs should be package recognizable by find_packages then there should be __init__.py inside this directory (see documentation).

Second thing is that executable script should not be part of python package. It should be separated file

#/path/to/python3 -B

from python_pkgs.a.b import foo
from python_pkgs.utils import util

or

#/path/to/python3 -B

from python_pkgs.a.a import main

if __name__ == "__main__":
    main()

You can also call python module python -m python_pkgs.a.a

Answered By: Grzegorz Bokota
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.