5

I do my first steps in python package distributions.
Unfortunately, I have ModuleNotFoundError after successful install from pip.

My dirs layout is pretty simple:

maindir
   |- setup.py
   |- pysoft
         |- __init__.py
         |- main.py
         |- pylib.py

main.py:

import pylib


def main():
    print("main program")
    pylib.libfunc()


if __name__ == '__main__':
    main()

pylib.py:

def libfunc():
    print("lib func")

setup.py:

import setuptools


setuptools.setup(
    name='pysoft',
    version='0.0.21',
    author='als',
    author_email='als@gnail.com',
    description='deploy tester',
    py_modules=['pylib'],
    packages=setuptools.find_packages(),
    python_requires='>=3.6',
    entry_points={
        'console_scripts': [
            'pysoft = pysoft.main:main',
        ],
    },
)

I do packaging and uploading to test.pypi.org:

python3 setup.py sdist bdist_wheel
python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*

I setup and start new virtualenv and install my package:

 python3 -m pip install -i https://test.pypi.org/simple/ pysoft

Then I try to run it, but got error:

pysoft 
Traceback (most recent call last):
  File "/home/fat/buff/tt/bin/pysoft", line 5, in <module>
    from pysoft.main import main
  File "/home/fat/buff/tt/lib/python3.6/site-packages/pysoft/main.py", line 1, in <module>
    import pylib
ModuleNotFoundError: No module named 'pylib'

Could you figure out where I have wrong step?

fat
  • 6,435
  • 5
  • 44
  • 70

1 Answers1

4

You do import pylib as if said pylib is a top-level module or package. But it's not — it's a submodule of the package pysoft. For the proper import do:

from pysoft import pylib

py_modules=['pylib'] in your setup.py is ignored because setuptools cannot find top-level pylib.py. But packages=setuptools.find_packages() works and include pysoft package into distributions.

phd
  • 82,685
  • 13
  • 120
  • 165
  • Yes, it works now, thank you. But if I try to run it locally (`python ./main.py`) I got error: `ModuleNotFoundError: No module named 'pysoft'` – fat Dec 12 '19 at 13:37
  • 1
    You need to adapt `$PYTHONPATH` when running locally. Your `main.py` doesn't know about `pysoft/` directory. The simplest fix is `PYTHONPATH=.. ./main.py` – phd Dec 12 '19 at 13:41