6

I want to know if there is a way to integrate a library written in Python into my Rails application. I have always used gems to implement outside libraries so I have no idea how to do this(yet).

Is it possible to use this Python library?

4
  • Are you on Heroku? Important, because there are additional steps. Commented Jun 1, 2013 at 0:23
  • @Narfanator, Yes I'm on Heroku! But I might be changing to a different server later if it is necessary. Commented Jun 1, 2013 at 0:31
  • 1
    As a sanity check, stop and think about what you're asking. A Python library is written in Python, not Ruby. The two languages have no similarities allowing one to run code from the other, so there's no way to call a library from one with the other directly. What CAN happen is to spin up a sub-shell that runs the interpreter of the other, and THAT runs the library in question. Once it's running it's possible to communicate between the two in various ways, but you won't have the same integration you'd get talking directly to a Ruby gem or class. Commented Jun 1, 2013 at 2:02
  • Thanks! It seems there are ways to achieve this, but it makes more sense to only use Ruby libraries on ROR applications. Commented Jun 2, 2013 at 11:52

1 Answer 1

5

This is mostly what you want:

Calling Python from Ruby

I've looked into this question before and, excepting the Heroku deploy, the answer was "easily! check this out". (On a fun note, there's a gem to let you embed Perl code in a Ruby file)

However, while I can't seem to find it right now, I remember reading that deploying both to Heroku required a custom buildpac, since one won't normally include the binaries necessary for the other.

Sign up to request clarification or add additional context in comments.

5 Comments

Is this possible with an entire library, not just a single script? Do I need to manually include the entire library in a folder in lib?
Hmm. AFAIK, yes, but that's not a weird thing, since that's basically how python (and ruby, and JS, and Go...) all do it anyway. Something weird might also happen with the python imports?
Any idea how to go about trying this for a library like Spacy.io? In other words: once I stick the entire library into /lib, what would I do next...?
Hmm. In a Ruby gem, there's usually one Ruby file that loads the rest (via require statement). If Python does it the same way, look for that file, and try to load it via the methods in the answer. Poking back into that answer, you might skip the whole "load into Ruby" and go straight to the Apache client thingy. Sorry, it's been a long time since I poked at this :P
No worries! Good response. How would you install Spacy? I understand that one script imports the rest, but don’t understand how I’d install the Spacy library inside rails.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.