Best Practice For Imports?



  • On 17/10/2014 at 10:27, xxxxxxxx wrote:

    Hi All,

    I am working in the Script Manager  executing code and I want to import a few supporting modules. What is the typical/best practice way for doing this? I have companion .py files in the same folder. Can I just use a standard import mymodule.py or do I have to deal with pathing?

    Thanks



  • On 17/10/2014 at 10:44, xxxxxxxx wrote:

    When importing modules/packages in a script or plugin that are not part of the global PYTHONPATH
    but are delivered with your plugin or script, you MUST ensure that you leave no trace of it after the
    import.

    There are several ways to do that. Just today I developed myself a small bootstrap code section that
    I paste in my plugins to import local modules and packages. You should het the newest version from here: https://gist.github.com/NiklasRosenstein/f5690d8f36bbdc8e5556

    with _localimport('res/modules') :
            import some_package
        assert 'some_package' not in sys.modules

    For your script: The module you want to import is most-likely located in the same directory
    as your script. If it is, use _localimport('') instead of _localimport('res/modules').

    You can find additional tips for Python development at my py4d-tips repository.

    edit (2014-10-17) : I've updated the code here to reflect the code in the Gist.
    edit (2015-04-22) : Relinked to new version of the _localdist class and updated text to reflect the changes



  • On 17/10/2014 at 11:25, xxxxxxxx wrote:

    Thanks for the reply and script. It looks a bit complicated but I'll try it out.

    Is there anyway to simply reference one of the other python scripts already loaded in the Script Manager? Say I have script_one.py loaded in the script manager and I want to import it into script_two.py which is also in the script manager?



  • On 17/10/2014 at 11:29, xxxxxxxx wrote:

    You do not necessarily have to understand the code, but it wouldn't hurt if you do.  It's coded very
    densely by intention to make it easier to copy & paste the code into plugins or scripts.

    Originally posted by xxxxxxxx

    Is there anyway to simply reference one of the other python scripts already loaded in the Script Manager? Say I have script_one.py loaded in the script manager and I want to import it into script_two.py which is also in the script manager?

    You must know where script_two.py is located. Assuming your scripts folders looks like this:

    > scripts/
    >     script_one.py
    >
    >     script_two.py

    You can use the above code in script_one.py and then do

    > with _localimport('') :
    >     import script_two



  • On 03/06/2015 at 18:17, xxxxxxxx wrote:

    What is the issue with using something like this?:

      
    import sys   
    base_path = os.path.dirname(__file__)   
    lib_path = os.path.join(os.path.dirname(__file__), 'lib')   
    sys.path.insert(0, lib_path)   
    try:   
        import my_lib   
    finally:   
        sys.path.pop(0)   
    

    @Niklas, I think you were the one I borrowed this from in the first place



  • On 03/06/2015 at 19:23, xxxxxxxx wrote:

    The issue is when another plugin does the same. Especially when they use "my_lib" in a different
    version or, even worse, are completely different Python modules. One of the plugins will get the
    wrong module.



  • On 04/06/2015 at 12:42, xxxxxxxx wrote:

    Gotcha. Looks like I'm going to need to go back and modify the import code on all of my plugins. I wish there was something that could be done on Maxon's end to make this import process a little less kludgy.


Log in to reply