2016-05-04 9 views
4

私は次のようにrubypythonをテストしてみました:Ruby rspecでPythonコードをテストするには?

bundle exec irb 
require 'rubypython' 
RubyPython.start 

これはエラーになりました。私の場合には、 irb(main):002:0> RubyPython.start(:python_exec => 'python2.7') RubyPython::InvalidInterpreter: An invalid interpreter was specified. from /home/nitrous/code/.bundle/gems/rubypython-0.6.3/lib/rubypython.rb:67:in `block in start' from /home/nitrous/code/.bundle/gems/rubypython-0.6.3/lib/rubypython/python.rb:10:in `synchronize' from /home/nitrous/code/.bundle/gems/rubypython-0.6.3/lib/rubypython/python.rb:10:in `synchronize' from /home/nitrous/code/.bundle/gems/rubypython-0.6.3/lib/rubypython.rb:54:in `start' from (irb):2 from /usr/local/opt/rbenv/versions/2.1.5/bin/irb:11:in `<main>'

ドキュメントは、私はルビーを使用してインポートされたことのPythonを実行することができるだろうことを示唆して:

Python-2.7.5 python --version 
Python 2.7.6 
➜ Python-2.7.5 cd .. 
➜ code ls 
design Gemfile Gemfile.lock Python-2.7.5 Python-2.7.5.tgz ratelimit_spec.rb 
➜ code bundle exec irb 
irb(main):001:0> require 'rubypython' 
=> true 
irb(main):002:0> RubyPython.start 
RubyPython::InvalidInterpreter: An invalid interpreter was specified. 

のPython 2.7がインストールされています:

エラーメッセージがありますRspec経由でテストしますが、そうはしません。

RubyからPythonをインポートして実行できるはずですか?

+0

完全なエラーメッセージを表示してください。 – Schwern

+1

@Schwernが今すぐ追加しました。ありがとうございます。 – Angela

+0

RubyPythonはPythonの特定のバージョンもインストールされていることに依存しますか? – tadman

答えて

1

最近のDebianビルドでRubyPythonを使って作業しているときに、私はこの問題に数回ぶつかりました。

この問題は、RubyPython :: Interpreter#find_python_libメソッドにあります。この方法では、python-configを呼び出すのではなく、ハードコードされたパスとOS検出を使用してライブラリを検索します。

私は方法を修復するために、次のコードを使用します。pythonconfigはライブラリを見つけることが失敗した場合

require "rubypython" 
class RubyPython::Interpreter 
    alias :find_python_lib_orig :find_python_lib 

    def find_python_lib 
    @libbase = "#{::FFI::Platform::LIBPREFIX}#{@version_name}" 
    @libext = ::FFI::Platform::LIBSUFFIX 
    @libname = "#{@libbase}.#{@libext}" 

    # python-config --confdir provides location of .so 
    config_util = "#{version_name}-config" 
    confdir = %x(#{config_util} --configdir).chomp 

    library = File.join(confdir, @libname) 
    if (File.exist? library) 
     @locations = [ library ] 
    else 
     library = find_python_lib_orig 
    end 
    library 
    end 
end 

RubyPython.start(:python_exe => "/usr/bin/python2.7") 

これは、元(欠陥)メソッドを呼び出します。

関連する問題