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
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    External dependencies question

    Cinema 4D SDK
    python
    3
    3
    656
    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.
    • mfersaouiM
      mfersaoui
      last edited by

      Hello,

      I have a plugin that contain multiple subplugins (.pyp files) and want to know how to insert my lib modules to the system path and that affect all the plugin .pyp files. (I have already seen the post: External dependencies: The right way to do and I did not find an answer concerning this point )

      lib\n    module1.py, module2.py, module3.py,...
      res\n    <resource files>
      plugin_1.pyp
      plugin_2.pyp
      plugin_3.pyp
      ...
      

      Must I use the "sys.path.inser()" method on each of my subplugins?
      I noticed that the subplugins is loaded one by one and in alphabetical order. so, for a test I created a new file and I rename it "a.pyp" (to be loaded in the first) and then I put inside it the following code:

      import os
      import sys
        
      dirname = os.path.dirname(__file__)
      lib_path = os.path.join(dirname, 'lib')
      sys.path.insert(0, lib_path)
      

      This it works, and affect all the .pyp files of my plugin.

      Is there a right way to do that?

      Thanks.

      1 Reply Last reply Reply Quote 0
      • M
        mp5gosu
        last edited by

        Here you go: https://github.com/NiklasRosenstein/py-localimport
        That helper is also available as a minified version.

        1 Reply Last reply Reply Quote 0
        • M
          m_adam
          last edited by

          Hi @mfersaoui there is nothing wrong with the way you are doing, but I would like to point out that the sys.path is a global variable shared over the current python environment. Meaning that 3rd party will be able also to import your own module so it's recommended to have an ambiguous name (e.g. if your module1.py is called util.py) people will be able to do import util and this can be really misleading so it's preferred to have a unique name (maybe you can start with a prefix).

          Finally, the solution offered by @mp5gosu is the cleanest one as it will create a local import space, so your util.py will only be importable by your own pyp file that actually calls the local import so it's so far the cleanest way as you are not "polluting" the global sys.path.

          But which one is the best, only you can decide according to what you are aiming to produce.
          Cheers,
          Maxime.

          MAXON SDK Specialist

          Development Blog, MAXON Registered Developer

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