3

I have an application written in python 2.7 . In my app I want to give users the ability to compile a python source file (i.e py to pyc).

The builtin compile function does this but the output file is only compatible with python 2.7.

What I want is to compile the file to a range of different python versions (specifically python 2.5 , 2.6 & 2.7 )

I know that one way to approach this problem is re-write my application in each of those versions and just use the inbuilt compile function, but I do not want to do this.

I am open to all suggestions including writing a C extension, embedding python etc...

EDIT

The application which I am writing is a tool which allows to inject/modify arbitrary code inside a pyinstaller exe.

Now you may be knowing that pyinstaller actually puts compiled python source files within the exe. My app just extracts these embedded pyc files, decompiles them, allows the user to modify them, and then rebuild the exe.

Since the exe can be from a different python version, so I need the functionality to compile the decompiled output to that version.

Here is a screenshot of that app.

enter image description here

8
  • 2
    Why do you need to do this? If you use a Python install tool like pip this is done for you on install. And Python automatically does this when running the code anyway, provided there is write access at import time. Commented Feb 13, 2014 at 15:47
  • And the built-in compile() function does not produce .pyc files. You can use the compileall module to have Python produce those files for a whole directory or individual files instead. Just use python-2.5 -m compileall /path/to/project to do so with the Python 2.5 interpreter, for example. Commented Feb 13, 2014 at 15:49
  • 1
    pyinstaller installs code with a specific Python version. There is no need to precompile there. Commented Feb 13, 2014 at 15:50
  • @MartijnPieters yeah i know all details of the compile function. Commented Feb 13, 2014 at 15:50
  • @MartijnPieters Really? (I'm not a pythonist so far) This sounds great, but how are version incompatibilities in source code handled "automatically"? I assume producing compatible code is the point here. Isn't it? Commented Feb 13, 2014 at 15:51

1 Answer 1

1

I solved my own problem in a different approach.

Now, my main app is written in python 2.7. I wrote 2 dll each of which had a python interpreter embedded. Within one I embedded python 2.6 and within other python 2.5. Each of these two dlls uses the Python C API to compile a script given as an argument to the respective python version.

Now, from my main app (written in python 2.7) , I used ctypes to call the exported functions from these two dlls. The script to compile was passed as an argument.

Using this approach I was able to compile a given script to any of the 3 python versions.

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

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.