Python: How to import files as modules?
-
Hello everybody,
I'm new to this forum, so please bear with me in case my questions are hardly noob ish. I tried my best to consult the docs and the search first, but still I do have this question:
Right now, I'm working on a script that hit the 600 lines mark, so I thought it's a good idea to create different files and use import to use these files as modules. But somehow C4D python refuses to find the file and use these files as modules. As soon as I deactivate the VSC C4D bridge, the import works (at least this is what the VSC console is telling me).
I've set up an Environment Variable to my script repo with the hope this would fix it, but without any luck.Is this bad practice for a script? Shall I turn it into a plugin to have this as an option?
-
Hi @gaschka,
In your next postings please provide more detailed description of your question. In this case you're lacking information about the file structure you're using, the code that performs the import, the information on how and what have you set the environment variable for.
Regarding your question, when splitting python code into separate pieces one usually uses the concept of packages rather than raw files. Hence, your question relates to local modules you'd like to use in your script. Please, have a look at the Python Libraries Manual especially the last section about the Local Plugin Search Paths.
Another point of interest for you would be a utility function LocalImportPath that simplifies the process of importing packages.
If you have any further questions, please let me know and provide a code sketch that showcases your issue.
Cheers,
Ilia