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 7584805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:00:41+00:00 2026-05-30T19:00:41+00:00

I have written a small C program that embeds Python. I’m setting it up

  • 0

I have written a small C program that embeds Python. I’m setting it up correctly using Py_Initialize() and Py_Finalize(), and am able to run scripts either using PyRun_SimpleString or PyRun_SimpleFile. However, I don’t know how mimic the behavior of Python’s own interpreter when printing variables.

Specifically:

a = (1, 2, 3)
print a

Works fine for me: it prints out (1, 2, 3)

However:

a = (1, 2, 3)
a

Prints out nothing at all. In Python’s own interpreter, this would print out (1, 2, 3) as well. How can I make my code do what users would expect and print out the value?

Thanks in advance!

  • 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-30T19:00:43+00:00Added an answer on May 30, 2026 at 7:00 pm

    To run the interpreters interactive loop, you should use the function PyRun_InteractiveLoop(). Otherwise, your code will behave as if it were written in a Python script file, not entered interactively.

    Edit: Here’s the full code of a simple interactive interpreter:

    #include <Python.h>
    
    int main()
    {
        Py_Initialize();
        PyRun_InteractiveLoop(stdin, "<stdin>");
        Py_Finalize();
    }
    

    Edit2: Implementing a full interactive interpreter in a GUI is a bit of a project. Probably the easiest way to get it right is to write a basic terminal emulator connected to a pseudo-terminal device, and use the above code on that device. This will automatically get all subtleties right.

    If your aim isn’t a full-blown interactive editor, an option might be to use PyRun_String() with Py_single_input as start token. This will allow you to run some Python code as in the interactive interpreter, and if that code happened to be a single expression that doesn’t evaluate to None, a representation of its value is printed — to stdout of course. Here is some example code (without error checking for simplicity):

    #include <Python.h>
    
    int main()
    {
        PyObject *main, *d;
        Py_Initialize();
        main = PyImport_AddModule("__main__");
        d = PyModule_GetDict(main);
        PyRun_String("a = (1, 2, 3)", Py_single_input, d, d);
        PyRun_String("a", Py_single_input, d, d);
        Py_Finalize();
    }
    

    This will print (1, 2, 3).

    There are still a lot of problems:

    • No error handling and traceback printing.
    • No “incremental input” for block commands like in the interactive interpreter. The input needs to be complete.
    • Output to stdout.
    • If multiple lines of input are given, nothing is printed.

    To really replicate the behaviour of the interactive interpreter is not easy. That’s why my initial recommendation was to write a basic terminal emulator in your GUI, which shouldn’t be too hard — or maybe there’s even one available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say I have written a small program that reads file_A and file_B as
I have written a small program using Borland's C++ builder, and along the way,
I have written a very small C# program, that uses a very small SQL
I have a written a program in Python 3 and are using Sphinx to
I have written a small GUI based vb.net program that speaks to embedded devices
I have written a small 16-bit assembly program that writes some values in some
As a Christmas gift I have written a small program in Java to calculate
I have visual studio 2008. I have a written a small C program. I
I have written a small chat app that uses mysql + php to facilitate
I have written a small app that puts my bluetooth in discoverable mode for

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.