Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Python: How to import files as modules?

    Cinema 4D SDK
    python 2024
    2
    2
    402
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • gaschkaG
      gaschka
      last edited by

      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?

      i_mazlovI 1 Reply Last reply Reply Quote 0
      • i_mazlovI
        i_mazlov @gaschka
        last edited by

        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

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • First post
          Last post