summaryrefslogtreecommitdiffstats
path: root/Doc/faq/programming.rst
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2013-12-13 17:30:29 (GMT)
committerR David Murray <rdmurray@bitdance.com>2013-12-13 17:30:29 (GMT)
commit18701160cc67a3523e77d16879c8c9f8080f8685 (patch)
tree31f3d2afa908d5b6d767eb77095774d603930f5f /Doc/faq/programming.rst
parentca7ab7c7f10f27d8e3df3281c78180802d767358 (diff)
parentd913d9d54e6156a6998ef1f7009cc9ba8142d8a4 (diff)
downloadcpython-18701160cc67a3523e77d16879c8c9f8080f8685.zip
cpython-18701160cc67a3523e77d16879c8c9f8080f8685.tar.gz
cpython-18701160cc67a3523e77d16879c8c9f8080f8685.tar.bz2
Merge: #18036: update .pyc FAQ entry in light of PEP 3147.
Diffstat (limited to 'Doc/faq/programming.rst')
-rw-r--r--Doc/faq/programming.rst45
1 files changed, 27 insertions, 18 deletions
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index ac12da3..79c7289 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -1607,26 +1607,34 @@ Modules
How do I create a .pyc file?
----------------------------
-When a module is imported for the first time (or when the source is more recent
-than the current compiled file) a ``.pyc`` file containing the compiled code
-should be created in the same directory as the ``.py`` file.
-
-One reason that a ``.pyc`` file may not be created is permissions problems with
-the directory. This can happen, for example, if you develop as one user but run
-as another, such as if you are testing with a web server. Creation of a .pyc
-file is automatic if you're importing a module and Python has the ability
-(permissions, free space, etc...) to write the compiled module back to the
-directory.
+When a module is imported for the first time (or when the source file has
+changed since the current compiled file was created) a ``.pyc`` file containing
+the compiled code should be created in a ``__pycache__`` subdirectory of the
+directory containing the ``.py`` file. The ``.pyc`` file will have a
+filename that starts with the same name as the ``.py`` file, and ends with
+``.pyc``, with a middle component that depends on the particular ``python``
+binary that created it. (See :pep:`3147` for details.)
+
+One reason that a ``.pyc`` file may not be created is a permissions problem
+with the directory containing the source file, meaning that the ``__pycache__``
+subdirectory cannot be created. This can happen, for example, if you develop as
+one user but run as another, such as if you are testing with a web server.
+
+Unless the :envvar:`PYTHONDONTWRITEBYTECODE` environment variable is set,
+creation of a .pyc file is automatic if you're importing a module and Python
+has the ability (permissions, free space, etc...) to create a ``__pycache__``
+subdirectory and write the compiled module to that subdirectory.
Running Python on a top level script is not considered an import and no
``.pyc`` will be created. For example, if you have a top-level module
-``foo.py`` that imports another module ``xyz.py``, when you run ``foo``,
-``xyz.pyc`` will be created since ``xyz`` is imported, but no ``foo.pyc`` file
-will be created since ``foo.py`` isn't being imported.
+``foo.py`` that imports another module ``xyz.py``, when you run ``foo`` (by
+typing ``python foo.py`` as a shell command), a ``.pyc`` will be created for
+``xyz`` because ``xyz`` is imported, but no ``.pyc`` file will be created for
+``foo`` since ``foo.py`` isn't being imported.
-If you need to create ``foo.pyc`` -- that is, to create a ``.pyc`` file for a module
-that is not imported -- you can, using the :mod:`py_compile` and
-:mod:`compileall` modules.
+If you need to create a ``.pyc`` file for ``foo`` -- that is, to create a
+``.pyc`` file for a module that is not imported -- you can, using the
+:mod:`py_compile` and :mod:`compileall` modules.
The :mod:`py_compile` module can manually compile any module. One way is to use
the ``compile()`` function in that module interactively::
@@ -1634,8 +1642,9 @@ the ``compile()`` function in that module interactively::
>>> import py_compile
>>> py_compile.compile('foo.py') # doctest: +SKIP
-This will write the ``.pyc`` to the same location as ``foo.py`` (or you can
-override that with the optional parameter ``cfile``).
+This will write the ``.pyc`` to a ``__pycache__`` subdirectory in the same
+location as ``foo.py`` (or you can override that with the optional parameter
+``cfile``).
You can also automatically compile all files in a directory or directories using
the :mod:`compileall` module. You can do it from the shell prompt by running