Importing modules in embedded python

Question:

I’m trying to get module imports to work in embeddable python, but it doesn’t want to work

C:UserstestDesktopwinpypython-3.10.10-embed-win32>type run_scriptsscript.py
from module_test import test

print("Hello world!")
print(test())

C:UserstestDesktopwinpypython-3.10.10-embed-win32>type run_scriptsmodule_test.py
def test():
    return "Test!"

C:UserstestDesktopwinpypython-3.10.10-embed-win32>@python.exe run_scriptsscript.py
Traceback (most recent call last):
  File "C:UserstestDesktopwinpypython-3.10.10-embed-win32run_scriptsscript.py", line 1, in <module>
    from module_test import test
ModuleNotFoundError: No module named 'module_test'

Why is the module not being imported? I tried changing PYTHONPATH but it didn’t help

Asked By: Holux

||

Answers:

im not that good in python but this is should be make you understand :
The error message ModuleNotFoundError: No module named ‘module_test’ indicates that the Python interpreter is unable to find the module_test module. This can happen if the module is not located in the same directory as the script or if it is not in the Python path.

To resolve this issue, you can add the directory where the module is located to the Python path, for example:

import sys
sys.path.append(r"C:UserstestDesktopwinpypython-3.10.10-embed-win32run_scripts")

from module_test import test

print("Hello world!")
print(test())
Answered By: jeogo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.