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

  • Home
  • SEARCH
  • 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 9146305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:43:07+00:00 2026-06-17T10:43:07+00:00

I want to do following. Script has some python code as a string (saved

  • 0

I want to do following. Script has some python code as a string (saved in a variable) and is it possible to run that code ?

Well, I know one way, writing that string to a file & running it, but I don’t want that. Without creating any extra file, is it possible to run it ?

Here is a example :

let’s assume my python file has following content

#this is a main python file
content = ''' print 'hello!'
                   print 'this is from sub python code' '''
print 'from main python'

The content string has a python code & I want to run it. Is it possible ?

Hope I am clear. Thank you !

  • 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-06-17T10:43:09+00:00Added an answer on June 17, 2026 at 10:43 am

    I’ll say this up front: This is a terrible idea, and depending on the source of the string a serious security risk.

    That disclaimer out of the way, python has an exec function that executes a string containing python code. For example:

    exec("print 2+2")
    

    Edit: I originally used eval in my answer, which is useful for evaluating individual expressions, while exec can be used for more general execution of arbitrary python code in a string.

    Relevant docs:

    http://docs.python.org/2/reference/simple_stmts.html#exec

    http://docs.python.org/2/library/functions.html#eval

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

Sidebar

Related Questions

Well, let's say I have a php-script, that has some select box, and I
I want to get script's origin filename in global functions. I tried following code,
For the following code <script type=text/javascript> $(document).ready(function(){ $(#erase).click(function(){ confirm('Are you sure you want to
I wrote the following pre-commit script for SVN to validate that a user has
I want to write in a bash script a piece of code that checks
I want to load some variable from a text file into my batch script.
I would like to write a python script that addresses the following problem: I
I want to Make selenium script which move slider given on following site Example
I want to include external js file in my php script. I am following
I want the following code to execute when the myString is having either of

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.