Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 1 | .. highlightlang:: none |
| 2 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 3 | .. _using-on-general: |
| 4 | |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 5 | Command line and environment |
| 6 | ============================ |
| 7 | |
| 8 | The CPython interpreter scans the command line and the environment for various |
| 9 | settings. |
| 10 | |
| 11 | .. note:: |
| 12 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 13 | Other implementations' command line schemes may differ. See |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 14 | :ref:`implementations` for further resources. |
| 15 | |
| 16 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 17 | .. _using-on-cmdline: |
| 18 | |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 19 | Command line |
| 20 | ------------ |
| 21 | |
| 22 | When invoking Python, you may specify any of these options:: |
| 23 | |
Christian Heimes | 226679a | 2007-12-07 11:52:55 +0000 | [diff] [blame] | 24 | python [-bdEiOStuUvxX?] [-c command | -m module-name | script | - ] [args] |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 25 | |
| 26 | The most common use case is, of course, a simple invocation of a script:: |
| 27 | |
| 28 | python myscript.py |
| 29 | |
| 30 | |
| 31 | Interface options |
| 32 | ~~~~~~~~~~~~~~~~~ |
| 33 | |
| 34 | The interpreter interface resembles that of the UNIX shell: |
| 35 | |
| 36 | * When called with standard input connected to a tty device, it prompts for |
| 37 | commands and executes them until an EOF (an end-of-file character, you can |
| 38 | produce that with *Ctrl-D* on UNIX or *Ctrl-Z, Enter* on Windows) is read. |
| 39 | * When called with a file name argument or with a file as standard input, it |
| 40 | reads and executes a script from that file. |
| 41 | * When called with ``-c command``, it executes the Python statement(s) given as |
| 42 | *command*. Here *command* may contain multiple statements separated by |
| 43 | newlines. Leading whitespace is significant in Python statements! |
| 44 | * When called with ``-m module-name``, the given module is searched on the |
| 45 | Python module path and executed as a script. |
| 46 | |
| 47 | In non-interactive mode, the entire input is parsed before it is executed. |
| 48 | |
| 49 | An interface option terminates the list of options consumed by the interpreter, |
| 50 | all consecutive arguments will end up in :data:`sys.argv` -- note that the first |
| 51 | element, subscript zero (``sys.argv[0]``), is a string reflecting the program's |
| 52 | source. |
| 53 | |
| 54 | .. cmdoption:: -c <command> |
| 55 | |
| 56 | Execute the Python code in *command*. *command* can be one ore more |
| 57 | statements separated by newlines, with significant leading whitespace as in |
| 58 | normal module code. |
| 59 | |
| 60 | If this option is given, the first element of :data:`sys.argv` will be |
| 61 | ``"-c"``. |
| 62 | |
| 63 | |
| 64 | .. cmdoption:: -m <module-name> |
| 65 | |
| 66 | Search :data:`sys.path` for the named module and run the corresponding module |
| 67 | file as if it were executed with ``python modulefile.py`` as a script. |
| 68 | |
| 69 | Since the argument is a *module* name, you must not give a file extension |
| 70 | (``.py``). However, the ``module-name`` does not have to be a valid Python |
| 71 | identifer (e.g. you can use a file name including a hyphen). |
| 72 | |
| 73 | .. note:: |
| 74 | |
| 75 | This option cannot be used with builtin modules and extension modules |
| 76 | written in C, since they do not have Python module files. |
| 77 | |
| 78 | If this option is given, the first element of :data:`sys.argv` will be the |
| 79 | full path to the module file. |
| 80 | |
| 81 | Many standard library modules contain code that is invoked on their execution |
| 82 | as a script. An example is the :mod:`timeit` module:: |
| 83 | |
| 84 | python -mtimeit -s 'setup here' 'benchmarked code here' |
| 85 | python -mtimeit -h # for details |
| 86 | |
| 87 | .. seealso:: |
| 88 | :func:`runpy.run_module` |
| 89 | The actual implementation of this feature. |
| 90 | |
| 91 | :pep:`338` -- Executing modules as scripts |
| 92 | |
| 93 | |
| 94 | .. describe:: <script> |
| 95 | |
| 96 | Execute the Python code contained in *script*, which must be an (absolute or |
| 97 | relative) file name. |
| 98 | |
| 99 | If this option is given, the first element of :data:`sys.argv` will be the |
| 100 | script file name as given on the command line. |
| 101 | |
| 102 | |
| 103 | .. describe:: - |
| 104 | |
| 105 | Read commands from standard input (:data:`sys.stdin`). If standard input is |
| 106 | a terminal, :option:`-i` is implied. |
| 107 | |
| 108 | If this option is given, the first element of :data:`sys.argv` will be |
| 109 | ``"-"``. |
| 110 | |
| 111 | .. seealso:: |
| 112 | :ref:`tut-invoking` |
| 113 | |
| 114 | |
| 115 | If no script name is given, ``sys.argv[0]`` is an empty string (``""``). |
| 116 | |
| 117 | |
| 118 | Generic options |
| 119 | ~~~~~~~~~~~~~~~ |
| 120 | |
| 121 | .. cmdoption:: -? |
| 122 | -h |
| 123 | --help |
| 124 | |
| 125 | Print a short description of all command line options. |
| 126 | |
| 127 | |
| 128 | .. cmdoption:: -V |
| 129 | --version |
| 130 | |
| 131 | Print the Python version number and exit. Example output could be:: |
| 132 | |
| 133 | Python 2.5.1 |
| 134 | |
| 135 | |
| 136 | Miscellaneous options |
| 137 | ~~~~~~~~~~~~~~~~~~~~~ |
| 138 | |
Christian Heimes | 226679a | 2007-12-07 11:52:55 +0000 | [diff] [blame] | 139 | .. cmdoption:: -b |
| 140 | |
| 141 | Issue a warning when comparing str and bytes. Issue an error when the |
| 142 | option is given twice (:option:`-bb`). |
| 143 | |
| 144 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 145 | .. cmdoption:: -B |
| 146 | |
| 147 | If given, Python won't try to write ``.pyc`` or ``.pyo`` files on the |
| 148 | import of source modules. See also :envvar:`PYTHONDONTWRITEBYTECODE`. |
| 149 | |
| 150 | .. versionadded:: 2.6 |
| 151 | |
| 152 | |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 153 | .. cmdoption:: -d |
| 154 | |
| 155 | Turn on parser debugging output (for wizards only, depending on compilation |
| 156 | options). See also :envvar:`PYTHONDEBUG`. |
| 157 | |
| 158 | |
| 159 | .. cmdoption:: -E |
| 160 | |
| 161 | Ignore all :envvar:`PYTHON*` environment variables, e.g. |
| 162 | :envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, that might be set. |
| 163 | |
| 164 | |
| 165 | .. cmdoption:: -i |
| 166 | |
| 167 | When a script is passed as first argument or the :option:`-c` option is used, |
| 168 | enter interactive mode after executing the script or the command, even when |
| 169 | :data:`sys.stdin` does not appear to be a terminal. The |
| 170 | :envvar:`PYTHONSTARTUP` file is not read. |
| 171 | |
| 172 | This can be useful to inspect global variables or a stack trace when a script |
| 173 | raises an exception. See also :envvar:`PYTHONINSPECT`. |
| 174 | |
| 175 | |
| 176 | .. cmdoption:: -O |
| 177 | |
| 178 | Turn on basic optimizations. This changes the filename extension for |
| 179 | compiled (:term:`bytecode`) files from ``.pyc`` to ``.pyo``. See also |
| 180 | :envvar:`PYTHONOPTIMIZE`. |
| 181 | |
| 182 | |
| 183 | .. cmdoption:: -OO |
| 184 | |
| 185 | Discard docstrings in addition to the :option:`-O` optimizations. |
| 186 | |
| 187 | |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 188 | |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 189 | |
| 190 | Disable the import of the module :mod:`site` and the site-dependent |
| 191 | manipulations of :data:`sys.path` that it entails. |
| 192 | |
| 193 | |
| 194 | .. cmdoption:: -t |
| 195 | |
| 196 | Issue a warning when a source file mixes tabs and spaces for indentation in a |
| 197 | way that makes it depend on the worth of a tab expressed in spaces. Issue an |
| 198 | error when the option is given twice (:option:`-tt`). |
| 199 | |
| 200 | |
| 201 | .. cmdoption:: -u |
| 202 | |
| 203 | Force stdin, stdout and stderr to be totally unbuffered. On systems where it |
| 204 | matters, also put stdin, stdout and stderr in binary mode. |
| 205 | |
| 206 | Note that there is internal buffering in :meth:`file.readlines` and |
| 207 | :ref:`bltin-file-objects` (``for line in sys.stdin``) which is not influenced |
| 208 | by this option. To work around this, you will want to use |
| 209 | :meth:`file.readline` inside a ``while 1:`` loop. |
| 210 | |
| 211 | See also :envvar:`PYTHONUNBUFFERED`. |
| 212 | |
| 213 | |
| 214 | .. XXX should the -U option be documented? |
| 215 | |
| 216 | .. cmdoption:: -v |
| 217 | |
| 218 | Print a message each time a module is initialized, showing the place |
| 219 | (filename or built-in module) from which it is loaded. When given twice |
| 220 | (:option:`-vv`), print a message for each file that is checked for when |
| 221 | searching for a module. Also provides information on module cleanup at exit. |
| 222 | See also :envvar:`PYTHONVERBOSE`. |
| 223 | |
| 224 | |
| 225 | .. cmdoption:: -W arg |
| 226 | |
| 227 | Warning control. Python's warning machinery by default prints warning |
| 228 | messages to :data:`sys.stderr`. A typical warning message has the following |
| 229 | form:: |
| 230 | |
| 231 | file:line: category: message |
| 232 | |
| 233 | By default, each warning is printed once for each source line where it |
| 234 | occurs. This option controls how often warnings are printed. |
| 235 | |
| 236 | Multiple :option:`-W` options may be given; when a warning matches more than |
| 237 | one option, the action for the last matching option is performed. Invalid |
| 238 | :option:`-W` options are ignored (though, a warning message is printed about |
| 239 | invalid options when the first warning is issued). |
| 240 | |
| 241 | Warnings can also be controlled from within a Python program using the |
| 242 | :mod:`warnings` module. |
| 243 | |
| 244 | The simplest form of argument is one of the following action strings (or a |
| 245 | unique abbreviation): |
| 246 | |
| 247 | ``ignore`` |
| 248 | Ignore all warnings. |
| 249 | ``default`` |
| 250 | Explicitly request the default behavior (printing each warning once per |
| 251 | source line). |
| 252 | ``all`` |
| 253 | Print a warning each time it occurs (this may generate many messages if a |
| 254 | warning is triggered repeatedly for the same source line, such as inside a |
| 255 | loop). |
| 256 | ``module`` |
| 257 | Print each warning only only the first time it occurs in each module. |
| 258 | ``once`` |
| 259 | Print each warning only the first time it occurs in the program. |
| 260 | ``error`` |
| 261 | Raise an exception instead of printing a warning message. |
| 262 | |
| 263 | The full form of argument is:: |
| 264 | |
| 265 | action:message:category:module:line |
| 266 | |
| 267 | Here, *action* is as explained above but only applies to messages that match |
| 268 | the remaining fields. Empty fields match all values; trailing empty fields |
| 269 | may be omitted. The *message* field matches the start of the warning message |
| 270 | printed; this match is case-insensitive. The *category* field matches the |
| 271 | warning category. This must be a class name; the match test whether the |
| 272 | actual warning category of the message is a subclass of the specified warning |
| 273 | category. The full class name must be given. The *module* field matches the |
| 274 | (fully-qualified) module name; this match is case-sensitive. The *line* |
| 275 | field matches the line number, where zero matches all line numbers and is |
| 276 | thus equivalent to an omitted line number. |
| 277 | |
| 278 | .. seealso:: |
| 279 | |
| 280 | :pep:`230` -- Warning framework |
| 281 | |
| 282 | |
| 283 | .. cmdoption:: -x |
| 284 | |
| 285 | Skip the first line of the source, allowing use of non-Unix forms of |
| 286 | ``#!cmd``. This is intended for a DOS specific hack only. |
| 287 | |
| 288 | .. warning:: The line numbers in error messages will be off by one! |
| 289 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 290 | .. _using-on-envvars: |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 291 | |
| 292 | Environment variables |
| 293 | --------------------- |
| 294 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 295 | These environment variables influence Python's behavior. |
| 296 | |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 297 | .. envvar:: PYTHONHOME |
| 298 | |
| 299 | Change the location of the standard Python libraries. By default, the |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame^] | 300 | libraries are searched in :file:`{prefix}/lib/python{version}` and |
| 301 | :file:`{exec_prefix}/lib/python{version}`, where :file:`{prefix}` and |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 302 | :file:`{exec_prefix}` are installation-dependent directories, both defaulting |
| 303 | to :file:`/usr/local`. |
| 304 | |
| 305 | When :envvar:`PYTHONHOME` is set to a single directory, its value replaces |
| 306 | both :file:`{prefix}` and :file:`{exec_prefix}`. To specify different values |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame^] | 307 | for these, set :envvar:`PYTHONHOME` to :file:`{prefix}:{exec_prefix}`. |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | .. envvar:: PYTHONPATH |
| 311 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 312 | Augment the default search path for module files. The format is the same as |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 313 | the shell's :envvar:`PATH`: one or more directory pathnames separated by |
| 314 | colons. Non-existent directories are silently ignored. |
| 315 | |
| 316 | The default search path is installation dependent, but generally begins with |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame^] | 317 | :file:`{prefix}/lib/python{version}`` (see :envvar:`PYTHONHOME` above). It |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 318 | is *always* appended to :envvar:`PYTHONPATH`. |
| 319 | |
| 320 | If a script argument is given, the directory containing the script is |
| 321 | inserted in the path in front of :envvar:`PYTHONPATH`. The search path can |
| 322 | be manipulated from within a Python program as the variable :data:`sys.path`. |
| 323 | |
| 324 | |
| 325 | .. envvar:: PYTHONSTARTUP |
| 326 | |
| 327 | If this is the name of a readable file, the Python commands in that file are |
| 328 | executed before the first prompt is displayed in interactive mode. The file |
| 329 | is executed in the same namespace where interactive commands are executed so |
| 330 | that objects defined or imported in it can be used without qualification in |
| 331 | the interactive session. You can also change the prompts :data:`sys.ps1` and |
| 332 | :data:`sys.ps2` in this file. |
| 333 | |
| 334 | |
| 335 | .. envvar:: PYTHONY2K |
| 336 | |
| 337 | Set this to a non-empty string to cause the :mod:`time` module to require |
| 338 | dates specified as strings to include 4-digit years, otherwise 2-digit years |
| 339 | are converted based on rules described in the :mod:`time` module |
| 340 | documentation. |
| 341 | |
| 342 | |
| 343 | .. envvar:: PYTHONOPTIMIZE |
| 344 | |
| 345 | If this is set to a non-empty string it is equivalent to specifying the |
| 346 | :option:`-O` option. If set to an integer, it is equivalent to specifying |
| 347 | :option:`-O` multiple times. |
| 348 | |
| 349 | |
| 350 | .. envvar:: PYTHONDEBUG |
| 351 | |
| 352 | If this is set to a non-empty string it is equivalent to specifying the |
| 353 | :option:`-d` option. If set to an integer, it is equivalent to specifying |
| 354 | :option:`-d` multiple times. |
| 355 | |
| 356 | |
| 357 | .. envvar:: PYTHONINSPECT |
| 358 | |
| 359 | If this is set to a non-empty string it is equivalent to specifying the |
| 360 | :option:`-i` option. |
| 361 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 362 | This variable can also be modified by Python code using :data:`os.environ` |
| 363 | to force inspect mode on program termination. |
| 364 | |
Georg Brandl | 3c8ce77 | 2007-11-01 20:58:08 +0000 | [diff] [blame] | 365 | |
| 366 | .. envvar:: PYTHONUNBUFFERED |
| 367 | |
| 368 | If this is set to a non-empty string it is equivalent to specifying the |
| 369 | :option:`-u` option. |
| 370 | |
| 371 | |
| 372 | .. envvar:: PYTHONVERBOSE |
| 373 | |
| 374 | If this is set to a non-empty string it is equivalent to specifying the |
| 375 | :option:`-v` option. If set to an integer, it is equivalent to specifying |
| 376 | :option:`-v` multiple times. |
| 377 | |
| 378 | |
| 379 | .. envvar:: PYTHONCASEOK |
| 380 | |
| 381 | If this is set, Python ignores case in :keyword:`import` statements. This |
| 382 | only works on Windows. |
| 383 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 384 | |
| 385 | .. envvar:: PYTHONDONTWRITEBYTECODE |
| 386 | |
| 387 | If this is set, Python won't try to write ``.pyc`` or ``.pyo`` files on the |
| 388 | import of source modules. |
| 389 | |
| 390 | .. versionadded:: 2.6 |
| 391 | |
| 392 | |
| 393 | .. envvar:: PYTHONEXECUTABLE |
| 394 | |
| 395 | If this environment variable is set, ``sys.argv[0]`` will be set to its |
| 396 | value instead of the value got through the C runtime. Only works on |
| 397 | MacOS X. |
| 398 | |
| 399 | |
| 400 | Debug-mode variables |
| 401 | ~~~~~~~~~~~~~~~~~~~~ |
| 402 | |
| 403 | Setting these variables only has an effect in a debug build of Python, that is, |
| 404 | if Python was configured with the :option:`--with-pydebug` build option. |
| 405 | |
| 406 | .. envvar:: PYTHONTHREADDEBUG |
| 407 | |
| 408 | If set, Python will print debug threading debug info. |
| 409 | |
| 410 | .. versionchanged:: 2.6 |
| 411 | Previously, this variable was called ``THREADDEBUG``. |
| 412 | |
| 413 | .. envvar:: PYTHONDUMPREFS |
| 414 | |
| 415 | If set, Python will dump objects and reference counts still alive after |
| 416 | shutting down the interpreter. |
| 417 | |
| 418 | |
| 419 | .. envvar:: PYTHONMALLOCSTATS |
| 420 | |
| 421 | If set, Python will print memory allocation statistics every time a new |
| 422 | object arena is created, and on shutdown. |
| 423 | |