Import custom modules in nengo_gui

I feel really silly but I can’t work out how to import my custom modules in Nengo GUI. When running my script from the terminal, from the tests folder, I usually do it as:

PYTHONPATH=… python main.py

My directory is organised as:

Project/
  library/
      file.py
  tests/
      main.py

And I import file.py into main.py as:

from library.file import *

I cannot work out how to do the same in Nengo GUI. I have tried adding

import sys
sys.path.append(‘…’)

to my script but to now avail.

As a quick check, if you run nengo from the Project folder like so:

cd Project
nengo tests/main.py

Does that work?

No, still the same ModuleNotFoundError .

Okay. So I tested this setup and it seems to work:

Folder structure:

Project/
    lib/
        __init__.py
        funcs.py
    tests/
        main.py

The contents of funcs.py:

import numpy as np
def sin_func(t):
    return np.sin(t * np.pi * 3)

The contents of main.py

import sys
import nengo

sys.path.append("../lib")

from funcs import sin_func

with nengo.Network() as model:
    input_node = nengo.Node(sin_func)

I think you might have been missing the __init__.py file in the library folder. Without that file, python imports will not work.

It still doesn’t seem to work in the GUI, even with the __init.py__ in there. It obviously works fine from the terminal or in my IDE but I just can’t seem to understand how to make it find the package in Nengo GUI.

The working directory for the NengoGUI is the directory in which you initiated the nengo command. So, in your example, the working directory is the <root> directory. Python is thus trying to look for the memristor_nengo package in <root>/.., which doesn’t exist.

For your use case, if you start Nengo from the <root> directory, you’ll want to do:

sys.path.append("memristor_nengo")

You can also use absolute paths (instead of relative paths) in sys.path.append to get the import to work from any directory.

Unfortunately, still no luck.
I’ve added the __init.py__ to the package, launched nengo from my root project folder, and tried to import with sys.path.append("memristor_nengo") from the main that is in root/tests.
I still get

Traceback (most recent call last):
File “/Users/thomastiotto/opt/anaconda3/envs/nengodl_opt/lib/python3.7/site-packages/nengo_gui/page.py”, line 229, in execute
exec(compiled, code_locals)
File “<nengo_gui_compiled>”, line 14, in
ModuleNotFoundError: No module named ‘memristor_nengo’

Ah. I see your issue here. You are importing your modules as

from memristor_nengo.extras import *
from memristor_nengo.learning_rules import mPES

And this means that python is trying to find the memristor_nengo module from the directories in your path. Since you did sys.path.append("memristor_nengo"), it’s trying to find the memristor_nengo module (or folder) from within ./memristor_nengo, which it would not be able to find.

Therefore, if you use from memristor_nengo... import ..., you’ll want to do sys.path.append(".") instead.

Or, if you want to do sys.path.append("memristor_nengo"), you’ll have to do from extras import * and from learning_rules import mPES.

It was so simple… It works now, thank you!