Search code examples
pythonimportpre-compilation

calling precompiled module from another file


Primarirly I am c++ developer trying to use python for certain tasks for me. I have made a python module in python 3.6 and got it pre-compiled in windows 7 using the following command

 python -m py_compile myfile.py

using information from this link. I get "myfile.pyc" created

Now I want to use this in a python file which is using python 2.7 so, I use information from this and this & write

mod=imp.load_source('myfile.func', 'c:/folder1/folder2/myfile.pyc')

But the above statement gives me exception

[name 'o' is not defined]

Is this because I am using pre-compiled in 3.6 & using in 2.7?

What is that am I missing here


Solution

  • First python 3.6 is not backwards compatible with python 2.7. Secondly its usually better to import the module as normal and let the compiler handle caching library code as compiled byte code. Also the function load_source is meant for loading uncompiled source files, the function you want is load_compiled. Check here https://docs.python.org/2/library/imp.html

    Lastly, if you are looking for performance improvements this will only help reduce compile time, and only on the first compile or when the imported file changes.

    What is __pycache__?