0

I created a Python program called test_optimize.py with just this line:

from scipy import optimize

I tried to run it with reticulate and got the error described below. What should I do to fix this?

library(reticulate)
source_python("~/test_optimize.py")

#> Error in py_run_file_impl(file, local, convert) : 
#>   ImportError: dlopen(/Users/vkv/anaconda3/lib/python3.6/sitepackages/scipy/optimize/minpack2.cpython-36m-darwin.so, 2): Symbol not found: _main
#>   Referenced from: /Users/vkv/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack2.cpython-36m-darwin.so
#>   Expected in: flat namespace
#>  in /Users/vkv/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack2.cpython-36m-darwin.so

It doesn't seem as if there's a problem with scipy. I ran the script below at the command line using Python 3.6.3, and it ran without errors and printed [ -1.09046105e-12 1.00000000e+00], which is a correct result.

import numpy as np
from scipy import optimize

x = np.array([0, 1])
y = x
fit = lambda beta, x: beta[0] + beta[1] * x
resid = lambda beta, x, y: y - fit(beta, x)
beta_init = np.array([0.5, 0.5])
beta_hat, success = optimize.leastsq(resid, beta_init, args = (x, y), ftol = 1e-9)
print(beta_hat)

Information about my R setup and Python setup is below.

sessionInfo()
#> R version 3.6.3 (2020-02-29)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Catalina 10.15.3
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] reticulate_1.13
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.4      lattice_0.20-40 digest_0.6.25   grid_3.6.3     
#>  [5] jsonlite_1.6.1  magrittr_1.5    evaluate_0.14   highr_0.8      
#>  [9] rlang_0.4.5     stringi_1.4.6   Matrix_1.2-18   rmarkdown_2.1  
#> [13] tools_3.6.3     stringr_1.4.0   xfun_0.12       yaml_2.2.1     
#> [17] compiler_3.6.3  htmltools_0.4.0 knitr_1.28

py_config()
#> python:         /Users/vkv/anaconda3/bin/python
#> libpython:      /Users/vkv/anaconda3/lib/libpython3.6m.dylib
#> pythonhome:     /Users/vkv/anaconda3:/Users/vkv/anaconda3
#> version:        3.6.3 |Anaconda, Inc.| (default, Oct  6 2017, 12:04:38)  [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
#> numpy:          /Users/vkv/anaconda3/lib/python3.6/site-packages/numpy
#> numpy_version:  1.13.3
#> 
#> python versions found: 
#>  /Users/vkv/anaconda3/envs/r-reticulate/bin/python
#>  /Users/vkv/anaconda3/bin/python
#>  /usr/bin/python
#>  /usr/bin/python3
#>  /usr/local/bin/python3
VKV
  • 135
  • 5

1 Answers1

2

Looking at the error you obtain, i wander what does your computer say if you try to run the same import statement from python directly. My guess is that you'll have the same error : there is a problem in the scipy installation in your anaconda;

For me, this has nothing to do with reticulate.

lrnv
  • 1,038
  • 8
  • 19
  • I revised my post to show that SciPy works fine outside reticulate. – VKV Apr 07 '20 at 22:01
  • 1
    As you showed in the py_config() result, this is not the same python that runs from reticulate and from your comand line. Try using `reticulate::use_python("/usr/local/bin/python3")` to force reticulate to use the same python as the comand line; – lrnv Apr 08 '20 at 07:52