01-2 Running Environments of Python
01-2 Running Environments of Python
of Python
Licheng QU
School of Information Engineering
University of Changan
How You Run Python Programs
• The Interactive Prompt
• Python Files
• Clicking File Icons (on Windows)
• Module Imports and Reloads
• Using exec to Run Module Files
• IPython
• Other IDEs
• Other Launch Options
The Interactive Prompt
• Perhaps the simplest way to run Python programs is to type them at
Python’s interactive command line, sometimes called the
interactive prompt.
• Assuming the interpreter is installed as an executable program on
your system, the most platformneutral way to start an interactive
interpreter session is usually just to type python at your operating
system's prompt, without any arguments.
Running Code Interactively
>>>print('Hello world!')
>>>print(2 ** 8)
>>>hw='hello world!'
>>>2 ** 8
• you can route the output of a Python script to a file to save it for later
use or inspection by using special shell syntax:
% python script1.py > output.txt
Unix Executable Scripts (#!)
• In simple terms, Unix-style executable scripts are just normal text files
containing Python statements, but with two special properties:
1) Their first line is special. Scripts usually start with a line that begins
with the characters #! (often called “hash bang”), followed by the
path to the Python interpreter on your machine.
#!/usr/local/bin/python
print('The Bright Side ' + 'of Life...') # + means concatenate for strings
2) They usually have executable privileges. Script files are usually
marked as executable to tell the operating system that they may be run
as top-level programs.
chmod +x file.py
Module Imports and Reloads
• imports are also a way to launch programs.
• In simple terms, every file of Python source code whose name ends in
a .py extension is a module.
• The contents of a module are made available to the outside world
through its attributes.
• if you start an interactive session, you can run the script1.py file you
created earlier with a simple import:
>>> import script1
Module Imports and Reloads
• This works, but only once per session (really, process) by default.
After the first import, later imports do nothing, even if you change
and save the module's source file again in another window.
• If you really want to force Python to run the file again in the same
session without stopping and restarting the session, you need to
instead call the reload function available in the imp standard library
module:
>>> from imp import reload # Must load from module in 3.0
>>> reload(script1)
Using exec to Run Module Files
• The exec(open('module.py').read()) built-in function call is another
way to launch files from the interactive prompt without having to
import and later reload.
• Each exec runs the current version of the file, without requiring later
reloads.
>>> exec(open('script1.py').read())
IDEs
• IPython and Jupyter
• Eclipse and PyDev
• Pycharm
• Komodo
• NetBeans IDE for Python
• PythonWin
Python Installation
Anaconda Installation
Anaconda Navigator
PyCharm IDE
Discussion & Homework