Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzzywuzzyR: "attempt to apply non-function" error for all the functions under FuzzMatcher, FuzzExtract and FuzzUtils classes #1

Closed
SNaveenMathew opened this issue Jun 13, 2017 · 14 comments

Comments

@SNaveenMathew
Copy link

I'm running R version 3.3.2 (64-bit), python version 2.7.12 (32-bit) on Windows 8.1 (64-bit). I have installed fuzzywuzzyR as well as corresponding python packages: fuzzywuzzy, Levenshtein

I tried the following:

s1 = ' It was a dark and stormy night. I was all alone sitting on a red chair.'
s2 = ' It was a murky and stormy night. I was all alone sitting on a crimson chair.'
init <- FuzzMatcher$new()
init$Partial_ratio(string1 = s1, string2 = s2)

I received the following error message upon running the last statement:
"Error in init$Ratio(string1 = s1, string2 = s2) :
attempt to apply non-function"

I also observed the following:

check_availability()
[1] FALSE

Is there a fix for this problem? All the online articles showed positive result while testing the functions of FuzzMatcher class.

@SNaveenMathew SNaveenMathew changed the title fuzzywuzzyR: "attempt to apply non-function" for all the functions under FuzzMatcher class fuzzywuzzyR: "attempt to apply non-function" error for all the functions under FuzzMatcher class Jun 13, 2017
@SNaveenMathew SNaveenMathew changed the title fuzzywuzzyR: "attempt to apply non-function" error for all the functions under FuzzMatcher class fuzzywuzzyR: "attempt to apply non-function" error for all the functions under FuzzMatcher, FuzzExtract and FuzzUtils classes Jun 13, 2017
@mlampros
Copy link
Owner

The fuzzywuzzyR package uses the reticulate package for the R-Python interface. The newer version of the reticulate package (== 0.8) includes a function py_discover_config(required_module = NULL), which shows if a specific module is installed in a python version (i.e. 2.7, 3.5) . For instance, the following appears in my R-session for the fuzzywuzzy module:

> reticulate::py_discover_config(required_module = 'fuzzywuzzy')
python:         /usr/bin/python3
libpython:      /usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/libpython3.5.so
pythonhome:     /usr:/usr
version:        3.5.2
numpy:          /usr/local/lib/python3.5/dist-packages/numpy
numpy_version:  1.12.1
fuzzywuzzy:     /usr/local/lib/python3.5/dist-packages/fuzzywuzzy

python versions found: 
 /usr/bin/python
 /usr/bin/python3

You can do the same for the other two modules, which are necessary for the fuzzywuzzyR package ,

reticulate::py_discover_config(required_module = 'Levenshtein')

reticulate::py_discover_config(required_module = 'difflib')

That way you can find out if all modules are installed in your default python version.

@SNaveenMathew
Copy link
Author

@mlampros: Your solution works! Thank you very much.

I followed these steps:

  1. reticulate::py_discover_config() showed me python v 3.5.3 (64-bit). However, command line start showed python v 2.7.12 (32-bit). On further investigation, I realized that a separate version of python was installed during installation of tensorflow package in R.

  2. As a quick fix, I changed the PATH variables to python 3.5 location and used pip to install Levenshtein and fuzzywuzzy. If R instance was running during this operation, then close and reopen R.

Result:
check_availability()
[1] TRUE

matcher$Partial_ratio(string1 = "abcd", string2 = "def")
[1] 33

@SNaveenMathew
Copy link
Author

I found the same issue when I used fuzzywuzzyR in another system:

reticulate::py_discover_config(required_module="fuzzywuzzy")
python: C:\Python27\python.exe
libpython: python27.dll
pythonhome: C:\Python27
version: 2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)]
Architecture: 32bit
numpy: C:\Python27\lib\site-packages\numpy
numpy_version: 1.13.0
fuzzywuzzy: C:\Python27\lib\site-packages\fuzzywuzzy

Python directory looks fine, python package is correctly detected. However,

matcher <- FuzzMatcher$new()
matcher$Partial_ratio(string1 = "abcd", string2 = "def")
Error in matcher$Partial_ratio(string1 = "abcd", string2 = "def") :
attempt to apply non-function

@mlampros
Copy link
Owner

The fact is that I built fuzzywuzzyR on a linux OS and I didn't encounter similar errors. However I think that the issues of the reticulate package (or issues of any package that imports the reticulate package - see the 'Reverse imports' or 'Reverse suggests') can be of advantage for you. For instance :

https://github.com/statsmaths/kerasR/issues/1
https://github.com/statsmaths/kerasR/issues/20
rstudio/reticulate#27

My guess is that you have more than one python 2.7 version on your operating system.

@mlampros
Copy link
Owner

mlampros commented Jul 1, 2017

I'll close the issue for now, hopefully you found a fix in the previously mentioned links.

@mlampros mlampros closed this as completed Jul 1, 2017
@SNaveenMathew
Copy link
Author

Here's a quick fix:

  1. Install Python 3.5.3 64-bit version in the other system and pointed R to this version.
  2. Install fuzzywuzzy and Levenshtein using pip.
  3. Restart R

