Reuse code from helper class



  • On 11/08/2018 at 08:27, xxxxxxxx wrote:

    Hi

    I'm just learning python for Cinema 4d and have a really beginner question.

    How do I import a helper class from another file?

    ----- Helper.py ---------------

    class Helpers(object) :  
        def \__init\_\_(self, arg) :  
            super(Helpers, self).\__init\_\_()  
      
        @staticmethod  
        def myFunction() :  
            print("My helper function")
    

    ---- Main script.py -------------

    import c4d  
    import Helpers  
      
    def main() :  
        Helpers.myFunction()  
      
    if \__name\_\_=='\__main\_\_':  
        main()
    

    When I try this, I get an error "ImportError: No module named Helpers"

    What am I doing wrong?



  • On 13/08/2018 at 03:32, xxxxxxxx wrote:

    Hi Fnoller.

    First of all welcome in the plugin cafe community.

    As you may know or not, import function in python looks for folders which are in the python import path and checks if there is a file with this name. 
    By default plugins folder, or the folder where script are saved are not added to this list of paths.
    But there is a package folder where you should put all your python module if you want to have something global which will work for all plugins/scripts.
    Package folder can be found in c4d temp folder

    AppData\Roaming\MAXON\XX.XXX_XXXXX\library\python\packages\win64\yourModule or python file
    

    If you want something more local to your script I suggest you to read this topic Best practice for Imports?.

    I would like to point you, in order to make your post more easy to readable for future reader,s please make sure to use the [ code] [ /code] markdown.

    If you have any question please, let me know!

    Cheers,
    Maxime. :)



  • On 18/08/2018 at 21:22, xxxxxxxx wrote:

    Hi Maxime

    Thank you very much for the answer 🙂

    Fnoller


Log in to reply