0

Need some help understanding importing packages in python 3. I have subdirectory with a module 'connection.py'. I need to access variables from parent directory module 'credentials.py'. How would I refer this correctly? I have tried as from benchchart.credentials import client_id, client_secret, redirect_uri, access_code, access_token, refresh_token which didn't quite work. This gives me an error 'ModuleNotFoundError: No module named 'benchchart''

Screenshot for folder structure is attached

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Nesh
  • 3
  • 1
  • Does this answer your question? [beyond top level package error in relative import](https://stackoverflow.com/questions/30669474/beyond-top-level-package-error-in-relative-import) – timbmg Apr 15 '20 at 08:22

1 Answers1

0

When you try to import without a directory context, e.g.

from x import y

Python will look in the installed libraries directory site-packages.

To import local modules you need to specify it with a . for current directory or .. for one directory above, e.g. because credentials.py is one directory up from connections.py you need to go up one level with ..:

from ..credentials import client_id

And if you want to import from the credentials.py that is in the same directory, you specify it with .:

from .credentials import client_id
Teemu
  • 286
  • 1
  • 2
  • 10