-1

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.

7
  • 2
    Works fine for me. What error do you get? Please post it in full. And for good measure, please post the complete code of pythonscript.py, i.e. imports (import sys; import os at the top) and function usage (e.g. print(get_version()) at the bottom). Also, what version of Python are you using? Commented Nov 2, 2024 at 16:02
  • 1
    How do you call your script (and where from)? What's the error? OS, Python versions? What about sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "third_party", "project", "src", "moduledir"))? Commented Nov 2, 2024 at 16:03
  • Instead of sys.path.append, I would suggest adding the module location to PYTHONPATH or use the pip install -e option. Commented Nov 2, 2024 at 16:05
  • This is a standard directory structure for a python install as described in the python packaging tutorial. Do you have a .toml file? The general answer to your problem is to make this package installable as described in that link. Commented Nov 2, 2024 at 21:14
  • AMENDED Reading that tutorial, it has you upload your project to pypi which I find to be generally ill-advised for personal scripts and downright bad for modules created in a corporate / professional / academic setting unless you really intend to publish to the world. But its a good starting point. Commented Nov 2, 2024 at 21:27

2 Answers 2

0

In pythonscript.py:

from third_party.project.src.moduledir.main import get_version
get_version()
Sign up to request clarification or add additional context in comments.

1 Comment

OP said "The whole directory structure third_party/project/src are not modules / packages", so I don't think this is what they want, though it does work, at least for me.
0

I tried some stuff and was surprised this worked:

sys.path.append(os.path.join(os.path.dirname(__file__), 'third_party/project/src'))
from moduledir.main import get_version

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.