2

I have two Ubuntu 18.04.1 LTS systems:

  1. clean installation of Ubuntu 18.04.1 LTS
  2. upgraded from Ubuntu 16.04.5 LTS

Note: All things in the both systems have been installed using APT (from deb-packages). I did not use pip/pip3, make install, checkinstall and other similar utilities. So it is absolutely on topic. It is Ubuntu, Ubuntu uses APT.

First system have the following pyc-files:

  • /usr/lib/python2.7/dist-packages/gi/overrides/Dee.pyc
  • /usr/lib/python3/dist-packages/gi/overrides/__pycache__/Dee.cpython-36.pyc
  • /usr/lib/python2.7/dist-packages/gi/overrides/Unity.pyc
  • /usr/lib/python3/dist-packages/gi/overrides/__pycache__/Unity.cpython-36.pyc

Second system do not have these files.

I can not find packages for these files with dpkg -S. But I can find for scripts (*.py).

As far I can understand these files came from gir1.2-dee-1.0 and gir1.2-unity-5.0.
But after re-installation of these packages the *.pyc files are not created.

What is wrong?
How to get *.pyc in their places? Will my system work normally without these files?

N0rbert
  • 103,263

2 Answers2

4

I'm waiting for APT-based

It is not related to APT so that is going to take some time ;-)

What is wrong?

Nothing.

How to get *.pyc in their places?

If you really want to you could do it manually with py_compile. From the python command line (replace {pyfile.py} for the one you want compiled):

>>> import py_compile
>>> py_compile.compile('{pyfile.py}')

There is also a command line method that allows to compile all in a directory with ...

python -m compileall .

There is not really a need to do so though: these get created as you use software on your machine. That is also why this has no relation to APT.

Will my system work normally without these files?

Yes.

What actually happens: when a module is imported or when the source was changed, a .pyc file containing the compiled code will be created in the same directory as the .py file. This file is used to skip the compilation step for that module. Shaves of a few milliseconds on execution (not important for normal usages but imagine a webserver calling the same script 1000s of times within a seconds. Those milliseconds suddenly become important).

Regarding not seeing a .pyc file somewhere:

There are times when python can not compile. 1 example would be if you use a 2nd user that is not allowed to create a file in the location the compilation would be stored. Execution will not stop and then will use the source (and not the compiled version) (Running a script is not considered an import; this is only done for modules).

Rinzwind
  • 309,379
2

The *.pyc files contain Python bytecode. They are created when the corresponding *.py file is executed for the first time (and updated on the first run after the corresponding *.py changes).

Since the Python interpreter on your machine is able to create the *.pyc files and is good in creating them on-the-fly, the *.pyc files are not included in the APT repositories. They are just generated on your machine from the corresponding *.py files – which are included.

As a result, the machine used for a longer time and having been running more applications will likely contain more *.pyc files than a clean OS installation unless you clean up those files.

See also:

Melebius
  • 11,750