Error on import caffe - python

For DeepDream or other deeplearning project, constructing environment of Caffe.
I installed required packages for PyCaffe and made PYTHONPATH to caffe/python.
However when I import caffe on python:
import caffe
Error occurred as below.How to solve this problem?
Segmentation fault: 11

Are you using a mac? I had a very hard time making pycaffe on mac, until I realized that there is a native python installed on all macs and that I was using another version I had installed. While compiling, caffe was using some of the stuff from the native python, and some of the stuff from the other python. I had to make sure to change all the relevant paths in the makefile.config file and to change the python my bash was using. I recommend working in a virtual environment as well. This is a good link to help you out, good luck!

This has been discussed since 2015 in Github issue.
Mostly the reason is the conflict of homebrew python and OS X system python.
Homebrew has provided an awesome solution for OS X:
$ python -V # system Python interpreter
$ python2 -V # Homebrew installed Python 2 interpreter
$ python3 -V # Homebrew installed Python 3 interpreter (if installed)
So the solution is changing all the python paths to python2. Bellowing is my Makefile.config related:
# ...
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
# /usr/lib/python2.7/dist-packages/numpy/core/include
# ------ For Homebrew installed python. Numpy path is added using python commands.
PYTHON_INCLUDE := /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/include/python2.7
# We need to be able to find libpythonX.X.so or .dylib. ------ (Update Homebrew path)
# PYTHON_LIB := /usr/lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib
PYTHON_LIB := /usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib
# Homebrew installs numpy in a non standard path (keg only) ------ (python2 for brew instead of python for system)
PYTHON_INCLUDE += $(dir $(shell python2 -c 'import numpy.core; print(numpy.core.__file__)'))/include
PYTHON_LIB += $(shell brew --prefix numpy)/lib
# ...

Try to manually set python path in your python script if you get no module named caffe error
Ex.
import sys
sys.path.insert(0, "/home/nviso/GitHub/caffe/distribute/python")
import caffe
This generally works for me.
Adding the caffe or python path to .bashrc manually should probably solve the issue as well though not sure, don't have my Office PC now to try :)

Related

Change 'python' to point to python2.7 inside virtualenv

