Let me begin with a bit on terminology:
Python is a programming language. It's "just" the programming language specification.
CPython is the reference implementation of the Python language. It's actually just one of several different Python interpreters. CPython itself works (let's call it an implementation detail) by translating (but you could also say compiling) the code in imported Python files/modules to bytecode and then executing that bytecode. It actually stores the translation as .pyc
files in the folder of that file) to make subsequent imports faster, but that's specific to CPython and can also be disabled.
PyCharm is an integrated development environment. However it requires to "Configure a Python Interpreter" to run Python code.
That means that PyCharm isn't responsible for creating .pyc
files. If you configured a non-CPython interpreter or used the environmental variable to disable the pyc
file creation there won't be any pyc files.
But if you used an appropriate CPython interpreter in PyCharm it will create .pyc
files for the files/modules you successfully imported. That means you actually have to import or otherwise run the Python files in your project to get the .pyc
files.
Actually the Python documentation contains a note about the "compiled" Python files:
To speed up loading modules, Python caches the compiled version of each module in the __pycache__
directory under the name module.version.pyc
, where the version encodes the format of the compiled file; it generally contains the Python version number. For example, in CPython release 3.3 the compiled version of spam.py would be cached as __pycache__/spam.cpython-33.pyc
. This naming convention allows compiled modules from different releases and different versions of Python to coexist.
Python checks the modification date of the source against the compiled version to see if it’s out of date and needs to be recompiled. This is a completely automatic process. Also, the compiled modules are platform-independent, so the same library can be shared among systems with different architectures.
Python does not check the cache in two circumstances. First, it always recompiles and does not store the result for the module that’s loaded directly from the command line. Second, it does not check the cache if there is no source module. To support a non-source (compiled only) distribution, the compiled module must be in the source directory, and there must not be a source module.
Some tips for experts:
- You can use the
-O
or -OO
switches on the Python command to reduce the size of a compiled module. The -O
switch removes assert statements, the -OO
switch removes both assert statements and doc strings. Since some programs may rely on having these available, you should only use this option if you know what you’re doing. “Optimized” modules have an opt-
tag and are usually smaller. Future releases may change the effects of optimization.
- A program doesn’t run any faster when it is read from a
.pyc
file than when it is read from a .py
file; the only thing that’s faster about .pyc
files is the speed with which they are loaded.
- The module
compileall
can create .pyc files for all modules in a directory.
- There is more detail on this process, including a flow chart of the decisions, in PEP 3147.