0

I want to have project structure as you can see below: some API module, which i use to make some tools.

├── api
│   ├── __init__.py
│   ├── some_script.py
├── tools
│   └── tool1.py

api/some_script.py

def func():
    print("Func1")

class some_object():
      ...

api/__init__.py

from .some_script import some_object, func

Where in tool1.py is

from ..api import func

I want that tools in folder, but I keep getting import error. To this point I had tool1.py in no folder and it worked fine (but with code from api import func)

ImportError: attempted relative import with no known parent package

How to make it work this way? Thanks for answers

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264

2 Answers2

0

I tried your code and got the same error, turns out you need to help the import using sys here is how to do it:

import sys 
sys.path.append("..")
from api import some_script
0

@basic beatle helped me solve it. With some other sources and this one works:

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))