Sublime Forum

[Solved] How to import a package and call its global functions / to get their global variable values?

#1

How to import a package and call its global functions / to get their global variable values?

I am on the package Packages/SublimeDefaultSyntax/default_syntax.py and I want to call a global function on the package Packages/SyncedSideBar/SyncedSideBar.py to get its current state on the package SublimeDefaultSyntax. How to accomplish so?

I tried several things, but they all seen useless. I just want to know the contents within on a global variable at the other package. I am able to modify both packages and to write whatever I need to get it working.

On the other file SyncedSideBar.py I got a function like this:

def print_hello():

    print( 'print_hello' )

Currently doing:
from SyncedSideBar import print_hello

I got:

reloading plugin SublimeDefaultSyntax.default_syntax
Traceback (most recent call last):
  File "D:\User\Dropbox\Applications\SoftwareVersioning\SublimeText\sublime_plugin.py", line 76, in reload_plugin
    m = imp.reload(m)
  File "./python3.3/imp.py", line 276, in reload
  File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1022, in load_module
  File "<frozen importlib._bootstrap>", line 1003, in load_module
  File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 868, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "D:\User\Dropbox\Applications\SoftwareVersioning\SublimeText\Data\Packages\SublimeDefaultSyntax\default_syntax.py", line 5, in <module>
    from SyncedSideBar import print_hello
ImportError: cannot import name print_hello

Also doing:

import SyncedSideBar

...
    def is_visible(self):

        print_hello()
        return isNotSyncedSideBarEnabled

I got:

Traceback (most recent call last):
  File "D:\User\Dropbox\Applications\SoftwareVersioning\SublimeText\sublime_plugin.py", line 476, in is_visible_
    ret = self.is_visible()
  File "D:\User\Dropbox\Applications\SoftwareVersioning\SublimeText\Data\Packages\SublimeDefaultSyntax\default_syntax.py", line 38, in is_visible
    print_hello()
NameError: global name 'print_hello' is not defined
0 Likes

#2

The packages dir is added to Python’s sys.path. SyncedSideBar is the package name. You want the module named SyncedSideBar within that package (from SyncedSideBar.py), so you do import SyncedSideBar.SyncedSideBar, or from SyncedSideBar.SyncedSideBar import print_hello.

2 Likes

#3

Thanks @FichteFoll

0 Likes