Run Python Scripts on macOS

This section provides a tutorial example to verify Python environment supported on macOS computers and run a simple Python interactive scripting session.

macOS computers support Python out-of-the-box. You don't have to do any installation to run Python scripts. Here is what I tried on my macOS computer.

1. Start a Terminal window and try to run Python scripts interactively as shown below:

herong$ python --version
Python 2.7.10

herong$ python
Python 2.7.10 (default, Feb  6 2017, 23:53:20)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> print "Hello world!\n"
Hello world!

>>> exit()

Note that you need to call the "exit()" function to terminate the Python interactive session.

2. If you just want to run one line of Python code, you can specify it in the command option "-c" as shown below:

herong$ python -c 'print "Hello world!\n"'

Hello world!

3. If you have a Python script file, you can run it in non-interactive mode as command argument as shown below:

herong$ python hello.py

Hello world!

Table of Contents

 About This Book

 Running Python Code Online

Python on macOS Computers

Run Python Scripts on macOS

 Install New Versions of Python on macOS

 pip3 - Package Installer for Python 3

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB