1

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?

Baghera
  • 53
  • 6
  • 1
    have you tried: **os.system("C:\\python34\\python print.pyc")** ? – Mansueli May 20 '14 at 16:15
  • No because that I want is not to repeat C:\python34\python since my shebang works fine. I want to say: Run that pyc file in the same already running python's version. – Baghera May 20 '14 at 16:21
  • 1
    Can you formulate your question better? You said : **os.system("C:\python34\python print.pyc")** would be fine but it doesn't *like pyc.* While it clearly doesn't have the right backslashes (\\ instead of \ ) in the string. – Mansueli May 20 '14 at 16:27
  • It works with "C:\\python34\\python print.pyc", with "C:\\\python34\\\python print.pyc", with "C:/python34/python print.pyc" and with "C:\python34\python print.pyc". But since the path is ok whith the shebang, I dan't want to repeat it. – Baghera May 20 '14 at 19:33
  • Have you tried os.system(sys.executable+" print.pyc") ? sys.executable is path to python executable that is running just now. – Filip Malczak May 20 '14 at 20:34
  • Seems to be ok. Not simpler, but ok. – Baghera May 21 '14 at 01:01

1 Answers1

2
#! C:\python34\python
import print # imports print.pyc


#now you can use the pyc as a module. 
DoSomething()
Mansueli
  • 6,223
  • 8
  • 33
  • 57
  • Yes, this is perfect: – Baghera May 20 '14 at 19:36
  • @Baghera, if this answer is what you were looking for, please accept it. – Mansueli May 20 '14 at 19:37
  • Yes, this is perfect:
    #! C:\python34\python import print This is the run.py file.
    It starts the print.pyc application with the Python's version I want to, because I compiled this application with.
    Thanks
    – Baghera May 20 '14 at 19:42