This is a matter of naming, structuring and conventions.
I am developing a simple python package. in a directory "PKG" I have 3 files:
__init__.py:
# import the main module
import PKG.main
# in case for: from PKG import *
__all__ = [ "main" ]
main.py (simplified):
import PKG.exceptions
def f(x):
return(x+1)
exceptions.py:
class Error(Exception):
"""Base class for other exceptions."""
pass
class SomethingWentWrong(Error):
"""Raised when something went wrong."""
pass
Apart from that I have a directory "tests" with two files:
test_basic.py:
# import the testing framework
import pytest
# import the package
from context import PKG
def test_simple():
"""Tests if the package is functional."""
result = PKG.main.f(5)
assert result==6
context.py:
import os
import sys
sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), '..')))
import PKG
And now I realised that both the tests and the potential user have to know that the main module of my package is called "main". I would like to omit that step, so that the user can call: PKG.f(5)
. What places should I adjust? What if my project would grow and there would be more modules (that are all loaded from main.py)?