Now check_availability shows TRUE.

@mlampros
Copy link
Owner

mlampros commented Jul 2, 2017

I'm glad you made it work.

@bradylawrencewoods
Copy link

bradylawrencewoods commented Mar 13, 2018

Hello, I am bumping into this is same issue as those above. My check_availability function returns false:

check_availability()
[1] FALSE

These are my reticulate returns for the Levenshtein and difflib packages:

reticulate::py_discover_config(required_module = 'Levenshtein')
python:         C:\Python27\ArcGIS10.5\python.exe
libpython:      python27.dll
pythonhome:     C:\Python27\ArcGIS10.5
version:        2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
Architecture:   32bit
numpy:          C:\Python27\ArcGIS10.5\lib\site-packages\numpy
numpy_version:  1.9.3
Levenshtein:    C:\Python27\ArcGIS10.5\lib\site-packages\python_levenshtein-0.12.0-py2.7 win32.egg\Levenshtein

reticulate::py_discover_config(required_module = 'difflib')
python:         C:\Python27\ArcGIS10.5\python.exe
libpython:      python27.dll
pythonhome:     C:\Python27\ArcGIS10.5
version:        2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
Architecture:   32bit
numpy:          C:\Python27\ArcGIS10.5\lib\site-packages\numpy
numpy_version:  1.9.3
difflib:        C:\Python27\ArcGIS10.5\lib\difflib.py

I can import both of these packages into a Python session, so I am a bit confused as to why R cannot find them. Thanks for your work on this!

@mlampros
Copy link
Owner

mlampros commented Mar 13, 2018

@bradylawrencewoods,

if you tried all the previous suggestions and they do not work for you then probably is something wrong with the python configuration. I faced the same problem in the past when trying to import python modules with the reticulate package. To avoid unistalling and installing python from scratch, I would suggest to also try the following (which in some cases solved the issue for me):

  • run R or Rstudio as Administrator (Start > search program and files > give R or Rstudio > right click > Run as administrator
  • run R from command prompt.

In my OS, for instance, R is located in C:\Program Files\R\R-3.4.0\bin\x64\R. Then, by opening a command prompt (console) and giving (for instance in my case),

cd C:\Program Files\R\R-3.4.0\bin\x64\

R

I had access to R console.

@bradylawrencewoods
Copy link

@mlampros

I believe this is the issue as the package cannot locate my python installation, which is located outside of the normal directory due to other software dependencies. Thanks!

@karrtikiyer
Copy link

I am still facing this issue, I am running it on MacOs and have used reticulate to point to an existing conda env.
> check_availability() [1] FALSE

> reticulate::py_discover_config(required_module = 'Levenshtein')
python:         /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/bin/python
libpython:      /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/lib/libpython3.8.dylib
pythonhome:     /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env:/Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env
version:        3.8.3 (default, May 19 2020, 13:54:14)  [Clang 10.0.0 ]
numpy:          /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/lib/python3.8/site-packages/numpy
numpy_version:  1.18.1
Levenshtein:    /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/lib/python3.8/site-packages/Levenshtein

python versions found: 
 /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/bin/python
 /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/bin/python3
 /usr/bin/python3
 /usr/bin/python
> reticulate::py_discover_config(required_module = 'difflib')
python:         /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/bin/python
libpython:      /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/lib/libpython3.8.dylib
pythonhome:     /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env:/Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env
version:        3.8.3 (default, May 19 2020, 13:54:14)  [Clang 10.0.0 ]
numpy:          /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/lib/python3.8/site-packages/numpy
numpy_version:  1.18.1
difflib:        /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/lib/python3.8

python versions found: 
 /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/bin/python
 /Users/my_user/.pyenv/versions/miniconda3-latest/envs/my_env/bin/python3
 /usr/bin/python3
 /usr/bin/python

@mlampros
Copy link
Owner

mlampros commented Jun 9, 2020

hi @karrtikiyer,

you use miniconda with which I am not familiar. Would you mind taking a look to the issues of the reticulate package related to miniconda3. There might be related issues to MacOS operating system.
Have you tried to import the fuzzywuzzy python module too? Either in an R or python console.

@karrtikiyer
Copy link

I reinstalled R and RStudio again, now it works with the above miniconda version. Thanks @mlampros

@timonbldw
Copy link

timonbldw commented Sep 2, 2021

I faced the same issue, I installed everything correctly and reticulate showed that fuzzywuzzy etc. was installed and found. However, fuzzywuzzyR would still throw this error. I also noticed, when running reticulate::py_available() in a fresh R-environment it would return FALSE until I'd run reticulate::py_config().

Long story short, in order to get the library working I always have to run reticulate::py_config() before loading the library:

reticulate::py_config()  # fuzzywuzzyR will return "attempt to apply non-function" when trying to call a function if this line is missing
library("fuzzywuzzyR")

matcher <- FuzzMatcher$new()
matcher$Partial_ratio(string1 = "abcd", string2 = "def")  # throws no error

I added a pull request to include it in the .onLoad() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants