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' 
0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.