0

I'm having difficultly with important a python module from another folder. Here's how my folder looks currently

foldername/
    __init__.py
    A/
        __init__.py
        spam.py
        grok.py
    B/
        __init__.py
        foo.py

I'm trying to import the functions and classes from the grok.py file into the foo.py in B. This is how my foo.py looks like

from ..A.spam import func 

However, I get the following error:

ValueError: attempted relative import beyond top-level package

Could somebody help me? I don't understand where I'm going wrong

alpha787
  • 171
  • 4
  • 10
  • 1
    Possible duplicate of: https://stackoverflow.com/questions/1918539/can-anyone-explain-pythons-relative-imports – Austin A Oct 18 '17 at 17:17

2 Answers2

0

You can't use '..' like you do on the command line. You have to add your 'A' folder to your Python path. You can use sys.path.append('/dir/of/A') and then from A.spam import func

bikemule
  • 316
  • 1
  • 9
0

Instead of using sys.path you could also add a *.pth-file to your python or anaconda "site-packages"-folder which contains the path to the folder "A". Import via from A.spam import func as @bikemule already proposed.

kai
  • 33
  • 7