0

I'm trying to import a function from a higher level directory. I believe converting my script to a python "package" is the best way to do this:

package/
    __init__.py  # contains add function
    subpackage/
       __init__.py
       my_script.py  # where I want to import add function

Let's say the function I want to import is simple:

def add(x, y):
    return x + y

I should be able to do something like this in my_script.py, per the python docs:

from package import add

print(add(5, 6))

But I'm getting a ModuleNotFoundError: No module named 'package' error. What am I doing wrong?

Johnny Metz
  • 5,977
  • 18
  • 82
  • 146

1 Answers1

-1

You should import your sub-package by referencing it from the main-package. See code below:

from package.subpackage import my_script

print(my_script.add(5,6))