1

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

AnotherDeveloper
  • 2,161
  • 2
  • 23
  • 27
  • Why exactly do you want the code precompiled? It might be easier to use the code directly as a module and let the python interpreter cache and compile it for you. Also in general 3.X and 2.X aren't compatible but I don't know if their bytecode is, but it probably is not cross compatible – Garrigan Stafford Aug 13 '18 at 15:02
  • I am asking why you it precompiled mainly becuase precompiling only saves compile time, the code itself runs the same speed as if you were to regularaly import it and then both code bases would be compiled on the same python enviroment – Garrigan Stafford Aug 13 '18 at 15:30
  • @GarriganStafford : Actually, the code which I have written contains some algorithms which are IP for the organization I am working. The code where, I have plugged-in lib is some open source code. Hence that precompiled / encryption. – AnotherDeveloper Aug 14 '18 at 04:18
  • Ok, then you will most likely have to make sure that you are on the same version of python as the library. Also check out what I said about load_compiled, did that help? – Garrigan Stafford Aug 14 '18 at 04:20

2 Answers2

1

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__?

Garrigan Stafford
  • 1,331
  • 9
  • 20
0

This is the complete solution of my problem. ( If you do not want to go through all the comment & discussion & figuring out the solution )

As Mr. Garrigan Stafford aptly pointed out that I am using the wrong API for loading the module.

The API for loading a compiled module is load_compiled & not load_source.

When I started using this API, ran in to the error of magic number: Bad magic number. This happens because while creating the file, the compiler inserts certain values to basically identify what file is it. ( more info : can be found here.).

In my case, compiled my lib is 3.6 & used in 2.7 which was causing the problem. To overcome, I basically went back to the original code & compiled my lib in 2.7 & then used it in the client code.

Volla !!!!

All works fine now.

Thanks to stackoverflow community as whole & Mr. Stafford in particular for helping out.

AnotherDeveloper
  • 2,161
  • 2
  • 23
  • 27