1

I've the following hierarchy:

.root
  /program/main.py
  /functions/myfunctions.py

And using my main.py I want to use the functions present in myfunctions.py script. For that I've defined my PYTHONPATH AS ./root/functions and I have this as the import on my script:

from functions import myfunctions as func

But I'm getting this error:

ModuleNotFoundError: No module named 'functions'

How can I solve this?

Thanks

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Pedro Alves
  • 1,004
  • 1
  • 21
  • 47
  • I think it's a problem with the working directory. This post [here](https://stackoverflow.com/a/4383597/16672014) gives some answers on how to solve that issue. – Paschalis Ag Dec 20 '22 at 15:42

2 Answers2

1

You defined your PYTHONPATH as ./root/functions, so everything(modules/packages) "inside" that directory is recognizable by Python. ./root/functions directory is gonna get inserted to the sys.path. These paths are where Python loader checks to find modules and packages.

Just import the myfunctions.py:

import myfunctions as func

If you had defined PYTHONPATH as ./root, then you would have access to functions directory. (It became your namespace package for more information)

S.B
  • 13,077
  • 10
  • 22
  • 49
-1

Import builtin module pathlib Add the following code in your myfunctions.py

from pathlib import Path
Path(__file__).resolve().parent.parent