Import python file.



  • Hello,

    Is it possible to load a python file in another file in Drawbot? Have been googling for pure python solutions, but I can't get it to work.

    Basically what I want:
    I have my main python code which I run to generate a movie. I want to load the 'settings' (just a bunch of variables I set in the beginning of the code) from another file. This way I can update the code and have the designer save different sets of settings and load these. What would be the way to go about this?

    I tried something like settings a function in the settings.py file and import that, but no luck.



  • hello @snaders,

    if your settings.py file lives next to your main script, you can import anything from it:

    # settings.py
    aVariable = 'hello'
    anotherVariable = 42
    
    # main.py
    from settings import *
    print(aVariable)
    print(anotherVariable)
    

    you can also store settings in plain text files or data formats, and read the data into your script. see for example the modules to read/write csv, json, plist, etc.

    hope this helps!



  • @gferreira Gustavo, is this a good way to build libraries of classes and functions, too? I'm thinking smaller-scale, for a set of my own sketches, not something totally built-out and documented.



  • @MauriceMeilleur this can be a start. but creating and using a library involves a bit more:

    relative imports installed module
    imported files must be in the same folder as the main script imported files can be elsewhere
    module is imported directly with a relative import module must be installed in Python (for example appending to sys.path or using a .pth file)
    module is available only for scripts in the same folder module is available for all Python scripts

    see How To Write Modules in Python 3 for a step-by-step guide about creating your own modules.

    good luck!



  • @gferreira Thanks for your quick reply. I actually was trying something like that, but couldn't get the variables out.

    It's working now, but I have to restart Drawbot everytime I want to load new settings. I can't just resave the settings file and rerun the script, it'll use the old variables.

    I think it sounds best to load something from an external JSON file. I will look into that.
    Basically I want the designers who use my code to keep a folder of different settings they can use for different outcomes. Now they save multiple versions of the code...



  • @snaders right, I forgot to mention: to update the contents of a module, you need to reload it:

    from importlib import reload
    import settings
    reload(settings)
    
    from settings import *
    print(aVariable)
    print(anotherVariable)
    

    cheers!



  • @gferreira Ah nice, that works.

    Just have 2 questions/wishes left. Tried googling, but these things look so confusing to me...

    I know have this:

    from importlib import reload
    
    import settings_new
    reload(settings_new)
    from settings_new import *
    

    Which works. Ideally I would let people set a variable (in this case) called settings_new at the beginning of the code and fill the import things in automatically. So they don't have to change this on 3 places.
    I could get the import and reload to work with a variable as the module-name, but not the from ... import * part. Can I do this?

    Also, can the module be in another folder? It's a subfolder of the root where the code is in. So not a complete other location.

    Thanks again!



  • I could get the 'import' and 'reload' to work with a variable as the module-name, but not the 'from ... import *' part. Can I do this?

    you can access all names inside a module using dot notation: myModule.aVariable

    Also, can the module be in another folder? It's a subfolder of the root where the code is in. So not a complete other location.

    yes, the module can be anywhere, and you can tell Python where to find it by adding its parent folder to sys.path:

    import sys
    sys.path.append('/path/to/folder/')
    

    but unless you are importing code, I think using a data format like json would be more appropriate.

    good luck!



  • @gferreira Thanks!



  • @gferreira said in Import python file.:

    but unless you are importing code

    Clear. I will look into JSON then. I only load some settings for size and speed and such. Thanks again.


Log in to reply