The following directory structure:
repo/
├─ third_party/
│ ├─ project/
│ │ ├─ src/
│ │ │ ├─ moduledir/
│ │ │ │ ├─ __init__.py
│ │ │ │ ├─ main.py
├─ pythonscript.py
In main.py there's a (dummy) function:
def get_version():
return "1.0"
Now, from pythonscript.py, how can I call this function get_version()?
Note: The whole directory structure third_party/project/src are not modules / packages, only moduledir contains an empty __init__
I tried this but it can't find get_version():
sys.path.append(os.path.join(os.path.dirname(__file__), 'third_party/project/src/moduledir'))
from main import get_version
The error message is:
ERROR: Error loading pythonscript at '/home/user/dev/Product/pythonscript.py': Unable to load pythonscript in /home/user/dev/Product/pythonscript.py
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/anaconda3/envs/meteomatics/lib/python3.11/imp.py", line 172, in load_source
module = _load(spec)
^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 721, in _load
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/user/dev/Product/pythonscript.py", line 13, in <module>
from main import get_version
ModuleNotFoundError: No module named 'main'
Maybe I should mention that I don't execute this script, it's part of a facility that executes this "plugin" file for me.
import sys; import osat the top) and function usage (e.g.print(get_version())at the bottom). Also, what version of Python are you using?sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "third_party", "project", "src", "moduledir"))?sys.path.append, I would suggest adding the module location toPYTHONPATHor use thepip install -eoption..tomlfile? The general answer to your problem is to make this package installable as described in that link.