1

I would like to call a python function from C++ and get the return value. I've been able to do that with an easy multiply python function using this website's example code in section 5.3. To compile my program, I would run g++ test.cpp -I/usr/include/python2.7 -lpython2.7. However, the python function I want to run imports numpy. When I try to run my program that is similar to the one on the code example mentioned above, I get an "ImportError: cannot import name _remove_dead_weakref". The full error is here:

Traceback (most recent call last):
  File "/home/osboxes/Desktop/test.py", line 1, in <module>
    import numpy as np 
  File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
    from . import add_newdocs
  File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/core/__init__.py", line 74, in <module>
    from numpy.testing.nosetester import _numpy_tester
  File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/testing/__init__.py", line 10, in <module>
    from unittest import TestCase
  File "/home/osboxes/miniconda2/lib/python2.7/unittest/__init__.py", line 64, in <module>
    from .main import TestProgram, main
  File "/home/osboxes/miniconda2/lib/python2.7/unittest/main.py", line 7, in <module>
    from . import loader, runner
  File "/home/osboxes/miniconda2/lib/python2.7/unittest/runner.py", line 7, in <module>
    from .signals import registerResult
  File "/home/osboxes/miniconda2/lib/python2.7/unittest/signals.py", line 2, in <module>
    import weakref
  File "/home/osboxes/miniconda2/lib/python2.7/weakref.py", line 14, in <module>
    from _weakref import (
ImportError: cannot import name _remove_dead_weakref

Some information: Python version is Python 2.7.14 :: Anaconda, Inc. (Is there a difference between python 2.7.14 and my version which has anaconda, inc. at the end?) The python program also runs just fine by itself. Any help would be appreciated. Thanks!

Edit: The path was being all weird with some parts going to my local python and numpy going to miniconda's python. Uninstalling miniconda as it wasn't needed for me fixed it.

Steven T
  • 83
  • 9

1 Answers1

2

This is happening because your environment is mixing two different Python installations. You can see it jump between them here:

File "/home/osboxes/.local/lib/python2.7/site-packages/numpy/testing/__init__.py"
File "/home/osboxes/miniconda2/lib/python2.7/unittest/__init__.py"

So you start out in /home/osboxes/.local/lib/python2.7/site-packages which is the Python installed by some system package manager (or perhaps even explicitly installed from source). But then it jumps to /home/osboxes/miniconda2/lib/python2.7 which is from Conda.

Since it appears you are intending to use Python from Conda, you need to install NumPy using Conda (so it is loaded from miniconda2 and not .local, and build your code using something like -I/home/osboxes/miniconda2/include/python2.7 instead of -I/usr/include/python2.7.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Makes sense. I've tried to build my code around it using the suggested `-I/home/osboxes/miniconda2/include/python2.7` after installing NumPy into Conda and it seems to still go to `/home/osboxes/.local/lib/python2.7/site-packages` first and get the same ImportError that I had mentioned above. I believe that the code I intend on using doesn't actually need Python from Conda. Is it possible to just use my local python instead of Conda's? – Steven T Apr 01 '18 at 04:06
  • 1
    What is your `PYTHONPATH` environment variable set to? Which Python library is listed when you run `ldd` on your executable file? You can certainly avoid using Python from Conda--that will probably happen by default if you make sure it's not in your `PYTHONPATH`. – John Zwinck Apr 01 '18 at 04:30
  • I remember looking at [here](https://stackoverflow.com/questions/24492327/python-embedding-in-c-importerror-no-module-named-pyfunction) and so I set my `PYTHONPATH` to `.`. By default, my `PYTHONPATH` isn't set to anything. When I run `ldd` on the executable file generated from `g++ test.cpp -I/home/osboxes/miniconda2/include/python2.7 -lpython2.7` , it linked to `libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0`. – Steven T Apr 01 '18 at 05:17
  • OK, so you need to decide if you want to use miniconda (for everything!) or not (for anything!). Probably you should not set `PYTHONPATH` to anything, unless you need to set it to be able to `import` your own .py files. – John Zwinck Apr 01 '18 at 05:19
  • When I don't set `PYTHONPATH` to anything, I experience the error that was in the previously linked post [linked here again](https://stackoverflow.com/questions/24492327/python-embedding-in-c-importerror-no-module-named-pyfunction). I would like to not use miniconda for anything(would uninstalling conda be a hacky workaround?) – Steven T Apr 01 '18 at 05:22
  • Right OK, so you need `PYTHONPATH=.` or similar to find your .py file. How did you install NumPy? Did you build it from source, or `apt-get install numpy` or what? – John Zwinck Apr 01 '18 at 05:30
  • I had used `pip install numpy` to install NumPy. – Steven T Apr 01 '18 at 05:33
  • 1
    OK. I suggest uninstalling Conda for now. That might either make it work, or make the path to working more clear. – John Zwinck Apr 01 '18 at 05:36
  • That fixed all the problems I had. Thank you so much for all the help! – Steven T Apr 01 '18 at 05:40