Python ModuleNotFoundError for command line tools built with setup.py

31 Views Asked by At

I am trying to build a simple command line tool and package it with setup.py. Here's my directory structure.

├── s3_md5
│   ├── __init__.py
│   ├── cmd.py
│   └── src
│       ├── __init__.py
│       ├── cli.py
│       ├── logger.py
│       ├── s3_file.py
│       └── s3_md5.py
├── setup.py
└── test
    ├── __init__.py
    ├── conftest.py
    ├── test_calculate_range_bytes_from_part_number.py
    ├── test_get_file_size.py
    ├── test_get_range_bytes.py
    └── test_parse_file_md5.py

In setup.py

'''installer'''
from os import getenv

from setuptools import find_packages, setup

setup(
    name="s3-md5",
    description="Get fast md5 hash from an s3 file",
    version=getenv('VERSION', '1.0.0'),
    url="https://github.com/sakibstark11/s3-md5-python",
    author="Sakib Alam",
    author_email="[email protected]",
    license="MIT",
    install_requires=[
        "boto3==1.26.41",
        "boto3-stubs[s3]",
    ],
    extras_require={
        "develop": [
            "autopep8==2.0.1",
            "moto==4.0.12",
            "pytest==7.2.0",
            "pylint==3.1.0",
        ],
        "release": ["wheel==0.43.0"]
    },
    packages=find_packages(exclude=["test", "venv"]),
    python_requires=">=3.10.12",
    entry_points={
        'console_scripts': ['s3-md5=s3_md5.cmd:run'],
    }
)

And cmd.py

'''driver'''
from time import perf_counter

from boto3 import client
from src.cli import parse_args
from src.logger import logger
from src.s3_md5 import parse_file_md5


def run():
// some stuff with imports from src


if __name__ == "__main__":
    run()

When I run the cmd.py from the s3_md5 directory itself, everything is fine. But when I build and install it as a command line tool and try to run that, it throws

ModuleNotFoundError: No module named 'src'

I checked the lib folder and it does contain the src folder. Oddly enough when I use s3_md5.src.cli within cmd.py; the command line tool works but running the script from the directory doesn't really work as it references the package installed not the code itself which causes issues for development usage. I've tried reading everything I can about python module system but I can't wrap my head around to this. I suspect its to do with the PYTHONPATH not knowing where to look for src but I can be wrong. I tried using relative import which works for the command line tool but throws no known parent for directly running python cmd.py

0

There are 0 best solutions below