14

I recently started python development on raspberry pi. While reading about .pyc file to speed up the start up, I was wondering if I test a .pyc file on PC, given that same python modules are available on Rpi, will it work directly ? Please also include what happens if python version or any of module version differs on target platform.

Thanks in advance.

dhruvvyas90
  • 1,135
  • 1
  • 15
  • 31
  • 1
    If the Python version differs, or the source files have a newer timestamp, the .pyc files are discarded and regenerated the first time there's any attempt to load them. From that perspective, trying to mess with them seems rather silly -- you're saving very little time, even on an RPi, and even that only the first time those modules are loaded, as on subsequent loads you'd have locally generated copies. – Charles Duffy May 25 '15 at 17:48
  • don't bother about .pyc files. always use .py files, the compiled version is generated automatically. – Daniel May 25 '15 at 17:52
  • The compiled version is not generated if the file is being run from a folder that's read only for the running user. – Seva Alekseyev Jul 27 '23 at 14:48

2 Answers2

12

Compiled Python bytecode files are architecture-independent, but VM-dependent. A .pyc file will only work on a specific set of Python versions determined by the magic number stored in the file.

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Can you explain VM dependent part a lil bit in detail ? Thanks – dhruvvyas90 May 25 '15 at 17:50
  • @dastaan, follow the link? It explains in great detail. (Perhaps "runtime version dependent" would be clearer). – Charles Duffy May 25 '15 at 17:51
  • @dastaan: Python is made up of a large shared library which contains the Python VM and a few select modules, the Python standard library which contains the majority of the modules, and a small stub executable that initializes the VM and feeds it a file and optionally the REPL. – Ignacio Vazquez-Abrams May 25 '15 at 17:54
  • @dastaan, the last four words of this post that we're commenting on -- "stored in the file" -- is a link, and has been since said post was first created. – Charles Duffy May 25 '15 at 17:54
-3

Short answer: Yes. Just keep in mind that your code must be OS-aware too.

And use the same version of python in both platforms.

Luciano Barcaro
  • 388
  • 2
  • 6