0

I'm sure this has to be a duplicate, but I can't find the answer.

I have the following setup:

example
├── __init__.py
├── submod
│   ├── __init__.py
│   ├── subscript1.py
│   └── subscript2.py
└── toplevel.py

There is a function in subscript1 that I want to call in toplevel when I run from the command line by:

python toplevel.py

I can do the following and it works:

from submod import subscript1
subscript1.my_func()

but what I want to do is:

import submod
submod.subscript1.my_func()

And this is giving an error of:

AttributeError: module 'submod' has no attribute 'subscript1'
mitoRibo
  • 4,468
  • 1
  • 13
  • 22

1 Answers1

0

In submod\__init__.py (which is now empty, I assume), add the following line:

from . import subscript1

This makes the subscript module accessible directly in the submod module.

NB: The __init__.py in the root of your project is not necessary.

wovano
  • 4,543
  • 5
  • 22
  • 49