如何用Ruby rspec测试Python代码?

2024-09-27 00:12:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试用以下方法测试rubypython

bundle exec irb
require 'rubypython'
RubyPython.start

这导致了一个错误。在

错误消息是:

^{pr2}$

已安装Python 2.7: 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>'

文档建议我可以运行使用Ruby导入的Python,在我的例子中,可以通过Rspec进行测试,但事实并非如此

我应该能够从Ruby中导入并运行Python吗?在


Tags: ingemsfromhomelib错误codestart
1条回答
网友
1楼 · 发布于 2024-09-27 00:12:34

在最近的Debian构建中使用RubyPython时,我曾多次遇到这个问题。在

问题在于RubyPython::Interpreter\find_python_lib方法。此方法使用硬编码路径和操作系统检测来查找库,而不是调用python config。在

我使用以下代码修复该方法:

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") 

如果pythonconfig找不到库,则调用原始(有缺陷)方法。在

相关问题 更多 >

    热门问题