With the r package reticulate
, I attempted to import the python package scipy
to do my data analysis. However, it says
ModuleNotFoundError: No module named 'scipy'.
But when I use jupyter notebook, clearly I can import scipy
so my computer has the package. Somehow RStudio is not importing it.
This is what I did:
library(reticulate) #import reticulate to enable python usage in r
use_python("/usr/local/bin/python3", required = TRUE) #set the path for where my python is.
from scipy.optimize import minimize #try to import the package in a python block
Then I get the error package not found. I tried so many things this afternoon but failed
First, I tried to install again using r studio with py_install("scipy")
. It installs, but when I try to import again, it still reports not module found.
Second, I tried to create virtual environments by conda_create('r-reticulate', packages = "python=3")
and then py_install("scipy")
. Doesn't help.
Third, I upgraded the python to python 3.7. Now not only the scipy
package, but all python packages like numpy
, pandas
also could not be imported. Of course they can still be imported in Jupiter notebook so it's not the case that my computer does not have them.
I tried the previous two things again but no help.
library(reticulate)
use_python("/usr/local/bin/python3.7", required = TRUE)
from scipy.optimize import minimize
import matplotlib.pyplot as plt # for plotting
import numpy as np # for numeric calculations
import pandas as pd # for python data frame
import time
I expect to successfully import all the python packages to RStudio. And figure out a way to install new python packages and import them to r.