加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用Ruby rspec测试Python代码?

发布时间:2020-12-17 02:09:11 所属栏目:百科 来源:网络整理
导读:我尝试用以下方法测试 ruby python: bundle exec irbrequire 'rubypython'RubyPython.start 这导致了错误. 错误消息是: Python-2.7.5 python --versionPython 2.7.6? Python-2.7.5 cd ..? code lsdesign Gemfile Gemfile.lock Python-2.7.5 Python-2.7.5.t
我尝试用以下方法测试 ruby python:

bundle exec irb
require 'rubypython'
RubyPython.start

这导致了错误.

错误消息是:

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:

IRB(主):002:0> RubyPython.start(:python_exec =>’python2.7′)
RubyPython :: InvalidInterpreter:指定了无效的解释器.
????来自/home/nitrous/code/.bundle/gems/rubypython-0.6.3/lib/rubypython.rb:67:in,clock in start’
????来自/home/nitrous/code/.bundle/gems/rubypython-0.6.3/lib/rubypython/python.rb:10:in`synchronize’
????来自/home/nitrous/code/.bundle/gems/rubypython-0.6.3/lib/rubypython/python.rb:10:in`synchronize’
????来自/home/nitrous/code/.bundle/gems/rubypython-0.6.3/lib/rubypython.rb:54:in’start’
????来自(irb):2
????来自/usr/local/opt/rbenv/versions/2.1.5/bin/irb:11:in`< main>‘

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

我是否应该能够从Ruby中导入然后运行Python?

解决方法

在最近的Debian构建中使用RubyPython时,我遇到过几次这个问题.

问题在于RubyPython :: Interpreter#find_python_lib方法.此方法使用硬编码路径和OS检测来查找库,而不是调用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无法找到库,则会调用原始(有缺陷的)方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读