I have a kind of plugin system, with this layout:
- Python
- SDK
- Plugins
- Plugin1
- Plugin2
- Plugins
- SDK
All 3 have a __init__.py
file. I wonder if is possible to be ab开发者_如何学运维le to do import SDK
from any plugin (as if SDK
was in the site-packages folder).
I'm in a situation where need to deploy, update, delete, add or change SDK
files or any of the plugins under non-admin accounts, and wonder if I can get SDK
in a clean way (I could sys.path.append
in all plugins but I wonder if exist a better option).
I imagine that using this in the Plugins init coulkd work:
import sys
import os
ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__),'..'))
print ROOT_DIR
sys.path.append( ROOT_DIR )
But clearly is not executed this code (I imagine __init__.py
was auto-magicalled executed in the load of the module ☹)
Python
start.py
from SDK.Plugins import Plugin1 print Plugin1.test()
SDK
__init__.py
Plugins
__init__.py
Plugin1.py
from SDK.Plugins import Plugin2 def test(): return Plugin2.test2()
Plugin2.py
def test2(): return "This worked!"
# python start.py
This worked!
This will work because in Plugin1.py
you are doing an import relative to start.py
, the executed script, not to itself.
If you were to execute directly Plugin1.py
, you'd have to mess with the path, but if it's always going to be used from a script higher on the folder hierarchy, then this is the cleanest way to do it.
精彩评论