0% found this document useful (0 votes)
3 views

01-2 Running Environments of Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

01-2 Running Environments of Python

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Development Environment

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

exit() or Ctrl-Z to exit


Why the Interactive Prompt?
• Experimenting
Because code is executed immediately, the interactive prompt is a
perfect place to experiment with the language and will be used often
to demonstrate smaller examples.
• Testing on the fly
Besides serving as a tool for experimenting while you’re learning the
language, the interactive interpreter is also an ideal place to test code
you’ve written in files. You can import your module files interactively
and run tests on the tools they define by typing calls at the interactive
prompt.
Using the Interactive Prompt
• Type Python commands only.
• print statements are required only in files.
• Don't indent at the interactive prompt (yet).
• Watch out for prompt changes for compound statements.
• Terminate compound statements at the interactive prompt with a
blank line.
• The interactive prompt runs one statement at a time.
Entering multiline statements
• For compound statements, though, remember that you must submit
a blank line to terminate the statement and make it run before you
can type the next statement.

>>> for x in 'Python':


... print(x) <== Press Enter twice here to make this loop run
...
Python File
• Open your favorite text editor (e.g., vi or Notepad), and type the
following statements into a new text file named script1.py:

# A first Python script


import sys # Load a library module
print(sys.platform)
print(2 ** 100) # Raise 2 to a power
x = 'Python!'
print(x * 8) # String repetition
Running Files with Command Lines
• Once you’ve saved this text file, you can ask Python to run it by
listing its full filename as the first argument to a python command,
typed at the system shell prompt:
% python script1.py

• 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

What methods have we learned to run Python code?

You might also like