No module named 'rpytools'?
Asked Answered
C

4

7

I'm trying to work with the reticulate library in R. I used the "functions.py" example to test it out:

# functions.py file
def add(x, y): 
    return x + y

In R studio (Version 3.5.2), this is what I have:

library(reticulate)  
source_python('functions.py')

However, this returns an error:

Error in py_set_attr_impl(x, name, value) : 
  Evaluation error: ModuleNotFoundError: No module named 'rpytools'.

So I'm stuck here. If it helps, I'll also share that my Python is 64-bit and version 3.6.5. Anyone know how to go about this?

Thanks

Castello answered 20/2, 2019 at 16:30 Comment(0)
H
2

rpytools is a module provided by reticulate and should be placed on the module path for you. For example, I see:

> library(reticulate)
> sys <- import("sys", convert = TRUE)
> sys$path
 [1] ""                                                                                                          
 [2] "/usr/local/Cellar/python@2/2.7.15_3/Frameworks/Python.framework/Versions/2.7/bin"                          
 [3] "/Users/kevin/Library/Python/2.7/lib/python/site-packages/pip-18.1-py2.7.egg"                               
 [4] "/Users/kevin/Library/Python/2.7/lib/python/site-packages/virtualenv-16.0.0-py2.7.egg"                      
 [5] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python27.zip"                         
 [6] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7"                            
 [7] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin"                
 [8] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac"                   
 [9] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages"
[10] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk"                     
[11] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old"                    
[12] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload"                
[13] "/Users/kevin/Library/Python/2.7/lib/python/site-packages"                                                  
[14] "/usr/local/opt/python@2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages"              
[15] "/Users/kevin/Library/R/3.5/library/reticulate/python"     

Note the last entry, which provides the path where rpytools would be found on import. Do you see something similar?

Hoahoactzin answered 22/2, 2019 at 0:30 Comment(0)
M
1

Install reticulate package, create conda env and install keras, tf and anything else you need.

Find where the packages for your current R version get stored, and locate reticulate folder, inside there should be a python/rpytools directory. (in my case R packages were installed in 'C:\Users\abc\Documents\R\win-library\4.1\reticulate\python')

Use this code:

    library(reticulate)
    sys <- import("sys", convert = TRUE)
    sys$path 

Last entry shows directory where you should copy the rpytools folder. After this, things started working for me.

Matabele answered 7/6, 2021 at 16:13 Comment(0)
M
1

I had the same problem in windows and what helped me was to manually add the path to reticulate python to the PYTHONPATH ennvironment variable.

In R you get the path to reticulate python with:

paste0(system.file(package = "reticulate"),"/python")

Create the PYTHONPATH environment variable in windows system environment variable settings if it doesnt exist. Add the result from the command above to the PYTHONPATH ennvironment variable path. Save and restart RStudio.

Mollymollycoddle answered 24/11, 2023 at 11:47 Comment(0)
A
1

I was able to fix this by copying the rpytools folder from my R package library and putting it into my Python site-packages folder.

Ararat answered 20/12, 2023 at 5:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.