mrkn / pycall.rb

Calling Python functions from the Ruby language
MIT License
1.06k stars 75 forks source link

How to import class with pyfrom? #124

Closed shamscorner closed 4 years ago

shamscorner commented 4 years ago

I want to import this, pyfrom :'sklearn.preprocessing', import: :PolynomialFeatures

But it is giving me this error, undefined methodmodule_eval'`

Can anybody help me with this? Am I missing something here?

lesliebinbin commented 4 years ago

probably should be pyfrom 'sklearn.preprocessing', import: 'PolynomialFeatures' as from the example https://github.com/mrkn/pycall.rb/blob/master/examples/plot_forest_importances_faces.rb, pyfrom 'sklearn.ensemble', import: 'ExtraTreesClassifier'

shamscorner commented 4 years ago

I tried with that solution but for some reason, it was not working. I solved this way though,

pyimport :'sklearn.preprocessing', as: :preprocessing

and then

poly = preprocessing.PolynomialFeatures.new(degree=4)

Ok, thanks for your reply. I will try to look at why the solution you mentioned was not working. I am closing this thread.

alexeevit commented 10 months ago

Ran into the same problem. If just in console I do pyfrom 'deepface', import: 'DeepFace' everything works fine. Once I created a class and call pyfrom in initializer, it crashes with the error:

/usr/local/bundle/ruby/3.2.0/gems/pycall-1.5.1/lib/pycall/import.rb:88:in `define_name': undefined method `module_eval' for #<DeepFaceFacade:0x0000ffffa42a5388 @assoc_array_matcher=#<Proc:0x0000ffffa42a5108 /usr/local/bundle/ruby/3.2.0/gems/pycall-1.5.1/lib/pycall/import.rb:107 (lambda)>> (NoMethodError)

          context.module_eval { const_set(name, pyobj) }
                 ^^^^^^^^^^^^

UPD: Since I use Rails, I just created an initializer and imported the module there, seem to work so far