2

Suppose I compile some python files (.py to .pyc / .pyo) containing code using modules like NumPy, SciPy, MatPlotLib. If I execute them on another configuration (i.e. the client), is it required that versions of modules are the same ? Or have I only to be in the range of compatible versions ?

2 Answers 2

4

Even as compiled bytecode, names in modules are still strings. As long as the interfaces of the modules are compatible the code will still work with different module versions.

Sign up to request clarification or add additional context in comments.

2 Comments

However, this doesn't extend to changing the version of the underlying Python runtime - not only is the Python version is embedded in the generated filename, but there's a version compatibility marker inside the file itself (Note: reworded because my original comment was based on misreading the question)
Oops, never mind, I misread the question - I thought it also covered changing the Python version as well (which is the part that would trigger the need for recompilation)
2
+50

.pyc and .pyo files are just cached bytecode. Python's import machinery is built entirely around strings, and this leaves code that executes an import decoupled from whatever library they import.

So those files are no more tied to the version of the libraries they import than the source code itself. If the source code works with a wide range of versions of the library, so will the compiled bytecode.

You can always take a look at what bytecode Python generates with the dis module. A straight-up import statement becomes:

>>> import dis
>>> dis.dis(compile('import numpy as np', '', 'single'))
  1           0 LOAD_CONST               0 (0)
              2 LOAD_CONST               1 (None)
              4 IMPORT_NAME              0 (numpy)
              6 STORE_NAME               1 (np)
              8 LOAD_CONST               1 (None)
             10 RETURN_VALUE

The IMPORT_NAME opcode takes the name from the co_names structure that is attached to a code object (stored in the cache too):

>>> compile('import numpy as np', '', 'single').co_names
('numpy', 'np')

It doesn't matter here that the numpy module consists in large parts of dynamically-loaded libraries; if you replaced the name numpy with something else that would be imported instead. Modules are loaded at runtime, not at compile time after all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.