5

I have the following file in a folder called cpp_examples.

#include <boost/python.hpp>
#include <string>

const std::string hello() {
return std::string("hello, zoo");
}

BOOST_PYTHON_MODULE(zoo) {
// An established convention for using boost.python.
using namespace boost::python;
def("hello", hello);
}

And I ran the following command to compile.

sumith@rztl516-Lenovo-G575:~/cpp_examples$ g++ zoo.cpp -I/usr/include/python2.7 -I/usr/lib/x86_64-linux-gnu/ -lboost_python  -lpython2.7 -o zoo.so -shared -fPIC

It got compiled and gave me a zoo.so file. And when I tried to import and run the zoo.hello() within the same folder it worked but it is not importing outside the cpp_examples folder

sumith@rztl516-Lenovo-G575:~/cpp_examples$ python2
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zoo
>>> zoo.hello()
'hello, zoo'
>>> exit()

The following is outside cpp_examples folder.

sumith@rztl516-Lenovo-G575:~$ python2
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zoo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named zoo
>>> 

What might be the reason for not getting imported of that folder ?. And while compiling I checked for python3 it is not compiling at all where I changed -lpython2.7 to -lpython3.4 and -I/usr/include/python2.7 to I/usr/include/python3.4 in above command but it is giving me error while compiling

/usr/bin/ld: cannot find -lpython3.4

If I can get answers for these two questions it would be of a great help. Thank you.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
Sumith
  • 161
  • 7
  • if `zoo.so` is in `~/cpp_examples/` then how would python know where `zoo` is when its run from `~/`? – kmdreko Mar 20 '17 at 06:49
  • related: http://stackoverflow.com/questions/15252040/how-does-python-find-a-module-file-if-the-import-statement-only-contains-the-fil – kmdreko Mar 20 '17 at 06:59
  • @vu1p3n0x So where do I have to place `zoo.so` ? – Sumith Mar 20 '17 at 07:07
  • 2
    @Sumith - Start up the Python2.7 interpreter, `import sys` and `print(sys.path)`. It should give you a list of the places you could put it and have it work. Your Python3 problem is more complex. You need a version of Boost.Python compiled specifically for Python3. – Omnifarious Mar 20 '17 at 07:11
  • Thank you very much @Omnifarious. I placed `zoo.so` in /usr/lib/python2.7/dist-packages and it works like charm in python2. Any suggestion on compiling in python3 ? – Sumith Mar 20 '17 at 07:41
  • Yes, but it'll have to wait until tomorrow. – Omnifarious Mar 20 '17 at 07:56
  • 1
    hm, change it to `-lpython3.4m` and try that again. In general, you can always use `python3-4-config --ldflags` to get the required flags for linking. – Dimitris Fasarakis Hilliard Mar 20 '17 at 08:25
  • 1
    Thank you @JimFasarakisHilliard I changed my command to `g++ zoo.cpp -I/usr/include/python3.4 -I/usr/lib/x86_64-linux-gnu/ -lboost_python-py34 -lpython3.4m -o zoo.so -shared -fPIC` it's working. – Sumith Mar 20 '17 at 10:26
  • I would actually recommend setting the `PYTHONPATH` environment variable rather than stuffing a file into a directory that should be managed by your distribution's package manager. – Omnifarious Mar 20 '17 at 17:00

1 Answers1

2

As @Omnifarious originally stated in a comment, you should look at the directories present in sys.path to find places where you can put your .so file. That's where Python will look by default when the import statement is found.

When you run Python in the directory containing the .so, it finds it easily since the first entry is sys.path is actually the current directory.

As for linking, you should generally provide the the flags that pythonX.Y-config --ldflags gives you, as described in the section Compiling and Linking under Unix-like systems.

The example in the documentation also provides sample output for it using Python 3.4:

$ /opt/bin/python3.4-config --ldflags
-L/opt/lib/python3.4/config-3.4m -lpthread -ldl -lutil -lm -lpython3.4m -Xlinker -export-dynamic
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253