1

Is there a way to mix subpackages and functions in my Python packages?

Currently, my layout is roughly like this:

lib/ __init__.py Transform.py Statistic.py 

where Transform.py and Statistic.py contain several functions each. To use them I do something like

from lib import Transform Transform.fft(signal); 

Now I would like to be able to have a function in a package inside Transform:

from lib.Transform import bins Transform.bins.extent(signal); 

Is that even possible? How would I have to define my packages to do that?

1 Answer 1

1

Solution #1: Try the following layout:

lib/ __init__.py Statistic.py Transform __init__.py bins.py 

In this case Transform.fft goes inside lib/Transform/__init__.py and Transform.bins.extent inside lib/Transform/bins.py

Solution #2: If you wish to keep __init__.py short and clean, you can also create a separate Python-module (like fft.py) and import it in __init__.py as follows:

from fft import * 

In which case you can also use:

from lib.Transform import fft 
Sign up to request clarification or add additional context in comments.

2 Comments

Ah! I thought __init__.py was only for stuff like __all__ = [...].
Nope, it's for all code. However, as a convention, you may not want to put too much stuff inside __init__.py. See the edit for details.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.