Jivan
Jivan

Reputation: 23038

Import specific part of a relative submodule in Python

With a project structure like the following:

myproject/
 |--- __init__.py
 |--- application.py
 |--- modules/
       |--- __init__.py
       |--- parser.py
 |--- utils/
       |-- __init__.py
       |-- helpers.py

In utils/helpers.py:

def find_stuff():
    return stuff

def help_me():
    return some_help

In modules/parser.py, I want to import find_stuff (and only that).

I've tried the following:

from ..utils.helpers import find_stuff

But...

ImportError: cannot import name 'find_stuff' from 'myproject.utils.helpers' (/Users/myself/myproject/utils/helpers.py)

What should be done here?

Notes:

Upvotes: 0

Views: 158

Answers (2)

Bloodmallet
Bloodmallet

Reputation: 424

Did you read this issue already? It depends on what you want to do. If you're writing something that is purely a module

from myproject.utils.helpers import find_stuff

should work.

Upvotes: 2

Hoog
Hoog

Reputation: 2298

Under utils you do not have an __init__.py file. I think you will need one and even an empty one will do the trick.

Upvotes: 3

Related Questions