Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Python for Beginners Running Python Scripts

Calling a Python script

When a Python script is written, the programmer doesn't go into the Python 'REPL' due to that nothing is being evaluated, correct? The programmer only wants to call the code that was written in the script and this can be done through the console?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

You can call a Python script by simply typing python script.py

Interactively, this would look like:

# show contents of foo.py
$ cat foo.py
name = 'Benjamin'
print(name)

# call python script
$ python foo.py 
Benjamin

# call python script then enter REPL using -i switch
$ python -i foo.py
Benjamin
>>> 
# inspect local variables
>>> name
'Benjamin'
>>> exit()

# enter REPL then call script
$ python
Python 3.4.0 (default, Jun 19 2015, 14:20:21) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo
Benjamin
# inspect name variable
>>> foo.name
'Benjamin'