Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Other Command-Line Features

Python, primarily, is a programming language. However, Python is also a family of related programs which interpret the Python language. While we can generally assume that the Python language is the same as the Python interpreter, there are some subtleties that are features of the interpreter, separate from the language.

Generally, the CPython interpreter is the baseline against which others are compared, and from which others are derived. Other interpreters include Jython, Iron Python and Python for .Net.

The Python interpreter has a fairly simple command-line interface. We looked at it briefly in the section called “Script Mode”. In non-Windows environments, you can use the man command to see the full set of command-line options. In all cases, you can run python -h or python --help to get a summary of the options.

Generally there are four kinds of command-line options.

  • Identify The Program (-c, -m, -, file ). The -c option provides the Python program on the command line as a quoted string. This isn't terribly useful. However, we can use it for things like the following.

    python -c 'import sys; print sys.version'

    Note the rarely-used ; to terminate a statement.

    The -m option will locate a module on the PYTHONPATH and execute that module. This allows you to install a complete application in the Python library and execute the top-level "main program" script.

    As we noted in the section called “Script Mode”, the command-line argument to the Python interpreter is expected to be a Python program file. Additionally, we can provide a Python program on standard input and use python - to read and process that program.

  • Select the Division Operator Semantics (-Q). As we noted in the section called “Division Operators”, there are two senses for division. You can control the meaning of / using -Qnew and -Qold. You can also debug problems with -Qwarn or -Qwarnall. Rather than rely on -Qnew, you should include from __future__ import division in every program that uses the new // operator and the new sense of the / operator.

  • Optimization (-O). We can use -O and -OO to permit some optimization of the Python bytecode. This may lead to small performance improvements.

    Generally, there are two sources for performance improvements that are far more important than optimization. First, and most fundamentally, correct choices of data structures and algorithms have the most profound influence on performance. Second, modules can be written in C and use the Python API's. These C-language modules can dramatically improve performance, also.

  • Startup and Loading (-S, -E). There are several ways to control the way Python starts and which modules it loads.

    The -E option ignores all environment variables (PYTHONPATH is the most commonly used environment variable.)

    Ordinarily Python executes an implicit import site when it starts executing. The site module populates sys.path with standard locations for packages and modules. The -S option will suppress this behavior.

  • Debugging (-d, -i, -v, -u). Python has some additional debugging information that you can access with the -d option. The -i option will allow you to execute a script and then interact with the Python intrpreter. The -v option will display verbose information on the import processing.

    Sometimes it will help to remove the automatic buffering of standard output. If you use the -u option, mixed stderr and stdout streams may be easier to read.

  • Indentation Problem-Solving (-t, the TabNanny, and -x). The -t option gives warning on inconsistent use of tabs and spaces. The -tt option makes these warnings into errors, stopping your program from running.

    The -x option skips the first line of a file. This can be used for situations where the first line of a Python file can't be a simple #! line. If the first line can't be a comment to Python, this will skip that line.

There are a number of environment variables that Python uses. We'll look at just a few.

PYTHONPATH

This defines the set of directories searched for modules. This is in addition to the directories placed on to sys.path by the site module.

PYTHONSTARTUP

This file is executed when you start Python for interactive use. You can use the script executed at startup time to import useful modules, define handy functions or alter your working environment in other ways.


 
 
  Published under the terms of the Open Publication License Design by Interspire