Module importing from scripts

So I am trying to import a method into a script from another file. It works, except when I refer to the magic Glyphs global. Running the script directly (and in it calling the method) works. Importing the method to another script fails.

With both files at the root of a folder in scripts:

test_module.py:

#MenuTitle: Test module
print(Glyphs.font)
def foo():
    print("foo()")
foo()

test_import.py:

#MenuTitle: Test importing
import test_module
print(Glyphs.font)
print(test_module.foo())

Running Test module (without active font):

Start
None
foo()
End

Running Test import (without active font):

Start
Traceback (most recent call last):
File “test_import.py”, line 2, in
import test_module
File “test_module.py”, line 3, in
print(Glyphs.font)
NameError: name ‘Glyphs’ is not defined
End

Am I missing something stupid here? Should this work or is this expected to fail?

Cheers,
Johannes

Add this line to test_module.py:

from GlyphsApp import Glyphs

You may also find yourself needing to import a few things from AppKit the same way.

1 Like

Is it possible to import modules from different folders?

That should follow the normal python rules. So I would check the python docs.