Import sub-folder modules dynamically



  • On 15/11/2017 at 08:07, xxxxxxxx wrote:

    Hi guys,

    I'm trying to import modules dynamically inside sub-folders of the plugin and can't seem to make it work. My current hierarchy looks as follow:

    /plugin
       |_plugin.pyp
       |_modules
       |  |_folder_A
       |    |   |
       |    |   |_file_A.py
       |    |
       |    |_folder_B
       |    |   |
       |    |   |_file_B.py
       |    |
       |    |_folder_C
       |        |
       |        |_file_C.py
       |
       |_res
            |_icons
                |_icon.tif
            |_c4d_symbols.h

    Would like to be able to access the file_A, file_B and file_C. Do you have to include an empty __init__ file inside each folder?

    Apologies if this has been replied before. Just can't seem to find an answer.

    Thank you very much in advance! 🙂

    Andre



  • On 15/11/2017 at 08:28, xxxxxxxx wrote:

    Yes do it like you will do for any python module, make an empty __init__.py
    And then after that you could use from .modules._folder_A._file_A import MyClassA

    Moreover I'm sure you already read it but I suggest you to read https://plugincafe.maxon.net/topic/8229/10727_best-practice-for-imports

    Here a exemple of my plugin RefractiveIndexImporter https://github.com/gr4ph0s/RefractiveIndex-Importer wich use Niklas py-localimport class in order to make safe import.

    Hope it's help



  • On 15/11/2017 at 08:34, xxxxxxxx wrote:

    Hi graphos,

    Many thanks for your quick and awesome reply!

    Will give it a try and come back with a conclusion.

    Thanks again! 🙂



  • On 15/11/2017 at 09:06, xxxxxxxx wrote:

    Graphos you're a star! Worked like a treat :).

    Thank you very much again and hopefully this will help others! 😄


Log in to reply