diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -1631,16 +1631,18 @@ 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. +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 the ``__pycache__`` directory, relative +to the directory containing 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. +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__`` directory 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. 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__`` directory and write the compiled module to that directory. 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 @@ -1658,8 +1660,9 @@ >>> 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 the ``__pycache__`` directory 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