Yao Lin
Yao Lin

Reputation: 3

Using rPython import numpy with python 3.5

My R version is 3.4.1, python version is 3.5.2 , and OS is Ubuntu 16.04.2

I have set RPYTHON_PYTHON_VERSION=3.5 when installing rPython, which is my default python version for rPython.

♥ python.exec('import sys')
♥ python.exec('print(sys.version)')
3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609]

When I import numpy through rPython (there is no issue with using import numpy in python 3.5, everything works fine.), I got this:

♥ python.exec('import numpy')
Error in python.exec("import numpy") :
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

Original error was: /usr/local/lib/python3.5/dist-packages/numpy/core/multiarray.cpython-35m-x86_64-linux-gnu.so: undefined symbol: PyType_GenericNew

However, if I set RPYTHON_PYTHON_VERSION=2 and reinstall rPython, the import numpy works. How can I successfully import numpy under rPython with python 3.5?

Upvotes: 0

Views: 560

Answers (1)

Bartimus
Bartimus

Reputation: 140

First off, can you import any packages into python 3.5.3 from R/rPython?

I am also having this problem. The error I get is exactly the same as the posters (numpy won't load). I later found that I cannot import any packages. I can however import packages in python 2.7.13 and python 3.5.3 (just not through R/rPython). This leads me to believe that this is an 'rPython' R package error. Here are the things that I have tried to do to fix this:

1) I have tried installing/reinstalling the R package rPython to use either python 2.7.13 or python 3.5.3. I could connect R to python 2.7.13 via reinstall of the rPython package:

install.packages("rPython",lib= "home/myusername/R/x86_64-pc-linux-gnu-library/3.4", configure.vars= "RPYTHON_PYTHON_VERSION=2")

Using "RPYTHON_PYTHON_VERSION=3" during install similarly allowed me to connect R with python 3.5.3. I could call "import numpy" from R when rPython was connected to python 2.7.13, but not when connected with 3.5.3.

2) I have tracked down all numpy and scipy's which had been previously installed and uninstalled them. I had several copies of each for both python 2.7.13 and python 3.5.3. Reinstalling using pip and pip3 did not fix the problem (I restarted R beforehand to be safe).

From both accounts this seems to be a problem with the R package 'rPython'. You could try the newer 'reticulate' package from R, and see if this works better for you. However, I have not been able to get parallel threads to work when using reticulate to connect R with python, and this is unfortunately what I need to do. Threading did however work perfectly when using 'rPython', but the package I need requires python 3+. I will keep troubleshooting and update this post if I am able to solve it. In the meantime, give 'reticulate' a shot, it is a very neat package.

EDIT I was able to load numpy from python 3.5.3 in R using the 'reticulate' package.

EDIT2 For those who find this post in the future, the only solution I could find to use python3 code with multithreading from R was to call python files with system(python3 "path_to_python_script" arg1 arg2 arg3)

Upvotes: 0

Related Questions