1

I wrote a package with several modules

pkg/
├── __init__.py
├── mod.py
├── mod_calc.py
└── mod_plotting.py

mod.py uses functions from mod_calc.py and mod_plotting.py, so in mod.py I use:

# mod.py:
import pkg.mod_calc as MC
import pkg.mod_plotting as MP
MC.calculate_this()
MP.plot_this()

I reckon this is ok.

There are also scripts (jupyter notebooks) with the suggested workflow designed for users with very little python knowledge, and I'd like them to use the functions as calculate_this() (defined in mod_calc.py) etc (as oppose to mod_calc.calculate_this() etc)

So here is what I'm currently doing:

# __init__.py:
from pkg.mod import *
from pkg.mod_calc import * 
from pkg.mod_plotting import *
# script:
from pkg import *
do_this() # from mod.py
calculate_this() # from mod_calc.py
plot_this() # from mod_plotting.py

This way the user doesn't need to worry about which function is defined in which module. It works fine, but I understand that from ... import * is not best practice. So what is the pythonic way to do this?

konstanze
  • 511
  • 3
  • 12
  • 2
    You sort of answered your own question. Using wild card imports are allowed but should be avoided. You might want to check these for example why it's a bad practice: 1) https://stackoverflow.com/questions/3615125/should-wildcard-import-be-avoided 2) https://stackoverflow.com/questions/2386714/why-is-import-bad – Tzane Jan 16 '23 at 09:11
  • This post https://stackoverflow.com/questions/7336802/how-to-avoid-circular-imports-in-python by brendan ended up being extremely helpful! – konstanze Feb 13 '23 at 10:38

0 Answers0