5

I am currently running a Python scripts both on Linux and Windows 7. The file is executed in an execv style with which I mean that the interpreter is defined in the beginning of the file in a command.

In Windows system, the interpreter specification is:

#!C:\Python26\python.exe

However in Linux this needs to be

#!/usr/bin/python

I would like to run this script in both systems without having to change this line again and again.

I have tried out the following:

#!C:\Python26\python.exe
#!/usr/bin/python

as well as:

#!C:\Python26\python.exe;/usr/bin/python

So: is there any way I could specify multiple interpreters?

jsalonen
  • 29,593
  • 15
  • 91
  • 109

3 Answers3

4

Depending on what you're trying to do, this might be a bit heavy-weight, but 0install can run your program will the appropriate Python interpreter for your platform. In your program's XML description, do something like this (e.g. if you want Python >= 2.6, < 3):

<command name="run" path="myprog.py">
  <runner interface="http://repo.roscidus.com/python/python">
    <version not-before="2.6" before="3"/>
  </runner>
</command>

See: http://www.0install.net/local-feeds.html

This will also make 0install download a suitable version of Python if the user doesn't have it already.

Note that you may want to do this even if you're only targetting Linux, because with Python 3 there is no single #! line that works on all platforms (some platforms, e.g. Arch, require "python2" not "python", while others, e.g. Debian, don't provide "python2", only "python").

Thomas Leonard
  • 7,068
  • 2
  • 36
  • 40
3
#!/usr/bin/env python

That will call the env program to search your PATH for a Python executable.

If you need to ensure a specific version of Python you can do e.g.:

#!/usr/bin/env python3.11
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • 1
    @jsalonen Windows doesn't use the shebang line to determine what to run the file with. The standard Python installer for Windows sets it up so you can run .py/.pyw files from the command line just like any other executable, so you shouldn't need to worry about Windows. – Liquid_Fire Oct 21 '10 at 15:14
  • The point being that I don't have /usr/bin/env available on Windows – jsalonen Oct 21 '10 at 16:02
  • @jsalonen: The point being that they're *different* operating systems and have almost *nothing* in common. What you're asking for cannot be done across operating systems. – S.Lott Oct 22 '10 at 11:13
  • @jsalonen you don't need to have `/usr/bin/env` available on Windows - when run on Windows, the Shebang line will simply be treated as a normal comment ! – TheEagle Aug 01 '21 at 17:24
1

Is there any way I could specify multiple interpreters ?

You don't need to. On Windows (at least as long as you don't have CygWin or similar installed), the Shebang line is treated as a normal Python comment; that means, it is ignored. Windows knows that it should run .py and .pyw files with the Python interpreter, because it is told that upon installation of Python.

TheEagle
  • 5,808
  • 3
  • 11
  • 39