Importing neuron module into Python

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

roybens
Posts: 54
Joined: Fri Mar 14, 2008 7:57 am

Re: Importing neuron module into Python

Post by roybens »

Hi Michael,

Unfortunately this fix does not seem to work anymore, after restarting my computer. Even though I had incorporated the command I mentioned earlier into my .bashrc file before restarting....

Code: Select all

export DYLD_LIBRARY_PATH=/anaconda/lib/
... when I attempted to replicate what I did in my previous post, the shell simply shows a blank cursor and does not give me the prompt again (I need to "control+z" in order to escape):

Code: Select all

Last login: Sun Jan  4 19:25:38 on ttys000
kjb:lib ken$ echo $DYLD_LIBRARY_PATH
/anaconda/lib/
kjb:lib ken$ python
Python 2.7.8 |Anaconda 1.9.0 (x86_64)| (default, Jul  2 2014, 15:36:00) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> import neuron

Using LD_LIBRARY_PATH in addition, or following aaronmil's suggestion of coping libreadline.6.2.dylib from /anaconda/lib to /usr/lib, both did not alleviate this problem.

Thoughts? I'm quite confused now.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Importing neuron module into Python

Post by hines »

All I can say is that when I start out with a PATH that allows me to run NEURON and allows python to find the NEURON module

N=/Applications/NEURON-7.4
export PATH=$N/nrn/x86_64/bin:$N/iv/x86_64/bin:$PATH
export PYTHONPATH=/Applications/NEURON-7.4/nrn/lib/python

Then I can launch the mac default python and
$ python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
...
>>> from neuron import h
NEURON -- VERSION 7.4 (1202:c1d173cd50e0) c1d173cd50e0
...
>>>

and then when I switch to anaconda

Michaels-MacBook-Air:~ hines$ export PATH="/Users/hines/anaconda/bin:$PATH"
Michaels-MacBook-Air:~ hines$ export DYLD_LIBRARY_PATH=$HOME/anaconda/lib
Michaels-MacBook-Air:~ hines$ python
Python 2.7.9 |Anaconda 2.1.0 (x86_64)| (default, Dec 15 2014, 10:37:34)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>> from neuron import h
NEURON -- VERSION 7.4 (1202:c1d173cd50e0) c1d173cd50e0
Duke, Yale, and the BlueBrain Project -- Copyright 1984-2014
See http://www.neuron.yale.edu/neuron/credits

>>>
roybens
Posts: 54
Joined: Fri Mar 14, 2008 7:57 am

Re: Importing neuron module into Python

Post by roybens »

Okay, I have another clue.

I uninstalled then reinstalled NEURON from the source code, then set the environmental variables as you mentioned, and I was able to get everything working again. However, once I exited the terminal and tried to open it up again, I got the same issue with the interpreter hanging when I try to import neuron. So I think it's a problem with either my .bash_profile or my .bashrc file. They are as follows:

.bash_profile:

Code: Select all

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
# PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
# export PATH
N=/Applications/NEURON-7.3
export PYTHONPATH=$PYTHONPATH:/Library/Python/2.7/site-packages
export PYTHONPATH=$PYTHONPATH:$N/nrn/lib/python
# added by Anaconda 1.9.0 installer
export PATH="/anaconda/bin:$HOME/local/bin:$PATH"
# reference .bashrc
source ~/.bashrc
source ~/git-completion.bash
umask 2
.bashrc:

Code: Select all

alias ..='cd ..'
alias ls='ls -aF'

N=/Applications/NEURON-7.3
export DYLD_LIBRARY_PATH=/anaconda/lib/:$DYLD_LIBRARY_PATH
export PATH=$N/nrn/x86_64/bin:$N/iv/x86_64/bin:$PATH
When I first open up the terminal, my environmental variables are as follows:

Code: Select all

