68

I have a demo file: test.py. In the Windows Console I can run the file with: C:\>test.py

How can I execute the file in the Python Shell instead?

asked Sep 14, 2011 at 18:09
2

6 Answers 6

133

Use execfile for Python 2:

>>> execfile('C:\\test.py')

Use exec for Python 3

>>> exec(open("C:\\test.py").read())
Yusuf K.
4,2601 gold badge36 silver badges71 bronze badges
answered Sep 14, 2011 at 18:11
Sign up to request clarification or add additional context in comments.

7 Comments

in the version 3 (my version) the equivalent is: exec(open("C:\\test.py").read()). thanks!
@loops I'm getting: SyntaxError: (Unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX excape. What does this mean? I tried changing the encoding of my .py document to Unicode but that didn't have any effect.
@loops --Okay I just learned about \U unicode escapes. Apparetly you avoid his miscommunication by duplicating all backslashes followed by U's. But this produces an OSError 22 invalid argument 'c:\\users\username\\desktop\\test.py' which is weird because I only duplicated the backslash in \Users. Why does the error show that I duplicated all the backslashes except for the one followed by username?
@Musixauce3000 Please post a new question.
add an 'r' at the beginning if you get the SyntaxError: (Unicode error) 'unicodeescape' codec... Therefore it will be, exec(open(r"C:\\test.py").read()).
|
56

If you're wanting to run the script and end at a prompt (so you can inspect variables, etc), then use:

python -i test.py

That will run the script and then drop you into a Python interpreter.

answered Sep 14, 2011 at 18:12

2 Comments

How about if my script is not in a file?
@DimitriKopriwa do you mean if it is coming in through a buffer or over the network?
21

It depends on what is in test.py. The following is an appropriate structure:

# suppose this is your 'test.py' file
def main():
 """This function runs the core of your program"""
 print("running main")
if __name__ == "__main__":
 # if you call this script from the command line (the shell) it will
 # run the 'main' function
 main()

If you keep this structure, you can run it like this in the command line (assume that $ is your command-line prompt):

$ python test.py
$ # it will print "running main"

If you want to run it from the Python shell, then you simply do the following:

>>> import test
>>> test.main() # this calls the main part of your program

There is no necessity to use the subprocess module if you are already using Python. Instead, try to structure your Python files in such a way that they can be run both from the command line and the Python interpreter.

answered Sep 14, 2011 at 18:18

Comments

6

For newer version of python:

exec(open(filename).read())
answered Jun 3, 2017 at 11:16

Comments

6

If you want to avoid writing all of this everytime, you can define a function :

def run(filename):
 exec(open(filename).read())

and then call it

run('filename.py')
wjandrea
34k10 gold badges69 silver badges106 bronze badges
answered Aug 6, 2018 at 11:42

1 Comment

This doesn't work for my use case since it doesn't import the variables. I'm not quite sure why not though.
2

From the same folder, you can do:

import test
answered Sep 14, 2011 at 18:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.