0

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:

  • everything was working fine with the whole project's policy of absolute import, until I started using Pytest, and then all hell broke loose
  • no, I don't want to from ..utils import helpers and then use helpers.find_stuff in parser.py — I assume that Python's import system is well-thought enough so that we can precisely avoid that
  • in the error message, we can see that Python manages to find the correct file, however for some reason it just won't import the function/class/object name, despite it being present in the file
Jivan
  • 21,522
  • 15
  • 80
  • 131
  • I have duplicated the project from your question in PyCharm and it works for me. – quamrana Apr 08 '19 at 14:45
  • @quamrana yes actually it works — the real case is that I have circular dependencies, and Python is quite terrible at managing them... – Jivan Apr 08 '19 at 14:46
  • Well, you didn't specify that in your question. Don't have circular dependencies! – quamrana Apr 08 '19 at 14:48
  • Seems that Python is making them more of a problem that they actually should be... – Jivan Apr 08 '19 at 14:51

2 Answers2

3

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.

Hoog
  • 2,280
  • 1
  • 14
  • 20
2

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.

Bloodmallet
  • 414
  • 4
  • 9