Last login: Mon Jan  5 19:06:27 on ttys000
kjb:~ ken$ echo $PATH
/Applications/NEURON-7.3/nrn/x86_64/bin:/Applications/NEURON-7.3/iv/x86_64/bin:/anaconda/bin:/Users/ken/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin
kjb:~ ken$ echo $PYTHONPATH
:/Library/Python/2.7/site-packages:/Applications/NEURON-7.3/nrn/lib/python
kjb:~ ken$ echo $DYLD_LIBRARY_PATH
/anaconda/lib/:
kjb:~ ken$ 
Does that seem right to you?
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Importing neuron module into Python

Post by hines »

That seems right to me. One difference is I am using the latest hg repository version for NEURON 7.4
i don't see anything wrong with your earlier configure, although I built using the default version of python and then switched to anaconda.
Does your version work with the default version of Python?
roybens
Posts: 54
Joined: Fri Mar 14, 2008 7:57 am

Re: Importing neuron module into Python

Post by roybens »

I think I got it, good call on anaconda.

So I tried what you suggested by moving the /anaconda/bin to the end of my PATH so I could run default Python, but this still left the hanging prompt. So I decided to uninstall anaconda (which apparently is as simple as rm -rf /anaconda according to its site), remove all of the anaconda references in the paths, restart the computer and try again on default Python and it worked. I was able to replicate it multiple times in the same session in the terminal, in multiple different sessions after restarting terminal, and after restarting my computer and trying again. So hopefully this works for good.

It seems that anaconda was the problem. I'm not sure why, but I have no problem just manually installing all of the pertinent scientific python libraries I may need.

Thanks again for your help! As a happy coincidence I've certainly learned a lot about how to work with the bash shell throughout this process :-)

Ken
wlkath
Posts: 18
Joined: Wed Jun 08, 2005 12:39 am
Location: Northwestern University
Contact:

Re: Importing neuron module into Python

Post by wlkath »

I recently got a new computer (Macbook Pro) and re-compiled NEURON with Anaconda python present. I ran into the problem of the missing libreadline.6.2.dylib when trying to "import neuron" in python, and came across this thread while searching for solutions to the issue. As previously reported,

> otool -L $HOME/anaconda/lib/python2.7/site-packages/neuron/hoc.so

lists, among other things as described in previous posts,

... libreadline.6.2.dylib (compatibility version 6.0.0, current version 6.2.0)

and in particular, I note that this library doesn't have a full path name. I then did (I don't recall where I found this online)

> install_name_tool -change libreadline.6.2.dylib $HOME/anaconda/lib/libreadline.6.2.dylib hoc.so

i.e., it fills in the full path name for this library in hoc.so. After doing this, python appears to import NEURON without any issues.

I don't know if this is an approved way of fixing things, but I thought I would post it here as an option for others.

- Bill
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Importing neuron module into Python

Post by hines »

Thanks Bill. That is very helpful though I'm sorry that these mysteries are left to the user and I don't know how to deal with them automatically.
Anaconda's otherwise need for a DYLD_LIBRARY_PATH instead of LD_LIBRARY_PATH still perplexes me.
peadargrant
Posts: 8
Joined: Mon May 21, 2012 10:12 am

Re: Importing neuron module into Python

Post by peadargrant »

Just to add: make sure that there is one, and one only, hoc.so in your default Python import path. I got an error relating to libreadline and it turned out that Python was pulling in a version that I had installed somewhere else on the filesystem and then forgotten about. Removing it solved the problem.
wlkath
Posts: 18
Joined: Wed Jun 08, 2005 12:39 am
Location: Northwestern University
Contact:

Re: Importing neuron module into Python

Post by wlkath »

I found the source for the install_name_tool suggestion, in case it helps someone sort this out further:

Ewen McNeill/ blog/ entry/ OS X dylib: care and feeding, http://ewen.mcneill.gen.nz/blog/entry/2 ... d-feeding/

Regards,

- Bill Kath
R_Jarvis

Re: Importing neuron module into Python

Post by R_Jarvis »

I am experiencing the same problem on Ubuntu. Initially this mpi+python installation worked but eventually something broke.

Code: Select all

