2018-07-04 09:06:45 +00:00
|
|
|
|
# Copyright (C) 2001-2018, Python Software Foundation
|
2018-07-04 09:08:42 +00:00
|
|
|
|
# For licence information, see README file.
|
2016-10-30 09:46:26 +00:00
|
|
|
|
#
|
|
|
|
|
msgid ""
|
|
|
|
|
msgstr ""
|
2019-12-05 22:15:54 +00:00
|
|
|
|
"Project-Id-Version: Python 3\n"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"Report-Msgid-Bugs-To: \n"
|
2021-11-29 13:13:01 +00:00
|
|
|
|
"POT-Creation-Date: 2021-11-27 10:27+0100\n"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"PO-Revision-Date: 2021-10-28 17:30+0200\n"
|
|
|
|
|
"Last-Translator: Jean Abou Samra <jean@abou-samra.fr>\n"
|
2018-07-04 09:14:25 +00:00
|
|
|
|
"Language-Team: FRENCH <traductions@lists.afpy.org>\n"
|
2017-05-23 22:40:56 +00:00
|
|
|
|
"Language: fr\n"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
"MIME-Version: 1.0\n"
|
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\n"
|
|
|
|
|
"Content-Transfer-Encoding: 8bit\n"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"X-Generator: Poedit 3.0\n"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
2021-09-24 08:20:01 +00:00
|
|
|
|
#: library/__main__.rst:2
|
|
|
|
|
msgid ":mod:`__main__` --- Top-level code environment"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr ":mod:`__main__` — Environnement d'exécution principal"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
2020-07-20 08:45:25 +00:00
|
|
|
|
#: library/__main__.rst:10
|
2016-10-30 09:46:26 +00:00
|
|
|
|
msgid ""
|
2021-09-24 08:20:01 +00:00
|
|
|
|
"In Python, the special name ``__main__`` is used for two important "
|
|
|
|
|
"constructs:"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"En Python, le nom ``__main__`` a une fonction particulière. Il intervient "
|
|
|
|
|
"dans deux cas :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:12
|
|
|
|
|
msgid ""
|
|
|
|
|
"the name of the top-level environment of the program, which can be checked "
|
|
|
|
|
"using the ``__name__ == '__main__'`` expression; and"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"c'est le nom de l'environnement d'exécution principal, ce qui donne lieu au "
|
|
|
|
|
"test courant ``__name__ == '__main__'`` ;"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
2020-07-20 08:45:25 +00:00
|
|
|
|
#: library/__main__.rst:14
|
2021-09-24 08:20:01 +00:00
|
|
|
|
msgid "the ``__main__.py`` file in Python packages."
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "c'est aussi le nom du fichier ``__main__.py`` dans les paquets Python."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:16
|
|
|
|
|
msgid ""
|
|
|
|
|
"Both of these mechanisms are related to Python modules; how users interact "
|
|
|
|
|
"with them and how they interact with each other. They are explained in "
|
|
|
|
|
"detail below. If you're new to Python modules, see the tutorial section :"
|
|
|
|
|
"ref:`tut-modules` for an introduction."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Les deux sont liés aux modules Python, à la manière de s'en servir en tant "
|
|
|
|
|
"qu'utilisateur et à la manière dont ils interagissent entre eux. Cette page "
|
|
|
|
|
"contient des détails sur les modules Python. Si vous ne les avez jamais "
|
|
|
|
|
"utilisés, commencez par la section qui leur est consacrée dans le "
|
|
|
|
|
"tutoriel : :ref:`tut-modules`."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:25
|
|
|
|
|
msgid "``__name__ == '__main__'``"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "``__name__ == '__main__'``"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:27
|
|
|
|
|
msgid ""
|
|
|
|
|
"When a Python module or package is imported, ``__name__`` is set to the "
|
|
|
|
|
"module's name. Usually, this is the name of the Python file itself without "
|
|
|
|
|
"the ``.py`` extension::"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Lorsqu'un module ou un paquet Python est importé, son attribut ``__name__`` "
|
|
|
|
|
"est défini à son nom, qui est la plupart du temps le nom du fichier qui le "
|
|
|
|
|
"contient sans l'extension ``.py`` :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:35
|
|
|
|
|
msgid ""
|
|
|
|
|
"If the file is part of a package, ``__name__`` will also include the parent "
|
|
|
|
|
"package's path::"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Si le fichier fait partie d'un paquet, ``__name__`` donne tout le chemin "
|
|
|
|
|
"d'accès :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:42
|
|
|
|
|
msgid ""
|
|
|
|
|
"However, if the module is executed in the top-level code environment, its "
|
|
|
|
|
"``__name__`` is set to the string ``'__main__'``."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"En revanche, si le module est exécuté dans l'environnement d'exécution "
|
|
|
|
|
"principal, la variable ``__name__`` vaut ``'__main__'``."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:46
|
|
|
|
|
msgid "What is the \"top-level code environment\"?"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "Qu'est-ce que l'« environnement d'exécution principal » ?"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:48
|
|
|
|
|
msgid ""
|
|
|
|
|
"``__main__`` is the name of the environment where top-level code is run. "
|
|
|
|
|
"\"Top-level code\" is the first user-specified Python module that starts "
|
|
|
|
|
"running. It's \"top-level\" because it imports all other modules that the "
|
|
|
|
|
"program needs. Sometimes \"top-level code\" is called an *entry point* to "
|
|
|
|
|
"the application."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"L'environnement principal a pour nom ``__main__``. Il s'agit du premier "
|
|
|
|
|
"module Python dont l'exécution a été demandée par l'utilisateur final. On le "
|
|
|
|
|
"qualifie de principal car c'est lui qui importe tous les autres modules "
|
|
|
|
|
"nécessités par le programme. On l'appelle parfois le point d'entrée de "
|
|
|
|
|
"l'application."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:53
|
|
|
|
|
msgid "The top-level code environment can be:"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "L'environnement principal peut prendre diverses formes :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:55
|
|
|
|
|
msgid "the scope of an interactive prompt::"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "l'environnement d'une invite de commande interactive :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:60
|
|
|
|
|
msgid "the Python module passed to the Python interpreter as a file argument:"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"le module passé directement en tant que fichier à la commande de "
|
|
|
|
|
"l'interpréteur Python :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:67
|
|
|
|
|
msgid ""
|
|
|
|
|
"the Python module or package passed to the Python interpreter with the :"
|
|
|
|
|
"option:`-m` argument:"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"le module ou paquet passé à l'interpréteur avec l'option :option:`-m` :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
2021-10-29 06:37:05 +00:00
|
|
|
|
# Minuscule car toujours dans la même énumération, ignorez l'avertissement que Poedit affiche à cause de la majuscule à « Python ».
|
2021-09-24 08:20:01 +00:00
|
|
|
|
#: library/__main__.rst:75
|
|
|
|
|
msgid "Python code read by the Python interpreter from standard input:"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "le code lu par l'interpréteur depuis l'entrée standard :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:86
|
2016-10-30 09:46:26 +00:00
|
|
|
|
msgid ""
|
2021-09-24 08:20:01 +00:00
|
|
|
|
"Python code passed to the Python interpreter with the :option:`-c` argument:"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "le code passé à l'interpréteur avec l'option :option:`-c` :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:97
|
|
|
|
|
msgid ""
|
|
|
|
|
"In each of these situations, the top-level module's ``__name__`` is set to "
|
|
|
|
|
"``'__main__'``."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Dans chacun de ces cas, l'attribut ``__name__`` du module principal est mis "
|
|
|
|
|
"à ``'__main__'``."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:100
|
|
|
|
|
msgid ""
|
|
|
|
|
"As a result, a module can discover whether or not it is running in the top-"
|
|
|
|
|
"level environment by checking its own ``__name__``, which allows a common "
|
|
|
|
|
"idiom for conditionally executing code when the module is not initialized "
|
|
|
|
|
"from an import statement::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Un module peut donc savoir s'il est exécuté dans l'environnement principal "
|
|
|
|
|
"en vérifiant son ``__name__``, ce qui permet typiquement d'exécuter du code "
|
|
|
|
|
"lorsque le module est initialisé d'une manière autre que l'importation ::"
|
2016-10-30 09:46:26 +00:00
|
|
|
|
|
2021-09-24 08:20:01 +00:00
|
|
|
|
#: library/__main__.rst:111
|
|
|
|
|
msgid ""
|
|
|
|
|
"For a more detailed look at how ``__name__`` is set in all situations, see "
|
|
|
|
|
"the tutorial section :ref:`tut-modules`."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Pour plus de détails sur la manière dont ``__name__`` est défini dans les "
|
|
|
|
|
"divers cas, voir la section :ref:`tut-modules` dans le tutoriel."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:239
|
|
|
|
|
msgid "Idiomatic Usage"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "Utilisation idiomatique"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:118
|
2021-11-29 13:13:01 +00:00
|
|
|
|
#, fuzzy
|
2021-09-24 08:20:01 +00:00
|
|
|
|
msgid ""
|
|
|
|
|
"Some modules contain code that is intended for script use only, like parsing "
|
2021-11-29 13:13:01 +00:00
|
|
|
|
"command-line arguments or fetching data from standard input. If a module "
|
|
|
|
|
"like this was imported from a different module, for example to unit test it, "
|
|
|
|
|
"the script code would unintentionally execute as well."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Il arrive qu'un module contienne du code qui ne doit s'exécuter que lorsque "
|
|
|
|
|
"le module est utilisé comme script. On peut penser à l'analyse des arguments "
|
|
|
|
|
"passés en ligne de commande, ou bien à la lecture de l'entrée standard. Il "
|
|
|
|
|
"ne faudrait pas que ces opérations soient effectuées lorsque le module est "
|
|
|
|
|
"importé depuis un autre module, comme pour les tests."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:123
|
|
|
|
|
msgid ""
|
|
|
|
|
"This is where using the ``if __name__ == '__main__'`` code block comes in "
|
|
|
|
|
"handy. Code within this block won't run unless the module is executed in the "
|
|
|
|
|
"top-level environment."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"C'est dans ces situations que sert la construction ``if __name__ == "
|
|
|
|
|
"'__main__'``. Le code mis à l'intérieur du bloc n'est exécuté que si le "
|
|
|
|
|
"module est dans l'environnement principal."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:127
|
|
|
|
|
msgid ""
|
|
|
|
|
"Putting as few statements as possible in the block below ``if __name___ == "
|
|
|
|
|
"'__main__'`` can improve code clarity and correctness. Most often, a "
|
|
|
|
|
"function named ``main`` encapsulates the program's primary behavior::"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Il vaut mieux mettre aussi peu de code que possible sous le ``if __name__ == "
|
|
|
|
|
"'__main__'`` pour garder le code clair et limiter les risques d'erreur. La "
|
|
|
|
|
"plupart du temps, on écrit une fonction ``main`` qui contient tout le code "
|
|
|
|
|
"spécifique à l'utilisation comme script :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:151
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that if the module didn't encapsulate code inside the ``main`` function "
|
|
|
|
|
"but instead put it directly within the ``if __name__ == '__main__'`` block, "
|
|
|
|
|
"the ``phrase`` variable would be global to the entire module. This is error-"
|
|
|
|
|
"prone as other functions within the module could be unintentionally using "
|
|
|
|
|
"the global variable instead of a local name. A ``main`` function solves "
|
|
|
|
|
"this problem."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Si, dans le module ci-dessus, le code de ``main`` était placé directement "
|
|
|
|
|
"dans le ``if __name__ == '__main__'``, la variable ``phrase`` serait "
|
|
|
|
|
"globale, et d'autres fonctions pourraient s'en servir par erreur à la place "
|
|
|
|
|
"d'une variable locale. Encapsuler le code dans la fonction ``main`` évite "
|
|
|
|
|
"cet ennui."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:158
|
|
|
|
|
msgid ""
|
|
|
|
|
"Using a ``main`` function has the added benefit of the ``echo`` function "
|
|
|
|
|
"itself being isolated and importable elsewhere. When ``echo.py`` is "
|
|
|
|
|
"imported, the ``echo`` and ``main`` functions will be defined, but neither "
|
|
|
|
|
"of them will be called, because ``__name__ != '__main__'``."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"De plus, la fonction ``echo`` est elle-même séparée du reste, et on peut "
|
|
|
|
|
"l'importer dans un autre module. Le fait d'importer ``echo.py`` définit les "
|
|
|
|
|
"fonctions ``main`` et ``echo``, mais n'en appelle aucune, puisque "
|
|
|
|
|
"``__name__ != '__main__'``."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:165
|
|
|
|
|
msgid "Packaging Considerations"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "Considérations liées à l'empaquetage"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:167
|
|
|
|
|
msgid ""
|
|
|
|
|
"``main`` functions are often used to create command-line tools by specifying "
|
|
|
|
|
"them as entry points for console scripts. When this is done, `pip <https://"
|
|
|
|
|
"pip.pypa.io/>`_ inserts the function call into a template script, where the "
|
|
|
|
|
"return value of ``main`` is passed into :func:`sys.exit`. For example::"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Les fonctions ``main`` servent souvent à créer des outils qui s'exécutent en "
|
|
|
|
|
"ligne de commande. Les scripts sont ajoutés au système à l'aide de points "
|
|
|
|
|
"d'entrée, qui demandent à `pip <https://pip.pypa.io/>`_ de créer un "
|
|
|
|
|
"exécutable. Il le fait en insérant un appel à la fonction à l'intérieur d'un "
|
|
|
|
|
"modèle prédéfini où la valeur qu'elle renvoie est passée directement à :func:"
|
|
|
|
|
"`sys.exit` :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:175
|
|
|
|
|
msgid ""
|
|
|
|
|
"Since the call to ``main`` is wrapped in :func:`sys.exit`, the expectation "
|
|
|
|
|
"is that your function will return some value acceptable as an input to :func:"
|
|
|
|
|
"`sys.exit`; typically, an integer or ``None`` (which is implicitly returned "
|
|
|
|
|
"if your function does not have a return statement)."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Puisque l'appel de ``main`` est encapsulé dans :func:`sys.exit`, ``main`` "
|
|
|
|
|
"doit renvoyer une valeur qui convienne comme argument à :func:`sys.exit`, "
|
|
|
|
|
"par exemple un code de retour sous forme d'entier. La valeur ``None`` est "
|
|
|
|
|
"également acceptée, et c'est d'ailleurs celle que renvoie la fonction si "
|
|
|
|
|
"elle se termine sans rencontrer d'instruction ``return``."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:180
|
2016-10-30 09:46:26 +00:00
|
|
|
|
msgid ""
|
2021-09-24 08:20:01 +00:00
|
|
|
|
"By proactively following this convention ourselves, our module will have the "
|
|
|
|
|
"same behavior when run directly (i.e. ``python3 echo.py``) as it will have "
|
|
|
|
|
"if we later package it as a console script entry-point in a pip-installable "
|
|
|
|
|
"package."
|
2016-10-30 09:46:26 +00:00
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Il est utile de se conformer à cette convention. De cette manière, le module "
|
|
|
|
|
"se comporte de la même manière s'il est distribué comme script à l'aide des "
|
|
|
|
|
"points d'entrée que lorsqu'il est exécuté directement (avec ``python3 echo."
|
|
|
|
|
"py``)."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:185
|
|
|
|
|
msgid ""
|
|
|
|
|
"In particular, be careful about returning strings from your ``main`` "
|
|
|
|
|
"function. :func:`sys.exit` will interpret a string argument as a failure "
|
|
|
|
|
"message, so your program will have an exit code of ``1``, indicating "
|
|
|
|
|
"failure, and the string will be written to :data:`sys.stderr`. The ``echo."
|
|
|
|
|
"py`` example from earlier exemplifies using the ``sys.exit(main())`` "
|
|
|
|
|
"convention."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"En particulier, mieux vaut éviter de renvoyer une chaîne de caractères "
|
|
|
|
|
"depuis la fonction ``main``. En effet, si :func:`sys.exit` reçoit une "
|
|
|
|
|
"chaîne, elle l'interprète comme un message d'erreur, qu'elle affiche sur :"
|
|
|
|
|
"data:`sys.stderr` avant de terminer le programme avec le code de retour 1 "
|
|
|
|
|
"(erreur). L'exemple de ``echo.py`` ci-dessus montre la pratique recommandée."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:193
|
|
|
|
|
msgid ""
|
|
|
|
|
"`Python Packaging User Guide <https://packaging.python.org/>`_ contains a "
|
|
|
|
|
"collection of tutorials and references on how to distribute and install "
|
|
|
|
|
"Python packages with modern tools."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Le `guide de l'empaquetage en Python <https://packaging.python.org/>`_ (en "
|
|
|
|
|
"anglais) contient plusieurs tutoriels et documents de référence autour de la "
|
|
|
|
|
"distribution et de l'installation de paquets Python avec des outils modernes."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:199
|
|
|
|
|
msgid "``__main__.py`` in Python Packages"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "Le fichier ``__main__.py`` dans les paquets Python"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:201
|
|
|
|
|
msgid ""
|
|
|
|
|
"If you are not familiar with Python packages, see section :ref:`tut-"
|
|
|
|
|
"packages` of the tutorial. Most commonly, the ``__main__.py`` file is used "
|
|
|
|
|
"to provide a command-line interface for a package. Consider the following "
|
|
|
|
|
"hypothetical package, \"bandclass\":"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Si vous n'êtes pas familier des paquets Python, lisez :ref:`tut-packages` "
|
|
|
|
|
"dans le tutoriel. Un fichier ``__main__.py`` permet à un paquet de définir "
|
|
|
|
|
"une interface en ligne de commande. Prenons pour exemple un paquet nommé "
|
|
|
|
|
"*bandclass* :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:213
|
|
|
|
|
msgid ""
|
|
|
|
|
"``__main__.py`` will be executed when the package itself is invoked directly "
|
|
|
|
|
"from the command line using the :option:`-m` flag. For example:"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Le fichier ``__main__.py`` qu'il contient s'exécute lorsque le paquet est "
|
|
|
|
|
"appelé depuis la ligne de commande avec l'option :option:`-m` de "
|
|
|
|
|
"l'interpréteur, comme ceci :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:220
|
|
|
|
|
msgid ""
|
|
|
|
|
"This command will cause ``__main__.py`` to run. How you utilize this "
|
|
|
|
|
"mechanism will depend on the nature of the package you are writing, but in "
|
|
|
|
|
"this hypothetical case, it might make sense to allow the teacher to search "
|
|
|
|
|
"for students::"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Cette commande lance l'exécution de ``__main__.py``. Il vous appartient, en "
|
|
|
|
|
"tant que concepteur du paquet, de déterminer ce qu'elle doit faire. Dans "
|
|
|
|
|
"notre exemple, elle pourrait rechercher un étudiant dans une base de "
|
|
|
|
|
"données ::"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:233
|
2021-11-29 13:13:01 +00:00
|
|
|
|
#, fuzzy
|
2021-09-24 08:20:01 +00:00
|
|
|
|
msgid ""
|
|
|
|
|
"Note that ``from .student import search_students`` is an example of a "
|
2021-11-29 13:13:01 +00:00
|
|
|
|
"relative import. This import style can be used when referencing modules "
|
2021-09-24 08:20:01 +00:00
|
|
|
|
"within a package. For more details, see :ref:`intra-package-references` in "
|
|
|
|
|
"the :ref:`tut-modules` section of the tutorial."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Remarquez l'importation ``from .student import search_students``. Le point "
|
|
|
|
|
"avant ``student`` sert à rendre le chemin ``student`` relatif à la position "
|
|
|
|
|
"du module qui l'importe. Pour plus d'informations, voir :ref:`intra-package-"
|
|
|
|
|
"references` dans la section :ref:`tut-modules` du tutoriel."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:241
|
|
|
|
|
msgid ""
|
|
|
|
|
"The contents of ``__main__.py`` typically isn't fenced with ``if __name__ == "
|
|
|
|
|
"'__main__'`` blocks. Instead, those files are kept short, functions to "
|
|
|
|
|
"execute from other modules. Those other modules can then be easily unit-"
|
|
|
|
|
"tested and are properly reusable."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"En général, on ne met pas le code de ``__main__.py`` dans un bloc ``if "
|
|
|
|
|
"__name__ == '__main__'``. Il vaut mieux définir toutes les fonctions utiles "
|
|
|
|
|
"dans d'autres modules et réduire la taille du fichier ``__main__.py`` au "
|
|
|
|
|
"minimum. Il est alors plus facile de tester et réutiliser ces autres modules."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:246
|
|
|
|
|
msgid ""
|
|
|
|
|
"If used, an ``if __name__ == '__main__'`` block will still work as expected "
|
|
|
|
|
"for a ``__main__.py`` file within a package, because its ``__name__`` "
|
|
|
|
|
"attribute will include the package's path if imported::"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Cependant, un ``if __name__ == '__main__'``, s'il est présent dans le "
|
|
|
|
|
"``__main__.py``, fonctionne correctement. En effet, si ``__main__.py`` est "
|
|
|
|
|
"importé depuis autre module, son attribut ``__name__`` contient, avant "
|
|
|
|
|
"``__main__``, le nom du paquet dont il fait partie :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:254
|
|
|
|
|
msgid ""
|
|
|
|
|
"This won't work for ``__main__.py`` files in the root directory of a .zip "
|
|
|
|
|
"file though. Hence, for consistency, minimal ``__main__.py`` like the :mod:"
|
|
|
|
|
"`venv` one mentioned above are preferred."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Malgré tout, cela ne fonctionne pas pour les fichiers ``__main__.py`` à la "
|
|
|
|
|
"racine d'une archive ZIP. Aussi est-il préférable d'écrire des ``__main__."
|
|
|
|
|
"py`` dans le style minimal de celui de :mod:`venv` mentionné ci-dessous."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:260
|
|
|
|
|
msgid ""
|
|
|
|
|
"See :mod:`venv` for an example of a package with a minimal ``__main__.py`` "
|
|
|
|
|
"in the standard library. It doesn't contain a ``if __name__ == '__main__'`` "
|
|
|
|
|
"block. You can invoke it with ``python3 -m venv [directory]``."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Le module :mod:`venv` de la bibliothèque standard est un exemple de paquet "
|
|
|
|
|
"contenant un fichier ``__main__.py`` minimal, sans ``if __name__ == "
|
|
|
|
|
"'__main__'``. Il s'appelle avec la commande :samp:`python3 -m venv {dossier}"
|
|
|
|
|
"`."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:264
|
|
|
|
|
msgid ""
|
|
|
|
|
"See :mod:`runpy` for more details on the :option:`-m` flag to the "
|
|
|
|
|
"interpreter executable."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"La documentation du module :mod:`runpy` fournit une description complète de "
|
|
|
|
|
"l'option :option:`-m` de l'interpréteur."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:267
|
|
|
|
|
msgid ""
|
|
|
|
|
"See :mod:`zipapp` for how to run applications packaged as *.zip* files. In "
|
|
|
|
|
"this case Python looks for a ``__main__.py`` file in the root directory of "
|
|
|
|
|
"the archive."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Le module :mod:`zipapp` exécute des applications emballées dans une archive "
|
|
|
|
|
"ZIP. Dans ce cas, l'interpréteur recherche un fichier ``__main__.py`` à la "
|
|
|
|
|
"racine de l'archive."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:274
|
|
|
|
|
msgid "``import __main__``"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "``import __main__``"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:276
|
|
|
|
|
msgid ""
|
|
|
|
|
"Regardless of which module a Python program was started with, other modules "
|
|
|
|
|
"running within that same program can import the top-level environment's "
|
|
|
|
|
"scope (:term:`namespace`) by importing the ``__main__`` module. This "
|
|
|
|
|
"doesn't import a ``__main__.py`` file but rather whichever module that "
|
|
|
|
|
"received the special name ``'__main__'``."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Quel que soit le module principal d'un programme, les autres modules peuvent "
|
|
|
|
|
"accéder à l':term:`espace de nommage <namespace>` dans lequel il s'exécute "
|
|
|
|
|
"en important le module spécial ``__main__``. Celui-ci ne correspond pas "
|
|
|
|
|
"forcément à un fichier ``__main__.py``. Il s'agit simplement du module qui a "
|
|
|
|
|
"reçu le nom ``'__main__'``."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:282
|
|
|
|
|
msgid "Here is an example module that consumes the ``__main__`` namespace::"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "Voici un exemple d'utilisation du module ``__main__`` :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:300
|
|
|
|
|
msgid "Example usage of this module could be as follows::"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "Ce code s'utilise comme ceci :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:319
|
|
|
|
|
msgid "Now, if we started our program, the result would look like this:"
|
2021-10-29 06:37:05 +00:00
|
|
|
|
msgstr "Le programme ci-dessus donne la sortie :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:326
|
|
|
|
|
msgid ""
|
|
|
|
|
"The exit code of the program would be 1, indicating an error. Uncommenting "
|
|
|
|
|
"the line with ``my_name = \"Dinsdale\"`` fixes the program and now it exits "
|
|
|
|
|
"with status code 0, indicating success:"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Son code de retour est 1, ce qui signifie une erreur. En supprimant la "
|
|
|
|
|
"marque de commentaire en début de ligne ``my_name = \"Dinsdale\"``, le "
|
|
|
|
|
"programme est corrigé, et renvoie au système le code 0 car il n'y a plus "
|
|
|
|
|
"d'erreur :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:335
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that importing ``__main__`` doesn't cause any issues with "
|
|
|
|
|
"unintentionally running top-level code meant for script use which is put in "
|
|
|
|
|
"the ``if __name__ == \"__main__\"`` block of the ``start`` module. Why does "
|
|
|
|
|
"this work?"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"On pourrait s'attendre à un problème au moment de l'importation de "
|
|
|
|
|
"``__main__`` : cela ne provoque-t-il pas l'exécution anticipée du code de "
|
|
|
|
|
"script sous ``if __name__ == '__main__'`` dans le module principal "
|
|
|
|
|
"``start`` ?"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:339
|
|
|
|
|
msgid ""
|
|
|
|
|
"Python inserts an empty ``__main__`` module in :attr:`sys.modules` at "
|
|
|
|
|
"interpreter startup, and populates it by running top-level code. In our "
|
|
|
|
|
"example this is the ``start`` module which runs line by line and imports "
|
|
|
|
|
"``namely``. In turn, ``namely`` imports ``__main__`` (which is really "
|
|
|
|
|
"``start``). That's an import cycle! Fortunately, since the partially "
|
|
|
|
|
"populated ``__main__`` module is present in :attr:`sys.modules`, Python "
|
|
|
|
|
"passes that to ``namely``. See :ref:`Special considerations for __main__ "
|
|
|
|
|
"<import-dunder-main>` in the import system's reference for details on how "
|
|
|
|
|
"this works."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"En fait, le déroulement de l'exécution est le suivant : au lancement de "
|
|
|
|
|
"l'interpréteur, un module ``__main__`` initialement vide est inséré dans :"
|
|
|
|
|
"attr:`sys.modules`. Il se remplit au fur et à mesure de l'exécution du code "
|
|
|
|
|
"principal. Dans notre exemple, le module principal est ``start``, qui "
|
|
|
|
|
"s'exécute ligne par ligne et en vient à importer ``namely``. Or ``namely``, "
|
|
|
|
|
"à son tour, importe ``__main__``, c'est-à-dire en fait ``start``. On a donc "
|
|
|
|
|
"une importation cyclique. Puisque le module ``__main__`` est déjà présent "
|
|
|
|
|
"dans :attr:`sys.modules`, bien qu'encore incomplet, il est directement passé "
|
|
|
|
|
"à ``namely`` sans réimportation. La section :ref:`import-dunder-main` dans "
|
|
|
|
|
"le document de référence du système d'importation explique plus avant ce "
|
|
|
|
|
"fonctionnement."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:348
|
|
|
|
|
msgid ""
|
|
|
|
|
"The Python REPL is another example of a \"top-level environment\", so "
|
|
|
|
|
"anything defined in the REPL becomes part of the ``__main__`` scope::"
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"L'interpréteur interactif est un autre environnement d'exécution principal "
|
|
|
|
|
"possible. Toute variable qui y est définie appartient à l'espace de nommage "
|
|
|
|
|
"``__main__`` :"
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:364
|
|
|
|
|
msgid ""
|
|
|
|
|
"Note that in this case the ``__main__`` scope doesn't contain a ``__file__`` "
|
|
|
|
|
"attribute as it's interactive."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Dans ce cas, il n'y a pas de variable ``__file__``, puisque cela n'a pas de "
|
|
|
|
|
"sens dans le mode interactif."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#: library/__main__.rst:367
|
|
|
|
|
msgid ""
|
|
|
|
|
"The ``__main__`` scope is used in the implementation of :mod:`pdb` and :mod:"
|
|
|
|
|
"`rlcompleter`."
|
|
|
|
|
msgstr ""
|
2021-10-29 06:37:05 +00:00
|
|
|
|
"Le module ``__main__`` est notamment employé dans les implémentations de :"
|
|
|
|
|
"mod:`pdb` et :mod:`rlcompleter`."
|
2021-09-24 08:20:01 +00:00
|
|
|
|
|
|
|
|
|
#~ msgid ""
|
|
|
|
|
#~ "``'__main__'`` is the name of the scope in which top-level code executes. "
|
|
|
|
|
#~ "A module's __name__ is set equal to ``'__main__'`` when read from "
|
|
|
|
|
#~ "standard input, a script, or from an interactive prompt."
|
|
|
|
|
#~ msgstr ""
|
|
|
|
|
#~ "``'__main__'`` est le nom du *scope* dans lequel le code s'exécute en "
|
|
|
|
|
#~ "premier. Le nom d'un module (son *__name__*) vaut ``'__main__'`` "
|
|
|
|
|
#~ "lorsqu'il est lu de l'entrée standard, lorsque c'est un script, ou une "
|
|
|
|
|
#~ "invite interactive."
|
|
|
|
|
|
|
|
|
|
#~ msgid ""
|
|
|
|
|
#~ "For a package, the same effect can be achieved by including a ``__main__."
|
|
|
|
|
#~ "py`` module, the contents of which will be executed when the module is "
|
|
|
|
|
#~ "run with ``-m``."
|
|
|
|
|
#~ msgstr ""
|
|
|
|
|
#~ "Pour un paquet, le même effet peut être obtenu en utilisant un module "
|
|
|
|
|
#~ "``__main__.py``, son contenu sera exécuté si le paquet est lancé via ``-"
|
|
|
|
|
#~ "m``."
|