Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d913d9d5
Commit
d913d9d5
authored
Dec 13, 2013
by
R David Murray
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#18036: update .pyc FAQ entry in light of PEP 3147.
Initial patch by Phil Connell.
parent
c9362cf8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
18 deletions
+27
-18
Doc/faq/programming.rst
Doc/faq/programming.rst
+27
-18
No files found.
Doc/faq/programming.rst
View file @
d913d9d5
...
@@ -1607,26 +1607,34 @@ Modules
...
@@ -1607,26 +1607,34 @@ Modules
How do I create a .pyc file?
How do I create a .pyc file?
----------------------------
----------------------------
When a module is imported for the first time (or when the source is more recent
When a module is imported for the first time (or when the source file has
than the current compiled file) a ``.pyc`` file containing the compiled code
changed since the current compiled file was created) a ``.pyc`` file containing
should be created in the same directory as the ``.py`` file.
the compiled code should be created in a ``__pycache__`` subdirectory of the
directory containing the ``.py`` file. The ``.pyc`` file will have a
One reason that a ``.pyc`` file may not be created is permissions problems with
filename that starts with the same name as the ``.py`` file, and ends with
the directory. This can happen, for example, if you develop as one user but run
``.pyc``, with a middle component that depends on the particular ``python``
as another, such as if you are testing with a web server. Creation of a .pyc
binary that created it. (See :pep:`3147` for details.)
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
One reason that a ``.pyc`` file may not be created is a permissions problem
directory.
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
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
``.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``,
``foo.py`` that imports another module ``xyz.py``, when you run ``foo`` (by
``xyz.pyc`` will be created since ``xyz`` is imported, but no ``foo.pyc`` file
typing ``python foo.py`` as a shell command), a ``.pyc`` will be created for
will be created since ``foo.py`` isn't being imported.
``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
If you need to create
a ``.pyc`` file for ``foo`` -- that is, to create a
that is not imported -- you can, using the :mod:`py_compile` and
``.pyc`` file for a module that is not imported -- you can, using the
:mod:`compileall` modules.
:mod:`
py_compile` and :mod:`
compileall` modules.
The :mod:`py_compile` module can manually compile any module. One way is to use
The :mod:`py_compile` module can manually compile any module. One way is to use
the ``compile()`` function in that module interactively::
the ``compile()`` function in that module interactively::
...
@@ -1634,8 +1642,9 @@ the ``compile()`` function in that module interactively::
...
@@ -1634,8 +1642,9 @@ the ``compile()`` function in that module interactively::
>>> import py_compile
>>> import py_compile
>>> py_compile.compile('foo.py') # doctest: +SKIP
>>> py_compile.compile('foo.py') # doctest: +SKIP
This will write the ``.pyc`` to the same location as ``foo.py`` (or you can
This will write the ``.pyc`` to a ``__pycache__`` subdirectory in the same
override that with the optional parameter ``cfile``).
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
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
the :mod:`compileall` module. You can do it from the shell prompt by running
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment