blob: b113c8f4489832442d4f7e31e1f209167fd2ecb3 [file] [log] [blame]
Georg Brandl59d121a2007-10-20 18:08:14 +00001.. highlightlang:: none
2
Georg Brandl87983f22007-12-01 23:12:45 +00003.. _using-on-general:
4
Georg Brandl1cddfed2007-10-20 18:33:20 +00005Command line and environment
6============================
Georg Brandl59d121a2007-10-20 18:08:14 +00007
8The CPython interpreter scans the command line and the environment for various
9settings.
10
11.. note::
12
Georg Brandlcbcfe4f2007-12-03 19:57:02 +000013 Other implementations' command line schemes may differ. See
Georg Brandl59d121a2007-10-20 18:08:14 +000014 :ref:`implementations` for further resources.
15
16
Georg Brandl87983f22007-12-01 23:12:45 +000017.. _using-on-cmdline:
18
Georg Brandl59d121a2007-10-20 18:08:14 +000019Command line
20------------
21
22When invoking Python, you may specify any of these options::
23
24 python [-dEiOQStuUvxX3?] [-c command | -m module-name | script | - ] [args]
25
26The most common use case is, of course, a simple invocation of a script::
27
28 python myscript.py
29
30
31Interface options
32~~~~~~~~~~~~~~~~~
33
34The 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
47In non-interactive mode, the entire input is parsed before it is executed.
48
49An interface option terminates the list of options consumed by the interpreter,
50all consecutive arguments will end up in :data:`sys.argv` -- note that the first
51element, subscript zero (``sys.argv[0]``), is a string reflecting the program's
52source.
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 .. versionchanged:: 2.5
94 The module name can now include packages.
95
96
97.. describe:: <script>
98
99 Execute the Python code contained in *script*, which must be an (absolute or
100 relative) file name.
101
102 If this option is given, the first element of :data:`sys.argv` will be the
103 script file name as given on the command line.
104
105
106.. describe:: -
107
108 Read commands from standard input (:data:`sys.stdin`). If standard input is
109 a terminal, :option:`-i` is implied.
110
111 If this option is given, the first element of :data:`sys.argv` will be
112 ``"-"``.
113
114 .. seealso::
115 :ref:`tut-invoking`
116
117
118If no script name is given, ``sys.argv[0]`` is an empty string (``""``).
119
120
121Generic options
122~~~~~~~~~~~~~~~
123
124.. cmdoption:: -?
125 -h
126 --help
127
128 Print a short description of all command line options.
129
130 .. versionadded:: 2.5
131 The ``--help`` variant.
132
133
134.. cmdoption:: -V
135 --version
136
137 Print the Python version number and exit. Example output could be::
138
139 Python 2.5.1
140
141 .. versionadded:: 2.5
142 The ``--version`` variant.
143
144
145Miscellaneous options
146~~~~~~~~~~~~~~~~~~~~~
147
Georg Brandl2da0fce2008-01-07 17:09:35 +0000148.. cmdoption:: -B
149
150 If given, Python won't try to write ``.pyc`` or ``.pyo`` files on the
151 import of source modules. See also :envvar:`PYTHONDONTWRITEBYTECODE`.
152
153 .. versionadded:: 2.6
154
155
Georg Brandl59d121a2007-10-20 18:08:14 +0000156.. cmdoption:: -d
157
158 Turn on parser debugging output (for wizards only, depending on compilation
159 options). See also :envvar:`PYTHONDEBUG`.
160
161
162.. cmdoption:: -E
163
Georg Brandlc5004f32007-10-20 19:05:45 +0000164 Ignore all :envvar:`PYTHON*` environment variables, e.g.
Georg Brandl35073332007-10-20 19:08:36 +0000165 :envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, that might be set.
Georg Brandl59d121a2007-10-20 18:08:14 +0000166
Georg Brandlc5004f32007-10-20 19:05:45 +0000167 .. versionadded:: 2.2
Georg Brandl59d121a2007-10-20 18:08:14 +0000168
169
170.. cmdoption:: -i
171
172 When a script is passed as first argument or the :option:`-c` option is used,
173 enter interactive mode after executing the script or the command, even when
174 :data:`sys.stdin` does not appear to be a terminal. The
175 :envvar:`PYTHONSTARTUP` file is not read.
176
177 This can be useful to inspect global variables or a stack trace when a script
178 raises an exception. See also :envvar:`PYTHONINSPECT`.
179
180
181.. cmdoption:: -O
182
183 Turn on basic optimizations. This changes the filename extension for
Georg Brandl5e52db02007-10-21 10:45:46 +0000184 compiled (:term:`bytecode`) files from ``.pyc`` to ``.pyo``. See also
Georg Brandl59d121a2007-10-20 18:08:14 +0000185 :envvar:`PYTHONOPTIMIZE`.
186
187
188.. cmdoption:: -OO
189
190 Discard docstrings in addition to the :option:`-O` optimizations.
191
192
193.. cmdoption:: -Q <arg>
194
195 Division control. The argument must be one of the following:
196
197 ``old``
198 division of int/int and long/long return an int or long (*default*)
199 ``new``
200 new division semantics, i.e. division of int/int and long/long returns a
201 float
202 ``warn``
203 old division semantics with a warning for int/int and long/long
204 ``warnall``
205 old division semantics with a warning for all uses of the division operator
206
207 .. seealso::
208 :file:`Tools/scripts/fixdiv.py`
209 for a use of ``warnall``
210
211 :pep:`238` -- Changing the division operator
212
213
214.. cmdoption:: -S
215
216 Disable the import of the module :mod:`site` and the site-dependent
217 manipulations of :data:`sys.path` that it entails.
218
219
220.. cmdoption:: -t
221
222 Issue a warning when a source file mixes tabs and spaces for indentation in a
223 way that makes it depend on the worth of a tab expressed in spaces. Issue an
224 error when the option is given twice (:option:`-tt`).
225
226
227.. cmdoption:: -u
228
229 Force stdin, stdout and stderr to be totally unbuffered. On systems where it
230 matters, also put stdin, stdout and stderr in binary mode.
231
Georg Brandlf4ef23f2007-10-30 17:51:18 +0000232 Note that there is internal buffering in :meth:`file.readlines` and
Georg Brandl59d121a2007-10-20 18:08:14 +0000233 :ref:`bltin-file-objects` (``for line in sys.stdin``) which is not influenced
234 by this option. To work around this, you will want to use
Georg Brandlf4ef23f2007-10-30 17:51:18 +0000235 :meth:`file.readline` inside a ``while 1:`` loop.
Georg Brandl59d121a2007-10-20 18:08:14 +0000236
237 See also :envvar:`PYTHONUNBUFFERED`.
238
239
240.. XXX should the -U option be documented?
241
242.. cmdoption:: -v
243
244 Print a message each time a module is initialized, showing the place
245 (filename or built-in module) from which it is loaded. When given twice
246 (:option:`-vv`), print a message for each file that is checked for when
247 searching for a module. Also provides information on module cleanup at exit.
248 See also :envvar:`PYTHONVERBOSE`.
249
250
251.. cmdoption:: -W arg
252
253 Warning control. Python's warning machinery by default prints warning
254 messages to :data:`sys.stderr`. A typical warning message has the following
255 form::
256
257 file:line: category: message
258
259 By default, each warning is printed once for each source line where it
260 occurs. This option controls how often warnings are printed.
261
262 Multiple :option:`-W` options may be given; when a warning matches more than
263 one option, the action for the last matching option is performed. Invalid
264 :option:`-W` options are ignored (though, a warning message is printed about
265 invalid options when the first warning is issued).
266
267 Warnings can also be controlled from within a Python program using the
268 :mod:`warnings` module.
269
270 The simplest form of argument is one of the following action strings (or a
271 unique abbreviation):
272
273 ``ignore``
274 Ignore all warnings.
275 ``default``
276 Explicitly request the default behavior (printing each warning once per
277 source line).
278 ``all``
279 Print a warning each time it occurs (this may generate many messages if a
280 warning is triggered repeatedly for the same source line, such as inside a
281 loop).
282 ``module``
283 Print each warning only only the first time it occurs in each module.
284 ``once``
285 Print each warning only the first time it occurs in the program.
286 ``error``
287 Raise an exception instead of printing a warning message.
288
289 The full form of argument is::
290
291 action:message:category:module:line
292
293 Here, *action* is as explained above but only applies to messages that match
294 the remaining fields. Empty fields match all values; trailing empty fields
295 may be omitted. The *message* field matches the start of the warning message
296 printed; this match is case-insensitive. The *category* field matches the
297 warning category. This must be a class name; the match test whether the
298 actual warning category of the message is a subclass of the specified warning
299 category. The full class name must be given. The *module* field matches the
300 (fully-qualified) module name; this match is case-sensitive. The *line*
301 field matches the line number, where zero matches all line numbers and is
302 thus equivalent to an omitted line number.
303
304 .. seealso::
305
306 :pep:`230` -- Warning framework
307
308
309.. cmdoption:: -x
310
311 Skip the first line of the source, allowing use of non-Unix forms of
312 ``#!cmd``. This is intended for a DOS specific hack only.
313
314 .. warning:: The line numbers in error messages will be off by one!
315
316
317.. cmdoption:: -3
318
Georg Brandl87983f22007-12-01 23:12:45 +0000319 Warn about Python 3.x incompatibilities. Among these are:
320
321 * :meth:`dict.has_key`
322 * :func:`apply`
323 * :func:`callable`
324 * :func:`coerce`
325 * :func:`execfile`
326 * :func:`reduce`
327 * :func:`reload`
Georg Brandl59d121a2007-10-20 18:08:14 +0000328
329 .. versionadded:: 2.6
330
331
Georg Brandl59d121a2007-10-20 18:08:14 +0000332
Georg Brandl87983f22007-12-01 23:12:45 +0000333.. _using-on-envvars:
Georg Brandl59d121a2007-10-20 18:08:14 +0000334
335Environment variables
336---------------------
337
338.. envvar:: PYTHONHOME
339
340 Change the location of the standard Python libraries. By default, the
341 libraries are searched in :file:`{prefix}/lib/python<version>` and
342 :file:`{exec_prefix}/lib/python<version>`, where :file:`{prefix}` and
343 :file:`{exec_prefix}` are installation-dependent directories, both defaulting
344 to :file:`/usr/local`.
345
346 When :envvar:`PYTHONHOME` is set to a single directory, its value replaces
347 both :file:`{prefix}` and :file:`{exec_prefix}`. To specify different values
348 for these, set :envvar:`PYTHONHOME` to :file:`{prefix}:{exec_prefix}``.
349
350
351.. envvar:: PYTHONPATH
352
353 Augments the default search path for module files. The format is the same as
354 the shell's :envvar:`PATH`: one or more directory pathnames separated by
355 colons. Non-existent directories are silently ignored.
356
357 The default search path is installation dependent, but generally begins with
358 :file:`{prefix}/lib/python<version>`` (see :envvar:`PYTHONHOME` above). It
359 is *always* appended to :envvar:`PYTHONPATH`.
360
361 If a script argument is given, the directory containing the script is
362 inserted in the path in front of :envvar:`PYTHONPATH`. The search path can
363 be manipulated from within a Python program as the variable :data:`sys.path`.
364
365
366.. envvar:: PYTHONSTARTUP
367
368 If this is the name of a readable file, the Python commands in that file are
369 executed before the first prompt is displayed in interactive mode. The file
Georg Brandla7395032007-10-21 12:15:05 +0000370 is executed in the same namespace where interactive commands are executed so
Georg Brandl59d121a2007-10-20 18:08:14 +0000371 that objects defined or imported in it can be used without qualification in
372 the interactive session. You can also change the prompts :data:`sys.ps1` and
373 :data:`sys.ps2` in this file.
374
375
376.. envvar:: PYTHONY2K
377
378 Set this to a non-empty string to cause the :mod:`time` module to require
379 dates specified as strings to include 4-digit years, otherwise 2-digit years
380 are converted based on rules described in the :mod:`time` module
381 documentation.
382
383
384.. envvar:: PYTHONOPTIMIZE
385
386 If this is set to a non-empty string it is equivalent to specifying the
387 :option:`-O` option. If set to an integer, it is equivalent to specifying
388 :option:`-O` multiple times.
389
390
391.. envvar:: PYTHONDEBUG
392
393 If this is set to a non-empty string it is equivalent to specifying the
394 :option:`-d` option. If set to an integer, it is equivalent to specifying
395 :option:`-d` multiple times.
396
397
398.. envvar:: PYTHONINSPECT
399
400 If this is set to a non-empty string it is equivalent to specifying the
401 :option:`-i` option.
402
403
404.. envvar:: PYTHONUNBUFFERED
405
406 If this is set to a non-empty string it is equivalent to specifying the
407 :option:`-u` option.
408
409
410.. envvar:: PYTHONVERBOSE
411
412 If this is set to a non-empty string it is equivalent to specifying the
413 :option:`-v` option. If set to an integer, it is equivalent to specifying
414 :option:`-v` multiple times.
415
416
417.. envvar:: PYTHONCASEOK
418
419 If this is set, Python ignores case in :keyword:`import` statements. This
420 only works on Windows.
421
Georg Brandl2da0fce2008-01-07 17:09:35 +0000422
423.. envvar:: PYTHONDONTWRITEBYTECODE
424
425 If given, Python won't try to write ``.pyc`` or ``.pyo`` files on the
426 import of source modules.
427
428 .. versionadded:: 2.6