Failed to run neuron inside virtual environment in Ubuntu18 with Anaconda (PATH issue?)

Post Reply
zoccoler
Posts: 2
Joined: Wed Dec 18, 2019 2:26 pm

Failed to run neuron inside virtual environment in Ubuntu18 with Anaconda (PATH issue?)

Post by zoccoler »

Hi all,

I am trying to install and run neuron 7.5 inside a virtual environment in Ubuntu18.04.3. I am not an experienced Linux user. Anaconda is already installed with python3, which I use for other projects, and I want to run neuron with python2. So, the general idea here is to create a virtual environment with python2 using Anaconda (e.g.: 'conda create -n my_env python=2.7'), install neuron in it and then run it. Some solutions to related problems found in this forum involved removing anaconda, but I cannot do that since I use it in other projects. It seems like a PATH problem to me, but I’m not sure.
Please let me number what I have done so far to facilitate any answer.

1. Downloading .deb file (with Anaconda previously installed)

I have downloaded Neuron7.5.deb (https://neuron.yale.edu/ftp/neuron/versions/v7.5/) and installed it following your quickguide (https://www.neuron.yale.edu/ftp/neuron/ ... kstart.pdf). It seems it was installed, but I cannot run neuron (‘from neuron import h, gui') either inside ‘my_env’ (which has python2) or outside it (both situations return ‘No module named neuron’). The quickguide mentions "If this doesn’t work, it’s probably a PATH or PYTHONPATH issue.", but it doesn’t give any direction on how to solve it.
I have tried to add neuron to PATH and PYTHONPATH by editing the bashrc file (last lines shown below), re-openning the terminal and trying to run neuron again, but it still doesn’t work. Did I do it right?

Code: Select all

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/marcelo/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/marcelo/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/marcelo/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/marcelo/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
export PATH=$PATH:/usr/local/nrn/x86_64/bin
export PYTHONPATH=/home/usr/local/nrn/lib/python
2. Compiling neuron from source (without Anaconda installed)

I have also tried to compile neuron from source following these instructions (https://www.neuron.yale.edu/neuron/down ... pile_linux). This is the only situation where it actually works, but it runs with python3 and without any virtual environment. As I said, I would like to run it inside a virtual environment. If I install anaconda after this, create a virtual environment and try to run neuron inside it, it stops working again.

3. Compiling neuron from source (with Anaconda previously installed and added to path)

If Anaconda is already installed and added to PATH, I get many errors after ‘make –j’ for neuron (but nor for iv). Last part of the errors is shown below:

Code: Select all

libtool: link: g++ -pthread -g -O2 -o .libs/nrniv nvkludge.o ../ivoc/nrnmain.o ../ivoc/ivocmain.o ../oc/modlreg.o ../oc/ockludge.o   ../nrnoc/.libs/libnrnoc.so ./.libs/libnrniv.so ../ivoc/.libs/libivoc.so ../oc/.libs/liboc.so -lX11 ../nrnmpi/.libs/libnrnmpi.so ../gnu/.libs/libneuron_gnu.so ../scopmath/.libs/libscopmath.so ../sparse13/.libs/libsparse13.so ../sundials/.libs/libsundials.so ../memacs/.libs/libmemacs.so ../mesch/.libs/libmeschach.so /usr/local/iv/x86_64/lib/libIVhines.so ../nrnpython/.libs/libnrnpython.so -L/home/marcelo/anaconda3/envs/modals_env/lib -lpython2.7 -lpthread -lutil ../readline/.libs/libreadline.so -lncurses -ltinfo -lm -ldl -pthread -Wl,-rpath -Wl,/home/marcelo/neuron/nrn-7.7/x86_64/lib -Wl,-rpath -Wl,/usr/local/iv/x86_64/lib -Wl,-rpath -Wl,/home/marcelo/anaconda3/envs/modals_env/lib
/usr/bin/ld: ../sundials/.libs/libsundials.so: undefined reference to `ompi_mpi_op_sum'
/usr/bin/ld: ../sundials/.libs/libsundials.so: undefined reference to `ompi_mpi_op_min'
/usr/bin/ld: ../memacs/.libs/libmemacs.so: undefined reference to `tgoto@NCURSES6_TINFO_5.0.19991023'
/usr/bin/ld: ../sundials/.libs/libsundials.so: undefined reference to `ompi_mpi_op_max'
/usr/bin/ld: ../sundials/.libs/libsundials.so: undefined reference to `nrnmpi_comm'
/usr/bin/ld: ../sundials/.libs/libsundials.so: undefined reference to `ompi_mpi_double'
/usr/bin/ld: ../memacs/.libs/libmemacs.so: undefined reference to `tgetent@NCURSES6_TINFO_5.0.19991023'
/usr/bin/ld: ../sundials/.libs/libsundials.so: undefined reference to `ompi_mpi_long'
/usr/bin/ld: ../sundials/.libs/libsundials.so: undefined reference to `MPI_Comm_size'
/usr/bin/ld: ../sundials/.libs/libsundials.so: undefined reference to `MPI_Allreduce'
/usr/bin/ld: ../memacs/.libs/libmemacs.so: undefined reference to `tgetnum@NCURSES6_TINFO_5.0.19991023'
/usr/bin/ld: ../memacs/.libs/libmemacs.so: undefined reference to `tputs@NCURSES6_TINFO_5.0.19991023'
/usr/bin/ld: ../memacs/.libs/libmemacs.so: undefined reference to `tgetstr@NCURSES6_TINFO_5.0.19991023'
collect2: error: ld returned 1 exit status
make[3]: *** [Makefile:749: nrniv] Error 1
make[3]: Leaving directory '/home/marcelo/neuron/nrn-7.7/src/nrniv'
make[2]: *** [Makefile:520: all-recursive] Error 1
make[2]: Leaving directory '/home/marcelo/neuron/nrn-7.7/src'
make[1]: *** [Makefile:657: all-recursive] Error 1
make[1]: Leaving directory '/home/marcelo/neuron/nrn-7.7'
make: *** [Makefile:515: all] Error 2
4. Compiling neuron from source (without Anaconda installed, but using pyenv)

I have also tried to use pyenv to create virtual environments instead of anaconda. Neuron runs OK outside environment, but it doesn’t work when inside ‘my_env’, giving the same error (‘No module named neuron’).

5. Compiling neuron from source (without Anaconda installed, but using virtualenv)

Same situation as number 4.

What am I missing? Any solutions? By the way, in Windows, I was able to run it inside a virtual environment created with Anaconda Navigator. Thanks in advance for your attention.
ramcdougal
Posts: 267
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Failed to run neuron inside virtual environment in Ubuntu18 with Anaconda (PATH issue?)

Post by ramcdougal »

Do you have root access?

Assuming so, when you installed with dpkg, I believe you meant for PYTHONPATH to start with /usr/ not with /home/usr/

That worked for me in Ubuntu 19.04:

Code: Select all

(base) robert@robert-VirtualBox:~/anaconda3/envs/my_env$ cd
(base) robert@robert-VirtualBox:~$ conda activate my_env
(my_env) robert@robert-VirtualBox:~$ python
Python 2.7.17 |Anaconda, Inc.| (default, Oct 21 2019, 19:04:46) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from neuron import h
NEURON -- VERSION 7.5 master (6b4c19f) 2017-09-25
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2016
See http://neuron.yale.edu/neuron/credits

>>> 
More specifically, here's what I did to get that to work:

I installed (user-level, not as root) Anaconda 2019.10 with Python 3.7, then did the "conda create -n my_env python=2.7"

I needed to "sudo apt install libtinfo5" before proceeding.

With this, I was able to "sudo dpkg -i nrn-7.5.x86_64.deb"

I added the following two lines to the end of my ~/.bashrc:

Code: Select all

export PYTHONPATH=$PYTHONPATH:/usr/local/nrn/lib/python/
export PATH=$PATH:/usr/local/nrn/x86_64/bin
I opened a new terminal (necessary, because otherwise the changes won't take effect), and then everything worked as above.

A few important notes:
  • This puts NEURON 7.5 in all of your environments
  • 7.5 is a couple years old. In particular, NEURON 7.5 came out before Python 3.7, so the deb installer version will not work with Python 3.7, but you can compile it yourself if you want 3.7 support.
  • Python 2.7 is at end-of-life.
  • For any new development, I strongly suggest using the latest version of NEURON (currently 7.7.2) and a recent Python (currently either 3.7 or 3.8, and I only include 3.7 because that's the latest Anaconda version).
zoccoler
Posts: 2
Joined: Wed Dec 18, 2019 2:26 pm

Re: Failed to run neuron inside virtual environment in Ubuntu18 with Anaconda (PATH issue?)

Post by zoccoler »

I followed your steps and it worked! Thank you so much for such a clear explanation!

For the record, I had done 2 things differently that probably were causing the error: first I hadn't typed the PYTHONPATH correctly in my bashrc file, and second, I had installed neuron through the GUI ("Open With Software Installer"). One of them or both were making neuron module not to be found inside my environment.
  • 7.5 is a couple years old. In particular, NEURON 7.5 came out before Python 3.7, so the deb installer version will not work with Python 3.7, but you can compile it yourself if you want 3.7 support.
  • Python 2.7 is at end-of-life.
  • For any new development, I strongly suggest using the latest version of NEURON (currently 7.7.2) and a recent Python (currently either 3.7 or 3.8, and I only include 3.7 because that's the latest Anaconda version).
Thanks for the headsup, for now I need these versions to run a piece of code that requires them, but I'll try to update it soon.
nicholasmg
Posts: 7
Joined: Sat Feb 29, 2020 11:16 am
Location: University of Colorado

Re: Failed to run neuron inside virtual environment in Ubuntu18 with Anaconda (PATH issue?)

Post by nicholasmg »

I just figured this out for myself, if you want to use NEURON from python and you do not want PYTHONPATH set globally (contaminating all your environments) you can remove the PYTHONPATH declaration from your .bashrc and set it automatically within your virtual environment upon activation and un-set it upon deactivation. I wrote a quick post on how to do this using python3.7 and virtualenvwrapper on macOS (https://nickgeorge.net/programming/NEUR ... ironments/).

It looks like you can do something very similar in conda environments as well https://docs.conda.io/projects/conda/en ... -variables

I used the installer for NEURON and did not compile from source. Also, I cannot run the NEURON GUI from within the environment (nrngui or nrniv), but I can script and run all the NEURON functions from within python with this setup

Code: Select all

python -c "from neuron import h"
runs without a problem.
Post Reply