What Do the Python File Extensions, .Pyc .Pyd .Pyo Stand For

What do the python file extensions, .pyc .pyd .pyo stand for?

  1. .py: This is normally the input source code that you've written.
  2. .pyc: This is the compiled bytecode. If you import a module, python will build a *.pyc file that contains the bytecode to make importing it again later easier (and faster).
  3. .pyo: This was a file format used before Python 3.5 for *.pyc files that were created with optimizations (-O) flag. (see the note below)
  4. .pyd: This is basically a windows dll file. http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll

Also for some further discussion on .pyc vs .pyo, take a look at: http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html (I've copied the important part below)

  • When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in ‘.pyo’ files. The optimizer currently doesn't help much; it only removes assert statements. When -O is used, all bytecode is optimized; .pyc files are ignored and .py files are compiled to optimized bytecode.
  • Passing two -O flags to the Python interpreter (-OO) will cause the bytecode compiler to perform optimizations that could in some rare cases result in malfunctioning programs. Currently only __doc__ strings are removed from the bytecode, resulting in more compact ‘.pyo’ files. Since some programs may rely on having these available, you should only use this option if you know what you're doing.
  • A program doesn't run any faster when it is read from a ‘.pyc’ or ‘.pyo’ file than when it is read from a ‘.py’ file; the only thing that's faster about ‘.pyc’ or ‘.pyo’ files is the speed with which they are loaded.
  • When a script is run by giving its name on the command line, the bytecode for the script is never written to a ‘.pyc’ or ‘.pyo’ file. Thus, the startup time of a script may be reduced by moving most of its code to a module and having a small bootstrap script that imports that module. It is also possible to name a ‘.pyc’ or ‘.pyo’ file directly on the command line.

Note:

On 2015-09-15 the Python 3.5 release implemented PEP-488 and eliminated .pyo files.
This means that .pyc files represent both unoptimized and optimized bytecode.

What are python extensions in diffrent operating systems?

They are the same. In both windows and mac, you have .py files and compiled .pyc files. They are the same on all operating systems.

You might also occasionally meet .pyo, which is the same as a .pyc but it has been optimized. What that means really is that all assert statements are removed (used in testing software).

However, .pyd is specifically for windows. You can learn more about it from here.

UPDATE

There is also .pyw for pythonw programs on Windows - these do not generate a Windows console

-- From cdarke

What is the difference between py[cod] and pyc in .gitignore notation?

You are safe to remove the .pyc entry from your .gitignore, since .py[cod] will cover it. The square brackets are for matching any one of the characters, so it matches .pyc, .pyo and .pyd.

Since they are all generated from the .py source, they should not be stored in source control.

How to run a .pyc file when it imports some other .py files?

Asked a machine learning group. Here is what I found.
As long as, main.py and other.py are compiled to main.pyc and other.pyc, I can run it by python3 main.pyc.

Before this, my python automatically converts other.py to other.cpython-35.pyc. In this case, main.pyc cannot import other since there is no other in the folder (it's called other.cpython-35 now).

Thus, make sure .pyc file have the same name as .py, and then you can run any of them and python will include .pyc file for you when you execute the command.

May I omit .pyo and .pyc files in an RPM?

You could indeed omit them - they are either generated (if you have write access), or the .py is parsed every time you import it (which costs time).

But, depending on your distribution, your RPM system might contain easy scripts for compiling .py files and bundle the .pyo and .pyc files on distribution, which makes the task quite easy.

$ rpm --showrc | grep -A 7 py.*_compile
-14: py3_compile(O)
find %1 -name '*.pyc' -exec rm -f {} ";"
python3 -c "import sys, os, compileall; br='%{buildroot}'; compileall.compile_dir(sys.argv[1], ddir=br and (sys.argv[1][len(os.path.abspath(br)):]+'/') or None)" %1
%{-O:
find %1 -name '*.pyo' -exec rm -f {} ";"
python3 -O -c "import sys, os, compileall; br='%{buildroot}'; compileall.compile_dir(sys.argv[1], ddir=br and (sys.argv[1][len(os.path.abspath(br)):]+'/') or None)" %1
}
-14: py3_incdir /usr/include/python3.3m
--
-14: py_compile(O)
find %1 -name '*.pyc' -exec rm -f {} \;
python -c "import sys, os, compileall; br='%{buildroot}'; compileall.compile_dir(sys.argv[1], ddir=br and (sys.argv[1][len(os.path.abspath(br)):]+'/') or None)" %1
%{-O:
find %1 -name '*.pyo' -exec rm -f {} \;
python -O -c "import sys, os, compileall; br='%{buildroot}'; compileall.compile_dir(sys.argv[1], ddir=br and (sys.argv[1][len(os.path.abspath(br)):]+'/') or None)" %1
}
-14: py_incdir %{py_prefix}/include/python%{py_ver}

I. e., you can put %py_compile resp. %py3_compile into your %build section and you have what you need.

But, as said, you as well can omit them if you want to use them from several Python installations of various version numbers. But then you should make sure the .pyc and .pyo files are never created, as this might mess up things.

Understanding python compile

Python saves the precompiled .pyc file only for imported modules, not for the main script you're running.

Running a program as main or importing it as a module is not the exact same thing, but very similar because in a module everything that is at top level is executed at import time.

Note that for main program the source code is completely parsed and compiled too (so for example if you have a syntax error in last line nothing will be executed). The difference is only that the result of compilation is not saved back to disk.



Related Topics



Leave a reply



Submit