Running and Quitting
Overview
Teaching: 5 min
Exercises: 0 minQuestions
How can I run Python programs?
Objectives
Run the
ipythoncommand prompt.Write a python file.
Execute a python file, both in the script and out.
Running an ipython session
-
Ipython is a common and very powerful way to test small pieces of python code.
-
It is also the basis for many other python tools such as Jupyter and is emulated in many other environments, such as IDEs.
-
To run
ipython, first enter your terminal, then simply type:
$ ipython
and you should see a prompt like this, but more colorful:
Python 3.6.6 (default, Sep 19 2018, 12:51:03)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:
- To exit the
ipython, typeexitand hit enter.
Creating an executable python file
-
Like shell scripts, you can save your code in a file
-
Now create a directory somewhere called
swc-python -
Open up
atom, either by clicking on the icon or typingatomin your terminal. -
Now, in your terminal, create a new directory under
swc-pythoncalledscripts. -
In atom, create a new file, and then save it as
test.pyin thescriptsdirectory. -
This is a python script file. Note that
.pyindicats a python file, but this is convention, not requirement. -
In your terminal, navigate to the
scriptsdirectory. - You can now run the script using
$ python test.py - Similarly, if you start up
ipythonagain, from withinipythonyou can doIn [1]: run test.pywhich will run all the code in
test.pyas though entered intoipython.
Key Points
Python scripts are plain text files.
Use the
ipythonfor editing and running Python.