Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search

Best Practice For Imports?

Author
Message
  Topic Search Topic Search
Dragon Seer View Drop Down
Member
Member
Avatar

Joined: 2010 Oct 28
Online Status: Offline
Posts: 21
Direct Link To This Post Topic: Best Practice For Imports?
    Posted: 2014 Oct 17 at 10:27am
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
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2014 Oct 17 at 10:44am
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


Edited by NiklasR - 2015 Apr 22 at 1:08am
Back to Top
Dragon Seer View Drop Down
Member
Member
Avatar

Joined: 2010 Oct 28
Online Status: Offline
Posts: 21
Direct Link To This Post Posted: 2014 Oct 17 at 11:25am
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?
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2014 Oct 17 at 11:29am
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 Dragon Seer

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



Edited by NiklasR - 2015 Apr 22 at 1:09am
Back to Top
Donovan Keith View Drop Down
Member
Member


Joined: 2002 Oct 30
Location: United States
Online Status: Offline
Posts: 199
Direct Link To This Post Posted: 2015 Jun 03 at 6:17pm
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

Edited by Donovan Keith - 2015 Jun 03 at 6:17pm
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2575
Direct Link To This Post Posted: 2015 Jun 03 at 7:23pm
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.


Edited by NiklasR - 2015 Jun 03 at 7:24pm
Back to Top
Donovan Keith View Drop Down
Member
Member


Joined: 2002 Oct 30
Location: United States
Online Status: Offline
Posts: 199
Direct Link To This Post Posted: 2015 Jun 04 at 12:42pm
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.
Back to Top
hfeist View Drop Down
Member
Member


Joined: 2018 Aug 14
Online Status: Offline
Posts: 1
Direct Link To This Post Posted: 2018 Aug 14 at 6:20pm

When I attempt to use the code

with _localimport(''):
    import script_two

an error occurs:
"name _localimport is not defined"

Perhaps there's a library I need to import beforehand.

(I realize this is a very old post but I'm desperate!)


Back to Top
MaximeA View Drop Down
Forum Moderator
Forum Moderator

Forum Moderator

Joined: 2018 Jan 04
Location: France - Nantes
Online Status: Offline
Posts: 119
Direct Link To This Post Posted: 2018 Aug 16 at 2:20am
Hi Hfeist!

First of all welcome in plugin cafe community!

localimport is a module made by Niklas. So you have to download it from github, then import it to your python script.
Niklas also provide a minified version which can be directly used more easily.
Note that you should use localimport instead of _localimport

And finally, if you post any code on the forum, please make sure to use [ code] [ /code] markdown.

Cheers,
Maxime
MAXON
SDK Specialist
Back to Top

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.094 seconds.