pythonpycexecfile

Calling python compiled files in Python 3


I have multiple Python versions installed (2.7 and 3.4) I want to run a .pyc with specified version of Python

#! C:\python34\python
import sys
print("Hello",sys.version.split()[0])
input()

This sheebang works fine on Windows because I use pylauncher So I can compile like that

c:\python34\python -m compileall print.py -b

But the sheebang is not recognized when I execute the pyc file.

This works, but I wouldn't like to repeat the C:\python34\python Because the current script will be already running under the Python version I asked in the shebang. Therefore I would like to make the sub program start with the same version of the Python.

So far, I tried:

#! C:\python34\python
import os
os.system("C:\python34\python print.pyc")

This would be perfect, but doesn't like pyc files. And the following doesn't works either:

exec( open('print.pyc').read() )

Does someone knows how to call the pyc files in the code?


Solution

  • #! C:\python34\python
    import print # imports print.pyc
    
    
    #now you can use the pyc as a module. 
    DoSomething()