gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:37:29 24 4
gpt4 key购买 nike

我尝试测试 rubypython具有以下内容:

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(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 吗?

最佳答案

在最近的 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 找不到库,这将调用原始的(有缺陷的)方法。

关于python - 如何使用 Ruby rspec 测试 Python 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37039129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com