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
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.