Importing modules in embedded python

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

C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\script.py
from module_test import test

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

C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\module_test.py
def test():
    return "Test!"

C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>@python.exe run_scripts\script.py
Traceback (most recent call last):
  File "C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32\run_scripts\script.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

>Solution :

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:\Users\test\Desktop\winpy\python-3.10.10-embed-win32\run_scripts")

from module_test import test

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

Leave a Reply