ModuleNotFoundError, but the file is in the same folder
-
Hello, i created a python file named test.py in the Script Manager and trying to import it in a new python script, but i'm getting ModuleNotFoundError. What's wrong? - Both files are in the same location.
import test
Traceback (most recent call last): File "C:\Users\Sun\AppData\Roaming\MAXON\Cinema 4D R26_8BE0107C\library\scripts\untitled.py", line 3, in <module> import test ModuleNotFoundError: No module named 'test'
-
Hello @Gaal-Dornik,
Thank you for reaching out to us. Please include the version number you are developing for in the future as a tag in your questions.
This is happening because the script folders (and also plugin folders) are not on the module search path of the Python instance of Cinema 4D. When you run a vanilla CPython interpreter, the path where your script lies is set as the working directory; and the working directory is injected into the module search paths. In Cinema/c4dpy, the working directory is the application path.
In S26 and higher, you can either place shared components in one of the library folders or modify the search paths, both have been documented in the Python Libraries Manual. In 2024 you can also use mxutils.LocalImportPath which has been added for the exact scenario you are in.
import mxutils with LocalImportPath(__file__): import test
LocalImportPath
does more or less exactly what I described in the Python Libraries Manual under Local Plugin Search Paths.Cheers,
Ferdinand -
I see, thanks. But honestly i don't get it why the plugins folder isn't in the search path...