9

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
0xC0000022L
  • 20,597
  • 9
  • 86
  • 152

2 Answers2

3

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.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Whoa, nice. Is there a way to override the `python` used, though? As I wrote in my question, it's inside a `virtenv`. Side-note: the main reason to omit them is the resulting size of the package. – 0xC0000022L Aug 19 '13 at 11:36
  • You can override it by replacing the mentionned macros in your `.rpmrc` or `.rpmmacros` file. – glglgl Aug 19 '13 at 16:12
  • using the `--define` switch itself, because the whole build step is scripted and some of the defines are being computed. – 0xC0000022L Aug 20 '13 at 13:56
1

It's safe as long as you have their .py files. As .pyo and .pyc files are generated from the original .py files.

See also What do the python file extensions, .pyc .pyd .pyo stand for? and If Python is interpreted, what are .pyc files?

Community
  • 1
  • 1
Map X
  • 444
  • 1
  • 4
  • 14
  • 1
    Thanks. I know *what* they are, but I would still like to re-generate them after RPM installation. So my question goes it bit beyond the scope of your answer :) (also w.r.t. the fact that I use `virtenv` which seems to change some rules) – 0xC0000022L Aug 19 '13 at 11:33