mrkn / pycall.rb

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

How to use PyCall in Rails 5.1? #66

Closed eltonfonseca closed 6 years ago

eltonfonseca commented 6 years ago

Hello, how do I use pycall in rails 5.1?

I have the following controller in Rails:

class RequerimentsController < ApplicationController

  require 'pycall/import'
  include PyCall::Import

  layout "admin"

  def index 
  end

  def show
  end

  def new
    @requeriment = Requeriment.new
    pyimport :nltk

    message = "Testando a integracao."
    nltk.word_tokenize(message)
  end
end

When I raise the server with rails s, and I trigger the new action it gives the following error:

/home/elton/.rvm/gems/ruby-2.5.0/gems/pycall-1.0.3/lib/pycall/pyobject_wrapper.rb:71: [BUG] Segmentation fault at 0x0000000000000020
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

-- Control frame information -----------------------------------------------
c:0092 p:---- s:0604 e:000603 CFUNC  :from_ruby
c:0091 p:0027 s:0599 e:000598 METHOD /home/elton/.rvm/gems/ruby-2.5.0/gems/pycall-1.0.3/lib/pycall/pyobject_wrapper.rb:71
c:0090 p:0022 s:0594 e:000593 METHOD /home/elton/.rvm/gems/ruby-2.5.0/gems/blankslate-3.1.3/lib/blankslate.rb:131 [FINISH]
c:0089 p:---- s:0588 e:000587 CFUNC  :include
c:0088 p:0086 s:0583 e:000582 METHOD /home/elton/.rvm/gems/ruby-2.5.0/gems/pycall-1.0.3/lib/pycall/pytypeobject_wrapper.rb:16 [FINISH]
c:0087 p:---- s:0577 e:000576 CFUNC  :extend
c:0086 p:0024 s:0572 e:000571 BLOCK  /home/elton/.rvm/gems/ruby-2.5.0/gems/pycall-1.0.3/lib/pycall/pytypeobject_wrapper.rb:86 [FINISH]
c:0085 p:---- s:0568 e:000567 CFUNC  :initialize
c:0084 p:---- s:0565 e:000564 CFUNC  :new
c:0083 p:0012 s:0561 e:000560 BLOCK  /home/elton/.rvm/gems/ruby-2.5.0/gems/pycall-1.0.3/lib/pycall/pytypeobject_wrapper.rb:84
c:0082 p:0166 s:0558 e:000557 METHOD /home/elton/.rvm/gems/ruby-2.5.0/gems/pycall-1.0.3/lib/pycall/wrapper_object_cache.rb:34
c:0081 p:0023 s:0551 e:000550 METHOD /home/elton/.rvm/gems/ruby-2.5.0/gems/pycall-1.0.3/lib/pycall/pytypeobject_wrapper.rb:83 [FINISH]
c:0080 p:-17563899743352 s:0546 e:000545 TOP    [FINISH]
c:0079 p:---- s:0543 e:000542 CFUNC  :require
mrkn commented 6 years ago

@eltonfonseca Does the following code works correctly?

require 'pycall'

NLTK = PyCall.import_module('nltk')

class RequerimentsController < ApplicationController
  layout "admin"

  def index 
  end

  def show
  end

  def new
    @requeriment = Requeriment.new
    message = "Testando a integracao."
    NTLK.word_tokenize(message)
  end
end

BTW, please show the full messages by SEGV. Don't cut its fragment your own decision.

eltonfonseca commented 6 years ago

Hello @mrkn, I ran and gave the following error:

NameError at /requeriments/new

uninitialized constant RequerimentsController::NTLK

but I was able to solve the problem by typing the following code.

require 'pycall'

class RequerimentsController < ApplicationController
  layout "admin"

  def index 
  end

  def show
  end

  def new
    @requeriment = Requeriment.new
    message = "Testando a integracao."
    nltk = PyCall.import_module('nltk')
    puts nltk.word_tokenize(message)
  end
end

Thank you for your attention and for your help!

mrkn commented 6 years ago

@eltonfonseca That's good!