1

I am creating my own python library: toolbox. In the end I want to be able to do: from toolbox.printing import warn.

My architecture looks like this:

repo
|--toolbox
|----__init__.py
|----printing
|------__init__.py
|------utils.py
|--demo.py
|--setup.py

Here is toolbox/printing/utils.py:

def warn(text: str):
    print(str)

I was able to run demo.py with this:

from toolbox.printing import warn
warn("This is a warning")

with toolbox/__init__.py:

from .printing import warn

and toolbox/printing/__init__.py

from .utils import warn

This works fine when executing python demo.py. However if in another project I do pip install git+myRepo.git, the import does not work. The error depended on what I put in the __init__.py files: ModuleNotFOundError, CircularImports, ... Here is my setup.py file:

import setuptools

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

setuptools.setup(
    name='toolbox',
    version='0.0.1',
    author='MyName',
    author_email='myEmail',
    description='Usefull functions for my personnal use',
    long_description=long_description,
    long_description_content_type="text/markdown",
    url='myRepo',
    project_urls = {
        "Bug Tracker": "myRepo"
    },
    license='MIT',
    packages=['toolbox'],
    install_requires=['requests',
                      'boto3',
                      'configparser'],
)

Could someone explain me:

  • What I should put in the __init__.py files so that it works?
  • What exactly is happening here? Is this a difference between modules and libraries?

Thanks!

See above for mor details.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Jechos
  • 21
  • 2
  • 1
    in `setup.py`'s `setuptools.setup()` the value for `packages` should be a list of all packages and sub-packages. So it should be `['toolbox', 'toolbox.printing']`. You might want to use `setuptools.find_packages()` instead of creating this list yourself. – sinoroc May 06 '23 at 08:35
  • Thanks a lot, that solved it! – Jechos Jun 15 '23 at 06:05

0 Answers0