- 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函数作为一类对象
- 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 theprintfunction.
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, enterpython和the name of the source file (the main module), including the.pyextension.
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 commandpython3rather thanpython.
If you do havepython3loaded, you can verify the exact version from the command line as follows:
python3 -Vpython 3.7.0
For example, if the filetest.py在当前目录中,你想compile it as a Python 3.0 program, then use the following command:
python3 test.py
The Python command (whetherpythonorpython3) 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 thenumpypackage.
All the packages mentioned in this book are completely free of charge (as most packages for Python are). Even better, thepiputility—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 installpackage_name
The package name, incidentally, uses no file extension:
pip install numpy
On Macintosh systems, you may need to use thepip3utility, 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 installpackage_name