First published: 2020-02-27 Thu
Last updated: 2020-02-27 Thu
The NEURON installation adds python support using the
PYTHONPATH global environmental variable. I use virtual environments to separate my projects but the
PYTHONPATH NEURON was unfortunately adding itself to every environment I created. I want to develop projects and organize dependencies in an orderly way, so here we will remove this global environmental NEURON installation and create an isolated environment for it.
I am using the NEURON simulation environment with Python 3.7.6 and virtualenvwrapper + venv. I'll assume you know your way around a terminal and environmental variables, use virtualenvwrapper for python development, and have a working NEURON simulation environment installed on macOS (I'll do Windows in the future). If you need help setting those up, please refer to their respective pages.
Our goal is to isolate the NEURON python bindings so they are local to a virtual environment, rather than a global install. So in the end we will be able to access it from the environment but not from outside like this:
Allow the installer to add NEURON to your path and to the
PYTHONPATH. Once installation is complete, you should be able to start a python shell and import the major neuron module,
nick ~ $ python3 -c 'from neuron import h'
This should return you to your shell prompt without any issues. So if it exits and you don't see an
ImportError, then you are all set to continue.
Note- I do not use the
PYTHONPATH environmental variable so I will be setting it and unsetting it without any concerns here. If you do use it, you will have to make some changes below…
On macOS, the NEURON installer defines the
PYTHONPATH environmental variable in your
.bash_profile (if you don't have a
.bash_profile it will be created). Open your
.bash_profile in your favorite text editor. For the moment, comment the line starting with
export PYTHONPATH... out. Close your terminal and open a new one.
echo $PYTHONPATH should return nothing.
Now, make a new python3 environment for your NEURON project.
nick ~ $ mkvirtualenv nrn ... # stuff printed when making environment here. (nrn) nick ~ $
To ensure that you properly removed the PYTHONPATH before setting up this environment, you should now get an
ImportError if you try to import neuron
(nrn) nick ~ $ python3 -c "from neuron import h" Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'neuron'
Since we globally unset the
PYTHONPATH variable, this will also happen if you try it from outside your environment as we did in the previous step.
Virtualenvwrapper provides some utility/customization bash scripts that are run before/after activating or deactivating an environment. See the virtualenvwrapper docs for more information. We will use the
postdeactivate scripts (run just after activating and just after deactivating your environment, respectively) to set the
PYTHONPATH environmental variables in your isolated environment. These files are located in the
nrn with whatever you named your virtualenvironment) folder wherever your
WORKON_HOME is set to (see https://virtualenvwrapper.readthedocs.io/en/latest/install.html#variable-workon-home).
I don't use the
PYTHONPATH in any other context, so I will simply set it in the
postactivate script (lives in
#!/bin/bash # This hook is sourced after this virtualenv is activated. export PYTHONPATH="/Applications/NEURON-7.7/nrn/lib/python"
then unset it in the
postdeactivate script (lives in
#!/bin/bash # This hook is sourced after this virtualenv is deactivated. export PYTHONPATH=""
You will have to replace the exact value of
PYTHONPATH with whatever NEURON added to your
.bash_profile. Now open a fresh shell and ensure the following works:
nick ~ $ echo $PYTHONPATH nick ~ $ workon nrn (nrn) nick ~ $ echo $PYTHONPATH /Applications/NEURON-7.7/nrn/lib/python (nrn) nick ~ $ deactivate nick ~ $ echo $PYTHONPATH
And that's it! You should now be able to activate your environment and start playing with NEURON in an isolated and controlled environment!
(nrn) nick ~ $ python Python 3.7.6 (default, Dec 30 2019, 19:38:28) [Clang 11.0.0 (clang-1184.108.40.206)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from neuron import h >>> soma = h.Section(name='soma')
Whereas exiting your environment you won't be able to:
(nrn) nick ~ $ deactivate nick ~ $ python3 Python 3.7.6 (default, Dec 30 2019, 19:38:28) [Clang 11.0.0 (clang-1220.127.116.11)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from neuron import h Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'neuron' >>>
You won't be able to run
nrngui from your shell in the environment, Not sure why but something to be aware of.
I found a windows laptop so I will get this working there and post back soon (maybe).