nrniv -python
>>>import neuron 
succeeds.

However

Code: Select all

$python 
In [1]: import neuron
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-861d48cbe3e0> in <module>()
----> 1 import neuron

/usr/local/lib/python2.7/dist-packages/neuron/__init__.py in <module>()
    104 except:
    105   #Python3.1 extending needs to look into the module explicitly
--> 106   import neuron.hoc
    107 
    108 import nrn

ImportError: libnrnpython.so.0: cannot open shared object file: No such file or directory
Inspired by above suggestions I try:
~/local/lib/python2.7/site-packages/neuron$ ldd hoc.so

Code: Select all

        linux-vdso.so.1 =>  (0x00007ffd4c580000)
	libnrnpython.so.0 => not found
	libnrnoc.so.0 => not found
	liboc.so.0 => not found
	libnrniv.so.0 => not found
	libivoc.so.0 => not found
	libmemacs.so.0 => not found
	libmeschach.so.0 => not found
	libneuron_gnu.so.0 => not found
	libnrnmpi.so.0 => not found
	libscopmath.so.0 => not found
	libsparse13.so.0 => not found
	libsundials.so.0 => not found
	libIVhines.so.3 => /home/russell/neuron/iv/x86_64/lib/libIVhines.so.3 (0x00007f178d353000)
	libreadline.so.0 => not found
	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f178d04e000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f178cc89000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f178c982000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f178c76c000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f178d986000)
However so most of these dependencies belong to the actual nrn-7.3 installation (other than libreadline.so.0), so I don't understand why ldd can't see those dependencies. Any ideas would be much appreciated.

Thanks for help.
Russell.
hines
Site Admin
Posts: 1682
Joined: Wed May 18, 2005 3:32 pm

Re: Importing neuron module into Python

Post by hines »

On ubuntu, it should be sufficient to have a correct LD_LIBRARY_PATH.
What you might be missing is the path to where you installed NEURON. ie. check the --prefix=.. value you used for configure.
It is interesting that your hoc.so found the path to iv. Do you have that in your LD_LIBRARY_PATH? Maybe you need to also
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/russell/neuron/nrn/x86_64/lib

Unless you moved things away from their install location, things are supposed to work from the launch python direction
with merely a correct PYTHONPATH. All the details were supposed to be handled by the call to setup.py at the last step of
make install

Given your path to iv, I presume you installed from sources. If you continue to have trouble, what was your configure command?
R_Jarvis

Re: Importing neuron module into Python

Post by R_Jarvis »

On ubuntu, it should be sufficient to have a correct LD_LIBRARY_PATH.
What you might be missing is the path to where you installed NEURON. ie. check the --prefix=.. value you used for configure.
It is interesting that your hoc.so found the path to iv. Do you have that in your LD_LIBRARY_PATH? Maybe you need to also
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/russell/neuron/nrn/x86_64/lib

Unless you moved things away from their install location, things are supposed to work from the launch python direction
with merely a correct PYTHONPATH. All the details were supposed to be handled by the call to setup.py at the last step of
make install
I think I caused the problem by not cleaning up a previous similar installation directory and also by accidentally deleting something from my bashrc file like PYTHONPATH etc, however replacing the setting of PYTHONPATH appropriately in .bashrc did not work.
Given your path to iv, I presume you installed from sources. If you continue to have trouble, what was your configure command?
Yes I compiled from sources to get MPI+Python+NEURON all working togethor.

I tried experimenting with setting PYTHONPATH to match the configure --prefix. Actually in the last case I omitted prefix and just used the source code directory as an installation dir. I was also trying

Code: Select all

$python
>>>import sys
>>>sys.path()
To check that changes to PYTHONPATH were effective, I could see the neuron module was visible in dist-packages, but it was still not importable.

Adding the line:

Code: Select all

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$USER/nrn-7.3/x86_64/lib
to ~/.bashrc
worked, so thanks.

I will now set about deleting previous build directory and maybe compiling with --prefix /usr/local instead.
Post Reply