May I omit .pyo and .pyc files in an RPM?
python, python-2.7, rpm, virtualenv
Solution
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.
Problem
I am bundling a Python application along with its virtenv environment inside an RPM for easier deployment. Is it a sound decision to omit all the `.pyo` and `.pyc` files? What I would do instead is to invoke `compileall.py` in the post-install action from within the `virtenv` instance. Does that work, or will this mess up things? Note: I realize that I could try on one machine, but a.) this wouldn't give me a conclusive answer as to whether this will work on other machines and b.) others may have the same question and I didn't find it answered.