0

Brand new to python and can't manage to get my import resolved when I debug my test within vscode.

Project structure:

a/b/c/d/service/functions/s3_proxy.py
a/b/c/d/service/functions/__init__.py

a/b/c/d/service/tests/test_s3_proxy.py
a/b/c/d/service/tests/__init__.py

test_s3_proxy.py

from functions import s3_proxy

I get an error that funcitons module isn't found.

I tried these things:

  • I tried putting an __init__.py in service
  • I tried from ..functions import s3_proxy
AfterWorkGuinness
  • 1,780
  • 4
  • 28
  • 47

3 Answers3

0

Relative imports doesn't work. You need to add the path a/b/c/d/service/functions to your python system path

import os
import sys
path_to_functions = os.path.join(os.path.dirname(__file__), os.pardir, 'functions')
sys.path.append(path_to_functions)
import s3_proxy

or replace path_to_functions with full literal path to the folder functions

path_to_functions = os.path.join('a', 'b', 'c', 'd', 'service', 'functions')

or (to get the from functions import)

path_to_functions = os.path.join(os.path.dirname(__file__), os.pardir)
sys.path.append(path_to_functions)
from functions import s3_proxy
niCk cAMel
  • 869
  • 1
  • 10
  • 26
0

You can add s3_proxy.py to your modules.

Here is an example snippet.

from functions import s3_proxy
from sys import modules
modules['s3_proxy'] = s3_proxy

From there you can use the s3_proxy module as needed.

tcglezen
  • 456
  • 1
  • 4
  • 12
-1

Use an absolute path, like this

from [drive letter]:/[example folder]/ functions import s3_proxy

Harez
  • 30
  • 5