4

I tried to test rubypython with the following:

bundle exec irb
require 'rubypython'
RubyPython.start

This resulted in an error.

The error message is:

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 has been installed: 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>'

The documentation suggested that I would be able to run Python that has been imported using Ruby and, in my case, test via Rspec, but it doesn't.

Should I be able to import and then run Python from within Ruby?

Satchel
  • 16,414
  • 23
  • 106
  • 192
  • Could you show us the full error message please? – Schwern May 04 '16 at 22:30
  • 1
    @Schwern just added it now, thanks. – Satchel May 04 '16 at 22:32
  • Does RubyPython depend on having a particular version of Python also installed? – tadman May 04 '16 at 22:40
  • Do you have `python` installed and in your PATH? – Schwern May 04 '16 at 22:40
  • @tadman RubyPython doesn't support python > 3.0 – Satchel May 04 '16 at 22:42
  • @Schwern I am checking the version now with python --version.....I didn't have python installed. When I read the documentation, I read it as the gem came with a python vm. – Satchel May 04 '16 at 22:43
  • This didn't work for me either. I don't think this question is really specific to RSpec, but rather how to get the correct Python version loaded. – max pleaner May 04 '16 at 23:26
  • I added the console above: 2.7.6 but I still get the error of invalid interpreter. How do I confirm the right path? – Satchel May 04 '16 at 23:32
  • I believe you have to install python yourself. And it has to be Python 2.x. – Schwern May 05 '16 at 01:03
  • @maxpleaner I have python 2.7.6 loaded. It should be supported, but I still get an error. Looking at how to explicitly point to this as the interpreter. – Satchel May 05 '16 at 21:48
  • @maxpleaner -- I do, in fact, have python and the path correct. If it type python2.7, I enter the python console. But it still doesn't work, even when I explicitly name the interpreter. – Satchel May 05 '16 at 22:36
  • @Angela I tried it (on ubuntu) and I have the same issue as you, not sure how to fix it. – max pleaner May 05 '16 at 22:58
  • @maxpleaner --- argh, thanks I have been running on ubuntu as well. I logged a ticket with the repo. ugh. Thanks! – Satchel May 06 '16 at 19:15

1 Answers1

1

I've run into this problem a few times when working with RubyPython on a recent Debian build.

The problem lies in the RubyPython::Interpreter#find_python_lib method. This method uses hard-coded paths and OS detection to find the library, instead of invoking python-config.

I use the following code to repair the method:

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

This calls the original (flawed) method if pythonconfig fails to find the library.

mkfs
  • 346
  • 1
  • 6