Import variable from plugin a to plugin b

Please post any questions about developing your plugin here. Please use the search function before posting!
BrunoG
Junior Member
Posts: 6
Joined: Sat Nov 09, 2013 8:28 pm

Import variable from plugin a to plugin b

Postby BrunoG » Wed Feb 05, 2014 6:16 am

Hello,

So i'm trying to import a variable from plugin a to plugin b, but i'm not being able to understand where the error is because the plugin2 just says it can't be loaded.

Code

Syntax: Select all

#  cstrike/addons/source-python/plugins/plugin1/plugin1.py

variable = ["foo"]


Syntax: Select all

# cstrike/addons/source-python/plugins/plugin2/plugin2.py

from plugin1 import variable


Code: Select all

#  cstrike/cfg/autoexec.cfg

sp load plugin1

sp load plugin2


Is this even possible with SP? I've done something similar with ES and it works. It's probably a namespace issue, but i can't seem to figure it out.

Thanks!

Edit:

I think i "figured it out", i've created at __init__.py with the necessary variables and it allows me to import it correctly.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Feb 05, 2014 6:38 am

Actually, just as the other recent thread about imports, you need to use the correct name. Notice that your plugin1.py is inside the plugin1 folder. Therefor, the import path for that module is plugin1.plugin1. So, if you want to get a variable from a different script, you could just use:

Syntax: Select all

from plugin1.plugin1 import variable

Also, you could use the SPPluginManager's get_plugin_instance method to retrieve plugin1's globals:

Syntax: Select all

from _core.command import SPPluginManager

test = SPPluginManager.get_plugin_instance('plugin1')
variable = test.globals['variable']

The SPPluginManager object will be moved (or at least imported) somewhere that isn't private fairly soon. There are still some changes planned for the PluginManager system. I will also look into using __getattr__ to retrieve values from <plugin>.globals automatically, so that it will more resemble the following in the future:

Syntax: Select all

variable = test.variable


Satoon
BrunoG
Junior Member
Posts: 6
Joined: Sat Nov 09, 2013 8:28 pm

Postby BrunoG » Wed Feb 05, 2014 6:44 am

Using the SPPluginManager works.

Thanks for the quick reply!
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Wed Dec 03, 2014 2:10 am

satoon101 wrote:

Syntax: Select all

from plugin1.plugin1 import variable

this does not work


satoon101 wrote:

Syntax: Select all

from _core.command import SPPluginManager

test = SPPluginManager.get_plugin_instance('plugin1')
variable = test.globals['variable']


don't work either (probably b/c i have newer version)

test.py

Syntax: Select all

x = 30


test2.py

Syntax: Select all

from test.test import x
print(x)


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '../addons/source-python/packages/source-python/plugins/manager.py', line 72, in __missing__
    instance = self.instance(plugin_name, self.base_import)
  File '../addons/source-python/packages/source-python/plugins/instance.py', line 82, in __init__
    self._plugin = import_module(import_name)
  File '../addons/source-python/plugins/test2/test2.py', line 12, in <module>
    from test.test import x

ImportError: cannot import name 'x'


test3.py

Syntax: Select all

from _core.command import SPPluginManager
test = plugins.manager.get_plugin_instance('test')
x = test.globals['x']


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '../addons/source-python/packages/source-python/plugins/manager.py', line 72, in __missing__
    instance = self.instance(plugin_name, self.base_import)
  File '../addons/source-python/packages/source-python/plugins/instance.py', line 82, in __init__
    self._plugin = import_module(import_name)
  File '../addons/source-python/plugins/test3/test3.py', line 12, in <module>
    from _core.command import SPPluginManager

ImportError: No module named '_core'



tried

Syntax: Select all

import plugins.manager

test = plugins.manager.get_plugin_instance('test')
x = test.globals['x']

print(x)


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '../addons/source-python/packages/source-python/plugins/manager.py', line 72, in __missing__
    instance = self.instance(plugin_name, self.base_import)
  File '../addons/source-python/packages/source-python/plugins/instance.py', line 82, in __init__
    self._plugin = import_module(import_name)
  File '../addons/source-python/plugins/test4/test4.py', line 14, in <module>
    test = plugins.manager.get_plugin_instance('test')

AttributeError: 'module' object has no attribute 'get_plugin_instance'
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Wed Dec 03, 2014 2:24 am

_core.command.SPPluginManager has been moved.

Syntax: Select all

from core.manager import core_plugin_manager

test = core_plugin_manager.get_plugin_instance('test')
x = test.globals['x']
Works as soon as "test" was loaded with "sp load test" prior test2.

Syntax: Select all

from test.test import x

print(x)
Also works.
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Wed Dec 03, 2014 2:57 am

ok this is very weird

Code: Select all

sp load test
[SP] Loading plugin 'test'...
[SP] Successfully loaded plugin 'test'.
sp load test2
30
[SP] Loading plugin 'test2'...
[SP] Successfully loaded plugin 'test2'.
sp unload test
[SP] Unloading plugin 'test'...
[SP] Successfully unloaded plugin 'test'.
sp unload test2
[SP] Unloading plugin 'test2'...
[SP] Successfully unloaded plugin 'test2'.
sp load test2
30
[SP] Loading plugin 'test2'...
[SP] Successfully loaded plugin 'test2'.


30 still gets printed when test is unloaded
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Dec 03, 2014 3:51 am

That isn't weird at all. Try loading test2 prior to ever loading test and you will get the same result. The import still exists, therefor you can still import the value. This is why you should check if test is loaded prior to importing from it. To do this, you can use either of the following:

Syntax: Select all

from core.manager import core_plugin_manager

if not core_plugin_manager.is_loaded('test'):
raise ImportError('Plugin "test" is not loaded.')

from test.test import x


Syntax: Select all

from core.manager import core_plugin_manager

if core_plugin_manager.get_plugin_instance('test') is None:
raise ImportError('Plugin "test" is not loaded.')

from test.test import x
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 24 guests