7

OS: Windows 7

Python: 3.6

I'm trying to create and installing a python wheel package. The building works fine but when i import the module into project after installing it, i get a "ModuleNotFound" error. My project has the following structure:

my_lib/
    __init__.py
    phlayer/
        __init___.py
        uart.py
    utils/
        __init___.py
        ctimer.py 

My setup.py for creating the wheel package:

import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
    name="my_lib",
    version="0.0.1",
    author="",
    author_email="",
    description="",
    packages=setuptools.find_packages(),
    classifiers=(
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ),
)

In uart.py i do:

from utils import ctimer

After installing i import the package into another project:

#Test.py

from my_lib.phlayer.uart import Uart

def main(args=None):
    pass

if __name__ == "__main__":
    main()

And i get the error:

  File "C:/.../.../.../Test.py", line 9, in <module>
from my_lib.phlayer.uart import Uart
File "C:\...\...\...\...\...\...\test\env\lib\site-packages\my_lib\phlayer\uart.py", line 3, in <module>
from utils import ctimer
ModuleNotFoundError: No module named 'utils'

So it seems that python cannot find the correct module in the other package. Do i need to specify the correct paths in the setup.py before creating the wheel package?

NoNAmE
  • 73
  • 1
  • 4

1 Answers1

2

You have to specify full module names:

from my_lib.utils import ctimer
phd
  • 82,685
  • 13
  • 120
  • 165
  • Ok that worked. But now i have the problem that python cannot find the modules in the actual project. When i execute `my_lib\my_lib.py` (for testing) i get the error: `ModuleNotFoundError: No module named my_lib`. So why in this case python cannot find the module? – NoNAmE Jul 04 '18 at 06:23
  • What do you mean *execute my_lib\my_lib.py*? `python my_lib\my_lib.py`? Isee 2 problems here: 1. Wrong PYTHONPATH. 2. Script `my_lib.py` named the same as the package and it shadows the package (Python prepends its path to `sys.path`). – phd Jul 04 '18 at 11:57
  • No, the script is not named the same as the package (named it only for explanation so lets say the name is `my_lib/test.py`). The thing is when i run the script `test.py` i get the mentioned error when i use full module names. So do i have to add ...\...\...\my_lib' to PYTHONPATH if i use full package names (like: `from my_lib.utils import ctimer`)? Why python can find the module if i use `from utils import ctimer` when i run the project directly but cannot find the module if i creating and importing it as a wheel package? – NoNAmE Jul 10 '18 at 09:51
  • Because when you run `my_lib/test.py` python adds directory `my_lib/` to `sys.path` and `import utils` finds `utils` in `sys.path`. After installation `utils` is not in `sys.path` — but `my_lib` is. – phd Jul 10 '18 at 17:59