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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:07:30+00:00 2026-06-14T19:07:30+00:00

Consider a python script: #### #Do some stuff# #### #Do stuff from separate file

  • 0

Consider a python script:

####
#Do some stuff#

####
#Do stuff from separate file

####
#Do other stuff

What it the best way to implement the middle bit (do stuff that is defined in another file)? I’ve been told that in C++ there’s a command that can achieve what I’m looking to do. Is there an equivalent in Python?

e.g. if A.py is:

print 'a'

### import contents of other file here

print 'm'

and B.py is:

print 'b'
print 'c'
print 'd'

then the desired output of A.py is:

a
b
c
d
m

B.py won’t actually contain print statements, rather variable assignments and simple flow control that won’t conflict with anything declared or defined in A.py. I understand that I could put the contents of B.py into a function, import this into A.py and call this function at the desired place. That would be fine if I wanted the contents of B.py to return some single value. But the contents of B.py may contain for example twenty variable assignments.

I guess then what I am really looking to do is not so much execute the contents of B.py within A.py, but moreover dynamically modify A.Py to contain, at some desired line in A.py, the contents of B.py. Then, obviously, execute the updated A.py.

Thoughts and tips much appreciated.

  • 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-14T19:07:31+00:00Added an answer on June 14, 2026 at 7:07 pm

    I don’t think this is a good programming practice but you can use execfile to execute all the code in another file.

    #a.py
    a = a + 5
    
    #b.py
    a = 10
    execfile("a.py")
    print a 
    

    If you run b.py it will print ’15’.

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

Sidebar

Related Questions

I need to have a Python CGI script do some stuff (a little bit
I am running some shell test scripts from a python script under Windows. The
I have a custom python script that monitors the call logs from a Nortel
If I have a Python script that requires at least a particular version of
Consider the following Python script, which uses SQLAlchemy and the Python multiprocessing module. This
I am making a python script that suggests new music to you, but for
What would anyone consider the most efficient way to merge two datasets using Python?
Here's a bit of a newbie Python question about instance variables. Consider the following
I have some python knowledge but I'd never consider myself particularly fluent in the
My python script does some heavy computation. To boost performance, it caches the computed

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.