Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

7/14/2016

Compile my C/C++ program with certain version of Python interpreter

If you have more than two Pythons in your system, such as in the case that Python2 and Python3 are installed in your system, you may have to specify one explicitly to compile that with your application program, which is written not in Python. Here is how to specify PythonX.Y with the python version number X.Y in a Makefile to build your application.

First, you need to find out compiler and linker options specified at the time of installation of PythonX.Y:

% pythonX.Y-config --cflags

will show you the compiler options <CFLAGS> and

% pythonX.Y-config --ldflags

will show you the linker options <LDFLAGS>.

Second, you add lines in the Makefile to include <CFLAGS> in the compilation and load <LDFLAGS> in the linkage.

That's it!

2/15/2014

Choosing a python bundle for PyROOT

After played with python 2.7 for a while, I got a need for ROOT drawings. So, I decided to try PyROOT which was supposed to be installed already in my ROOT 5.34.09 installation:

% port -v installed root
The following ports are currently installed:
  root @5.34.09_1+graphviz+gsl+minuit2+opengl+python27+roofit+soversion+ssl+tmva+xml (active) platform='darwin 12' archs='x86_64'

OK, python27 is there. But my python told

% python -c "import ROOT"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named ROOT

Well, PyROOT needs two files, ROOT.py and libPyROOT.so which must be visible from python. Where are they?
% port contents root | grep ROOT.py
  /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ROOT.py
% port contents root | grep libPyROOT.so
  /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/libPyROOT.so
  /opt/local/lib/root/libPyROOT.so

OK, you guys are there. But when I make these guys visible (by copying or symbolic links) from python, it told

% python -c "import ROOT"
Fatal Python error: PyThreadState_Get: no current thread
Abort trap: 6

What's wrong? After for a while, I found the bundle of python which I was using did not fit with the guy libPyROOT.so, who came through macport. To use this guy, I need to switch my python to the one came also from macport. This one was installed as /opt/local/bin/python2.7, and

%/opt/local/bin/python2.7 -c "import ROOT"

Passed!  What is the difference between 'em?

%/usr/bin/python
Python 2.7.5 (default, Aug  1 2013, 01:01:17)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin

%/opt/local/bin/python2.7
Python 2.7.5 (default, Aug 25 2013, 00:04:04) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin

Well, well, well. I can set my default python as the later one by, for instance,
sudo ln -s /opt/local/bin/python2.7 /opt/local/bin/python
and exporting PATH with /opt/local/bin placed earlier than /usr/bin so that I can just write, e.g.

%python -c "from ROOT import TCanvas"