Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6248431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:03:23+00:00 2026-05-24T13:03:23+00:00

As part of a larger project, I’m trying to embed a Python interactive interpreter

  • 0

As part of a larger project, I’m trying to “embed” a Python interactive interpreter in a Ruby process. I’d like to be able to do something like the following:

$ irb
irb(main):001:0> pipe = IO.popen("python", "w+")
=> #<IO:0x7f3dba4977e0>
irb(main):002:0> pipe.puts "print 'hello'"
=> nil
irb(main):003:0> pipe.gets
=> 'hello\n'

Unfortunately, the gets seems to hang rather than return any kind of output from the Python process. I’ve tried variations of this procedure with open3, using mode r+ instead of w+, and a couple other minor options (python -u among them), with no success.

Is there a way to establish interactive communication with a Python shell from Ruby – in effect, to “wrap” the Python CLI? I’m using Ruby 1.8.7 (2010-06-23 patchlevel 299) and Python 2.6.6 on an x86_64 machine, though hopefully solutions will be portable(ish) across Python versions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:03:24+00:00Added an answer on May 24, 2026 at 1:03 pm

    popen doesn’t look like a terminal to python, so you are not running in interactive mode. You can force python to start in interactive mode with -i:

    IO.popen("python -i", "r+") do |py|
      while cmd = gets
        py.puts cmd
        puts py.gets
      end
    end
    

    You’ll probably have to do some work to remove the >>> prompt, etc.

    EDIT: here is the multi-line friendly version (I’m preserving the code is clear and answered the original question):

    IO.popen("python -i", "r+") do |py|
      loop do
        fds = IO.select [py, STDIN]
        fds.each do |(fd)|
          case fd
          when nil;    next
          when STDIN;  py.puts gets
          else;        puts py.gets
          end
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As part of a larger project I'm trying to implement a facility using JOGL
I'm trying to add something to a larger C++ project which is developed using
As part of a larger series of operations, I'm trying to take tokenized chunks
I'm developing a small webserver in C# as part of a larger project (the
As part of a larger project, I want the ability to take two bodies
As part of a larger project I'm working on, we're looking to integrate a
I need to do some word sense disambiguation as part of a larger project
It's a part of larger code base, which forces -Werror on gcc. This warning
As part of a larger web-app (using CakePHP), I'm putting together a simple blog
As part of a larger task performed in R run under windows, I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.