How do I execute (not import) a python script from a python prompt?

I need to execute a Python script from an already started Python session, as if it were launched from the command line. It should be similar to doing source in bash or sh.

-------------Problems Reply------------

The builtin function execfile does this.

execfile(filename)

If you're running ipython (which I highly recommend for interactive python sessions), you can type:

%run filename

or

%run filename.py

to execute the module (rather than importing it). You'll get file-name completion, which is great for ReallyLongModuleName.py (not that you'd name your modules like that or anything).

Category:python Views:0 Time:2010-01-07

Related post

  • Run a python script from another python script, passing in args 2010-09-23

    This question already has an answer here: What is the best way to call a python script from another python script? 7 answers I want to run a python script from another python script. I want to pass variables like I would using the command line. For e

  • How to run a python script from another python script and get the returned status code? 2012-01-04

    I'm relatively new to both Python and bash. However, I am finding Python much more intuitive and easier than bash. I have a few bash scripts I have managed to cobble together, but I would like to replace them with Python scripts - for ease of mainten

  • executing a python script from another python script 2011-10-14

    I have a script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) + int(arg2) if __name__ == "__main__": import sys print foo(sys.argv[1], sys.argv[2])` I now want to make a script that can run the first script and write the output o

  • Execute python script inside a python script 2010-08-05

    I have a scenario where i want to dynamically generate a python script - inside my main python script - store it as a string and then when need be, execute this dynamically generated script from my main script. Is this possible, if so how? thanks ---

  • How to execute a Python script using the Python DLL? 2011-09-05

    I have a Python DLL already shipped and installed on the end user (guaranteed, and with a known version number). How can I use this DLL to execute some Python scripts without shipping the Python interpreter (again)? --------------Solutions-----------

  • Is it possible to generate and execute Python code in a Python script? [Dynamic Python code] 2011-11-22

    I am working on some reports (counts) and I have to fetch counts for different parameters. Pretty simple but tedious. A sample query for one parameter : qCountsEmployee = ( "select count(*) from %s where EmployeeName is not null" % (tablename) ) Coun

  • What is the best way to call a python script from another python script? 2009-07-27

    I have a script named test1.py which is not in a module. It just has code that should execute when the script itself is run. There are no functions, classes, methods etc. I have another script which runs as a service. I want to call test1.py from the

  • Python script opening a bash prompt terminating the script 2010-11-15

    I want to write a chroot wrapper in python. The script will be copying some files, setting up some other stuff and then executing chroot and should land me in a chroot shell. The tricky part is that I want no python processes running after I am in th

  • Get output of python script from within python script 2011-05-22

    printbob.py: import sys for arg in sys.argv: print arg getbob.py import subprocess #printbob.py will always be in root of getbob.py #a sample of sending commands to printbob.py is: #printboby.py arg1 arg2 arg3 (commands are seperated by spaces) print

  • call python with system() in R to run a python script emulating the python console 2012-04-14

    I want to pass a chunk of Python code to Python in R with something like system('python ...'), and I'm wondering if there is an easy way to emulate the python console in this case. For example, suppose the code is "print 'hello world'", how can I get

  • Redirect python script to another python script for validation of login credentials 2012-04-24

    I have a login python script in which i want to take the username and password and pass it to another python script which validates the username and password with the values in the database. If the user exists, it will create a cookie with the userna

  • calling one python script from another python script with arguments stored in a string 2014-04-08

    I am trying to call a Python script from another. I am using the subprocess-module and calling it like this: subprocess.call(["python","script.py","parameter_name parameter_value"], shell=False) The thing is when I call it this way it works fine for

  • How to spawn multiple python scripts from a python program? 2009-06-07

    Hi I want to spawn (fork?) multiple Python scripts from my program (written in Python too) My problem is that I want to dedicate one terminal to each script , because I'll gather their output using pexpect. I've tried using pexpect, os.execlp and os.

  • Terminate a python script from another python script 2010-06-16

    I've got a long running python script that I want to be able to end from another python script. Ideally what I'm looking for is some way of setting a process ID to the first script and being able to see if it is running or not via that ID from the se

  • how to run python script without typing 'python -' 2011-02-14

    I want to run a python script without explicitly having to call "python" every time in my shell. I've tried to add the shebang #!/path/to/python but this does not seem to work. Does anyone know a work around this? Many thanks. --------------Solutions

  • Run python script without the "python" keyword 2011-05-04

    How can I run a python script in Terminal on Mac without using the "python" keyword, without having to edit my existing python files? Right now I have to do this: python script.py What I like to do is this: script.py --------------Solutions----------

  • getting odd error when calling python script within another python script 2011-05-18

    I am getting an IOError when calling a python script(script2) within another python script(script1). Script 2 runs fine if called stand alone, however, if I call it from within script one, i get the following error. C:\>C:\Python32\python.exe R:\S

  • Python Scripts Are Opening Python 2.7 Interpreter 2012-04-27

    I've recently installed several Python packages on Kubuntu 12.04 (iPython, matplotlib, numpy, ipython-notebook, ipython-qtconsole, python-scipy). Now when I try and run a python script from the command line (./script.py) I get thrown into the python

  • Use Ruby on Rails to start Python Script / Hostgator Start Python on Boot 2012-01-20

    So I've run in to a snag on one of my ideas. I have a HostGator host, and I'd like it to start a Python script on boot whenever they do maintenance and such. Unfortunetly, there's no way to add items to HostGator's boot process (if there is, please m

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.160 (s). 11 q(s)