Distribution independent libpython path - python

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.

Related

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.

Error on import caffe

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 :)

QtCreator Python.h with Anaconda Python

Trying to embed anaconda Python into a Qt application. As a first step, I just want to get Python.h including properly.
I am running Qt4, Python 2.7.9, and OS X 10.9.
The project.pro has:
INCLUDEPATH += /home/myuser/anaconda/include/python2.7 /home/myuser/anaconda/include
LIBS += -L/home/myuser/anaconda/lib/python2.7 -lpython2.7
appended to the end.
In spite of this, I am getting the system installed python 2.7.5. This is verified by running import sys, sys.version and sys.path.
Clearly, I am missing something - a compiler flag? New to QtCreator, so any insight appreciated.

How to install EasyGUI on Mac OS X 10.6 (Snow Leopard)?

I would like to install EasyGUI on Mac OS X 10.6, but am running into trouble. Has anyone successfully done this? If so, what explicit set of steps did you follow?
Thank you.
It's hard to know what running into trouble means but, nonetheless, something like this seems to work on 10.6:
mkdir test_easygui
cd test_easygui
curl http://easygui.sourceforge.net/current_version/easygui_v0.93.tar.gz | tar xz
/usr/bin/python2.6 easygui.py
EDIT:
Unfortunately, the EasyGui download does not include a setup.py that would allow you to easily install it in the normal Python manner. To be a good citizen in the Python World, it should. Fortunately, it is easy to supply one. In the download directory (test_easygui in the example above), create the following text file and name it setup.py:
from distutils.core import setup
setup(name='easygui',
version='0.93',
py_modules=['easygui'],
)
Then run the following command:
sudo /usr/bin/python2.6 setup.py install
Now you should be able to run the demo from any directory by:
/usr/bin/python2.6 -m easygui
and you should be able to just import easygui in your own python modules. By the way, this process should work on any supported python platform (not just OS X) and with any supported python interpreter you have installed: just substitute the proper path for /usr/bin/python2.6. (If you don't have any extra pythons installed on OS X 10.6, typing just python should be sufficient.)

using RSPython in MacOSX

I am trying to install the
R/SPlus - Python Interface (RSPython) on my Mac OS X 10.4.11 with R version 2.7.2 (2008-08-25) and python 2.6.2 from fink.
The routine:
sudo R CMD INSTALL -c RSPython_0.7-1.tar.gz
produced this error message:
* Installing to library '/Library/Frameworks/R.framework/Resources/library'
* Installing *source* package 'RSPython' ...
checking for python... /sw/bin/python
Python version 2.6
Using threads
checking for gcc... gcc
checking for C compiler default output file name... configure: error: C compiler cannot create executables
See `config.log' for more details.
ERROR: configuration failed for package 'RSPython'
** Removing '/Library/Frameworks/R.framework/Versions/2.7/Resources/library/RSPython'
The config.log was not created o my system.
The contact e-mail address to the author does not work anymore, so I just hope somebody here tried the same already or can give me an alternative for running R routines in python.
Best regards,
Simon
To debug this, simply unpack the tar file yourself (tar xzvf RSPython_0.7-1.tar.gz) and run ./configure in the directory created. You should get a config.log file that you can examine.
Try running R CMD CHECK RSPython_0.7-1.tar.gz
That should produce at least produce bunch of logs in a RSPython.Rcheck folder
You might get some clues in there.
Update ---
If you can get one of the other packages to work I'd recommend it. On my system (R 2.9.1 using system python (2.6) in /usr/bin/python), install works but then RSPython fails to run due to problems inside its .First.lib function. I expect you would need to hack the sources considerably to get it to work.
I found the rpy and rpy2 packages and going to try those as an alternative to the older RSPython.
rpy2 is includes in fink's unstable distribution ... well, and it just works fine.

Categories