I'm trying to bazel build a target inside a virtualenv, and I'm coming across the following error:
Use --sandbox_debug to see verbose messages from the sandbox
Error occurred while attempting to use the default Python toolchain (#rules_python//python:autodetecting_toolchain).
According to '/home/vagrant/venv/bin/python -V', version is 'Python 3.7.5', but we need version 2. PATH is:
/home/vagrant/venv/bin:/home/vagrant/.local/bin:/home/vagrant/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/vagrant/bin
Please ensure an interpreter with version 2 is available on this platform as 'python2' or 'python', or else register an appropriate Python toolchain as per the documentation for py_runtime_pair (https://github.com/bazelbuild/rules_python/blob/master/docs/python.md#py_runtime_pair).
Note that prior to Bazel 0.27, there was no check to ensure that the interpreter's version matched the version declared by the target (#4815). If your build worked prior to Bazel 0.27, and you're sure your targets do not require Python 2, you can opt out of this version check by using the non-strict autodetecting toolchain instead of the standard autodetecting toolchain. This can be done by passing the flag `--extra_toolchains=#rules_python//python:autodetecting_toolchain_nonstrict` on the command line or adding it to your bazelrc.
I tried the --extra_toolchains=#rules_python//python:autodetecting_toolchain_nonstrict flag, but the build still failed after, so I thought I'd try building with python2 involved.
Current state of my system:
Inside virtualenv:
python and python3 seems to resolve to version 3.7.5
python2 doesn't resolve to anything
python2.7 resolves to python 2.7.17
Outside virtualenv:
python resolves to 2.7.17
python3 resolves to 3.7.5
python2 doesn't resolve to anything
python2.7 resolves to python 2.7.17
I want either python or python2 in my virtualenv to resolve to 2.7.17 as well. (I think maybe python2 is safer? I worry changing python can have a lot of other side-effects). For changing python I tried the "sudo update-alternatives" command several times, but it seems to already be pointing to the right thing. It seems like virtualenv just isn't picking it up. Any help would be much appreciated.
Thanks!

Issue with two versions of python

Problem having two versions of python on my Mac and them interacting. I have the following python locations
python is /anaconda3/envs/fenicsproject/bin/python
python is /usr/bin/python
and when I try to run a script. I get the following error:
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6
I have googled some solutions and have found some posts saying I should try
env PYTHON_CONFIGURE_OPTS="--enable-framework" #or
env PYTHON_CONFIGURE_OPTS="--enable-shared"
This is they type of code I try to run
#Import packages
import dolfin as dl
I installed the env fenics by following the directions here
The google search possibly found Homebrew + Python on mac os x 10.8: Fatal Python error: PyThreadState_Get: no current thread importing mapnik however I was unable to find the library that links to a wrong version of python using otool.
I also found https://github.com/enthought/mayavi/issues/552 which suggests pinning to a different version of python.
Based on the install notes for hippy, https://hippylib.readthedocs.io/en/latest/installation.html then where they say conda create -n fenicproject ... you need to substitute the following:
conda create -n fenicsproject python==3.5.1
conda install -n fenicsproject -c conda-forge fenics==2017.2.0 \
mpi4py matplotlib scipy sympy==1.1.1 jupyter
After this, python -c 'import dolfin' no longer fails. It might also be possible to use a later version of python (I only tried 3.5.1).
What OS are you using? That will largely determine the specifics of how to go about correcting this issue, but the key here is which Python version the system path points to and what Python version your IDE is pointing to.
What you largely want to avoid is a situation where you are running Python scripts via your native system Python (2, likely).
Check which version of Python your IDE is using (from: How do I check what version of Python is running my script?):
import sys
print(sys.version)
Is the first number a 2? Did you want to use Python 2?
Next, let's check what version your system currently defaults to. If Ubuntu/Linux, use:
python -V
Is this expected? If not, you may need to change your system environmental variables to point to the correct Python version. The solution to this is OS dependent. If Windows, search "Edit Environmental Variables for Your Account" -> "Environmental Variables" -> "Path" , be sure it either points to Anaconda or the correct Python version; if Ubuntu/Linux, check your .bashrc file:
gedit ~/.bashrc
to see if the system points to the correct Python variable. If using a Mac, I formally apologize.

Getting the correct version of Pmw to install

Problem:
I'd like to install Pmw 2.0.0 (project page here) so that I can use it with tkinter in python3. The setup script from the package detects which version of python you're using and installs the version that is appropriate for your system (Ubuntu 15 in my case). I can't find any references to switches to make it install the 2.0.0 instead of 1.3.3(the Python 2.7 version), nor have I been able to get the script to install to the python3 libraries.
What I've done so far:
I've changed the python version detector in the setup script from
if sys.version_info[0]<3:
version='2.0.0' # really '1.3.3'
packages=['Pmw', 'Pmw.Pmw_1_3_3', 'Pmw.Pmw_1_3_3.lib',]
to
if sys.version_info[0]<2:
version='2.0.0' # really '1.3.3'
packages=['Pmw', 'Pmw.Pmw_1_3_3', 'Pmw.Pmw_1_3_3.lib',]
to attempt to force the installer to default to the python3 version, which it does, but it installs them in the python2.7 libraries (/usr/local/lib/python2.7/distpackages).
What I want to do:
I'm looking for a way to force the installer to put the 3.4-compatible package into the python3 libraries. If that means getting it to install both packages in their respective correct directories, that's fine, too. I'm stumped about what to try next.
Answered by RazZiel on AskUbuntu:
Link here.
Instead of using the command sudo python setup.py build and then sudo python setup.py install, I should have been using python3 to execute the setup script. I've managed to outthink myself pretty badly on this one.

OpenCV installation problems for Python

I am trying to install OpenCV on my Ubuntu system. I followed the steps of this page. I am able to run the demos for C/C++ etc. But when I try to run the python demos, I get an ImportError: No module named cv2
Here is more info :
python --version
Python 2.7.8 :: Anaconda 2.1.0 (64-bit)
python -c 'import sys; print sys.path'
['', '/home/radar/anaconda/lib/python2.7/site-packages/pydy-0.2.1-py2.7.egg', '/home/radar', '/home/radar/opencv-2.4.9/modules/python/src2', '/home/radar/anaconda/lib/python27.zip', '/home/radar/anaconda/lib/python2.7', '/home/radar/anaconda/lib/python2.7/plat-linux2', '/home/radar/anaconda/lib/python2.7/lib-tk', '/home/radar/anaconda/lib/python2.7/lib-old', '/home/radar/anaconda/lib/python2.7/lib-dynload', '/home/radar/anaconda/lib/python2.7/site-packages', '/home/radar/anaconda/lib/python2.7/site-packages/PIL', '/home/radar/anaconda/lib/python2.7/site-packages/Sphinx-1.2.3-py2.7.egg', '/home/radar/anaconda/lib/python2.7/site-packages/runipy-0.1.1-py2.7.egg', '/home/radar/anaconda/lib/python2.7/site-packages/setuptools-5.8-py2.7.egg']
You are running Anaconda Python distribution, but you need to use native python to access openCV library. Run Python with specified path and it should work:
/usr/bin/python
Also note, that to use openCV with virtual environmet, you need to manually copy openCV running objects (cv.py, cv2.so) to the environment you are going to use from the dist-packages of your native python, default path is /usr/local/lib/python2.7/dist-packages/.

Distribution independent libpython path

Under newer Ubuntu/Debian versions, libpython2.7.so is under /usr/lib/i386-linux-gnu/libpython2.7.so or /usr/lib/x86_64-linux-gnu/libpython2.7.so, etc. Earlier, they could be found in /usr/lib/libpython2.7.so, no matter the architecture. I haven't checked for other distributions. How do I find the path of libpython2.7.so with python?
Using pkg-config is not the best option - it will not distinguish between different installations of Python, returning only the system installation. You are better off using the Python executable to discover the location of libpythonX.Y.so.
From inside Python:
from distutils import sysconfig;
print sysconfig.get_config_var("LIBDIR")
Or inside a Makefile:
PYTHON_LIBDIR:=$(shell python -c 'from distutils import sysconfig; print sysconfig.get_config_var("LIBDIR")')
This will discover the location from whatever Python executable is first in $PATH and thus will work if there are multiple Python installations on the system.
Credit to Niall Fitzgerald for pointing this out.
Here is my solution which seems to work against system wide Debian and CentOS installation, anaconda on Debian, miniconda on OSX, virtualenv on Debian... but fails for system-wide python on OSX:
from distutils import sysconfig;
import os.path as op;
v = sysconfig.get_config_vars();
fpaths = [op.join(v[pv], v['LDLIBRARY']) for pv in ('LIBDIR', 'LIBPL')];
print(list(filter(op.exists, fpaths))[0])
and here it ran on my laptop:
$> for p in python python3 ~/anaconda-4.4.0-3.6/bin/python ~datalad/datalad-master/venvs/dev/bin/python ; do $p -c "from distutils import sysconfig; import os.path as op; v = sysconfig.get_config_vars(); fpaths = [op.join(v[pv], v['LDLIBRARY']) for pv in ('LIBDIR', 'LIBPL')]; print(list(filter(op.exists, fpaths))[0])"; done
/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6m.so
/home/yoh/anaconda-4.4.0-3.6/lib/libpython3.6m.so
/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
P.S. I had no clue that it is such a problem... bad bad bad Python
I'm assuming you're looking to link against this file. Python is usually installed with pkgconfig info to help compile against it. Specifically for the .so file, you should use pkg-config --libs python-2.7. From Python:
import subprocess
subprocess.check_output(["pkg-config", "--libs", "python-2.7"])
If the only flag shown is -lpython2.7, you might want to consider reading /etc/ld.so.conf to see default locations in which the linker looks for its libraries.

Categories