- Python Shortcuts, Commands, and Packages
- 4.2 Twenty-Two Programming Shortcuts
- 4.3 Running Python from the Command Line
- 4.4 Writing and Using Doc Strings
- 4.5 Importing Packages
- 4.6 A Guided Tour of Python Packages
- 4.7 Functions as First-Class Objects
- 4.8 Variable-Length Argument Lists
- 4.9 Decorators and Function Profilers
- 4.10 Generators
- 4.11 Accessing Command-Line Arguments
- Chapter 4 Summary
- Chapter 4 Questions for Review
- Chapter 4 Suggested Problems
4.3 Running Python from the Command Line
If you’ve been running Python programs from within IDLE—either as commands entered one at a time or as scripts—one way to improve execution speed is to run programs from a command line instead; in particular, doing so greatly speeds up the time it takes to execute calls to the print function.
Some of the quirks of command-line operation depend on which operating system you’re using. This section covers the two most common operating systems: Windows and Macintosh.
4.3.1 Running on a Windows-Based System
Windows systems, unlike Macintosh, usually do not come with a version of Python 2.0 preloaded, a practice that actually saves you a good deal of fuss as long as you install Python 3 yourself.
To use Python from the command line, first start the DOS Box application, which is present as a major application on all Windows systems. Python should be easily available because it should be placed in a directory that is part of the PATH setting. Checking this setting is easy to do while you’re running a Windows DOS Box.
In Windows, you can also check the PATH setting by opening the Control Panel, choose Systems, and select the Advanced tab. Then click Environment Variables.
You then should be able to run Python programs directly as long as they’re in your PATH. To run a program from the command line, enter python and the name of the source file (the main module), including the .py extension.
python test.py
4.3.2 Running on a Macintosh System
Macintosh systems often come with a version of Python already installed; unfortunately, on recent systems, the version is Python 2.0 and not Python 3.0.
To determine which version has been installed for command-line use, first bring up the Terminal application on your Macintosh system. You may need to first click the Launchpad icon.
You should find yourself in your default directory, whatever it is. You can determine which command-line version of Python you have by using the following command:
python -V
If the version of Python is 2.0+, you’ll get a message such as the following:
python 2.7.10
But if you’ve downloaded some version of Python 3.0, you should have that version of Python loaded as well. However, to run it, you’ll have to use the command python3 rather than python.
If you do have python3 loaded, you can verify the exact version from the command line as follows:
python3 -V python 3.7.0
For example, if the file test.py is in the current directory, and you want to compile it as a Python 3.0 program, then use the following command:
python3 test.py
The Python command (whether python or python3) has some useful variations. If you enter it with -h, the “help” flag, you get a printout on all the possible flags that you can use with the command, as well as relevant environment variables.
python3 -h
4.3.3 Using pip or pip3 to Download Packages
Some of the packages in this book require that you download and install the packages from the Internet before you use those packages. The first chapter that requires that is Chapter 12, which introduces the numpy package.
All the packages mentioned in this book are completely free of charge (as most packages for Python are). Even better, the pip utility—which is included with the Python 3 download—goes out and finds the package that you name; thus all you should need is an Internet connection!
On Windows-based systems, use the following command to download and install a desired package.
pip install package_name
The package name, incidentally, uses no file extension:
pip install numpy
On Macintosh systems, you may need to use the pip3 utility, which is download with Python 3 when you install it on your computer. (You may also have inherited a version of pip, but it will likely be out-of-date and unusable.)
pip3 install package_name