I have both Python 2 and 3 installed on my machine running Windows.
I am running a program via cmd which has the command #!python2 at the very top of the code, and i want that to run in Python2.
It was my understanding that the having #!python2 on the first line would force Python to use Python2. Instead it uses Python3 [and hence the line 'print helloresults in the errorSyntaxError: Missing parentheses in call to print` ]
The story is a little more complicated on Windows than on Unix.
For Windows installations, when you install Python, the installer actually associates Python (.py) files with py.exe, an executable specifically made for launching Python on Windows. You can read all about it in PEP 397 -- Python launcher for Windows.
Since .py files are associated with that launcher, all the magic of reading the shebang line is implemented in py.exe. Long story short, your first line must read
#!/usr/bin/env python2
in order to work. In PEP 397, read the section titled "Python Script Launching" for more information.
Usually the hash-bang requires a full path, e.g.:
#!/usr/bin/python
If you've installed it such that there's an executable somewhere on your path called python2, you can find the full path to it by executing the command:
which python2
You'd then use the output of that command in the shebang.
And of course, you can always run your script with whatever python version you want by specifying it explicitly:
python2 your_script.py
Run this way, the shebang will be completely ignored and python2 will execute the script.
Related
I need to write a python file, make it executable and be able to execute it on Windows and Unix.
For example, I place #!/usr/bin/env python3 at the top of "foo.py". I chmod foo.py to be executable.
This runs fine on Unix.
On Windows, it gives the following error
/usr/bin/env: ‘python3’ No such file or directory
If I replace "python3" with "python" in "#!/usr/bin/env python3", the executable will work on Windows.
My question is
"Can I find a way to make the execytable foo.py run on diferent platforms without the need to manually replace "python3" with "python"?" For reference, I am attaching below an answer to such issue by #Christian Witts
"The Windows installation of Python 3.x does not have an executable called python3 unlike *nix which does. If you change your shebang line to #!/usr/bin/env python it will use the Python that is installed."
I have been reading about python, and I have certain doubts when using the shebang line. When I run:
py file_name.py
It executes the script using the python version that I have indicated in the shebang line, but when I execute,
python file_name.py
The last version of Python is used instead of the one that I have specified. I would like to know the difference between using python and py when running a script from the command line. My shebang line was #! python3.4
Does using either python or py have different implications or eventually both are the same?
(This answer assumes you're using Windows, but I expect it mostly applies to other OSes too, modulo some details)
"python.exe" is the actual interpreter. You have one for each version of Python on your system. Which version gets executed when you run "python" on the command line, depends on your PATH environment variable and your current working directory.
"py.exe" is the Python Launcher. You probably only have one, even if you have multiple Python installs. Mine is in C:\Windows. It looks at the script, decides what version it's using, and delegates the actual execution to the proper interpreter.
To see what version of Python are in use for each command, run these two commands:
python -c "import sys;print(sys.version)"
py -c "import sys;print(sys.version)"
If both show the same version, then on your system they are both currently the same. However, one or the other might be updated. I personally recommend to rely on neither, and to call the Python version that you want explicitly in your scripts.
I have installed both Python 2.7.1 and 2.6.5 versions on Windows. I have added only the path of Python 2.6.5 in the Environment Variables.
Now, I want to run a Python script x.py using Python 2.6.5. I know how to do it using the cmd but It would be more convenient to just open it with IDLE and run inside it using the Run Module option.
This is supposedly done by right-clicking over the script, and then going to Edit with IDLE option, but this opens and runs the script using Python 2.7.1. Is there a way to open and run it with Python 2.6.5?
The standard command in the registry for Edit with IDLE is the following:
"C:\Program Files\Python33\pythonw.exe" "C:\Program Files\Python33\Lib\idlelib\idle.pyw" -e "%1"
Now, as you can see, the path is hardcoded into it, so it just cannot use a different Python interpreter like that—at least per default.
However, PEP 397 introduced a new Python launcher for Python making it possible to launch different versions of Python based on the shebang line. So a file starting with #!/usr/bin/env python2 will launch the current Python 2 interpreter, while #!/usr/bin/env python3 will use Python 3.
Using that information, you can launch IDLE for a given Python version dynamically. For example this would edit the file using the launcher’s default Python version:
C:\Windows\pyw.exe -m idlelib.idle -e "%1"
This would force the use of Python 3
C:\Windows\pyw.exe -3 -m idlelib.idle -e "%1"
And this would force the use of Python 2:
C:\Windows\pyw.exe -2 -m idlelib.idle -e "%1"
So what you would need to do is write a script that basically checks which Python version a file wants to be executed with, i.e. by parsing the shebang line manually (sadly the launcher does not give you this information without actually launching the script—yet. I might start a pull request to get such a feature into the core). Then you would run either the Python 2 or Python 3 IDLE using the command above and done.
You would just need to change the Edit with IDLE command to execute your script then and it would work.
A very simple alternative would be to just add another registry key which launches the Python 2 IDLE. So you would have Edit with IDLE and Edit with IDLE (Py2) or something.
To do that, just put the following inside a .reg file and execute it:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)\command]
#="C:\\Windows\\pyw.exe -2 -m idlelib.idle -e \"%1\""
edit: I just noticed that I wrote this mainly about Python 2 vs. Python 3; the launcher ships with Python 2.7 I think, so this will work just the same. You just need to adjust the launcher’s version specificiers to -2.6 and -2.7 or whatever you want.
You can do this with some registry hacks to make IDLE 2.6 your default (rather than 2.7), but that's not really what you want, I think, since then you'd have to reverse the process when you want to test something in 2.7. Unless someone else knows some other way to integrate different IDLE installs into the shell, here are a couple better options:
Open IDLE 2.6 first, and just use the Open File dialog from the GUI.
Use a different IDE that actually supports this functionality. Eclipse with PyDev will let you switch interpreters between runs, or save configurations with different interpreters, and so on.
What line of text should I place at the top of a script.py to invoke the specific version of Python that I need to use?
I have two versions of Python on Windows XP, 2.6.5 and 2.7.2. They each have their own special modules and were installed by separate applications. My scripts are placed on the desktop so that I can double-click and run them conveniently.
The problem is that all my scripts invoke 2.6.5, which is fine for the scripts that use the modules installed with 2.6.5, but my scripts intended for 2.7.2 don't run. They invoke the Python 2.6.5 without the modules I need to import.
I've tried typing various headers without and without the #! to invoke 2.7.2 when I need to, but either my syntax is wrong or it just isn't possible to specify under Windows. Could anyone tell me the precise syntax of the line I need to add to my script. The python.exe for 2.7.2 is stored under C:\OSGeo4W\bin
Thanks for letting me know what line to place at the top of a script.py to invoke the exact version of Python I need to use.
If you have installed Python 3.3 on your system it added a new launcher for Python scripts which means you can use a shebang line:
#!python2.7
print "Hello"
or
#!python3.3
print("World")
will both work and run the appropriate python, or you can specify a full path to a Python interpreter or create an ini file that defines abbreviations for specific Python interpreters.
See PEP 397 for the different options available in Windows shebang lines.
If you don't want to install Python 3.3 then you can also install the launcher separately.
Instead of putting the script itself on the desktop, place a shortcut on the desktop. The process is described in a techrepublic.com article. Specify the appropriate interpreter as the program to run, and list one of your .py files as a parameter in the same field.
There is not "shebang" notation on Windows.
You'll need to change the file association for .py files to use your 2.7.2 installation ("Open With", "Use application as default").
I have some small utility scripts written in Python that I want to be usable on both Windows and Linux. I want to avoid having to explicitly invoke the Python interpreter. Is there an easy way to point shebang notation to the correct locations on both Windows and Linux? If not, is there another way to allow implicit invocation of the Python interpreter on both Windows and Linux without having to modify the script when transferring between operating systems?
Edit: The shebang support on Windows is provided Cygwin, but I want to use the native Windows Python interpreter on Windows, not the Cygwin one.
Edit # 2: It appears that shebang notation overrides file associations in Cygwin terminals. I guess I could just uninstall Cygwin Python and symlink /usr/bin/python to Windows-native Python.
Read up on the Python Launcher for Windows in the docs, which was initially described in PEP 397. It lets
you define custom shebang configurations in "py.ini" (e.g. to use pypy),
and out of the box you can use virtual shebangs such as #!/usr/bin/env python3, or shebangs with real paths such as #!"C:\Python33\python.exe". (Quoting is required for paths containing spaces.) You can also add command-line options to a shebang. For example, the following shebang adds the option to enter interactive mode after the script terminates: #!/usr/bin/python3 -i.
The installer associates .py (console) and .pyw (GUI) script file types with the respectively named launchers, py.exe and pyw.exe, in order to enable shebang support for scripts in Windows. For an all-users installation, the launchers are installed to the Windows folder (i.e. %SystemRoot%). For a per-user installation, you may need to manually add the installation directory to PATH in order to use py.exe in the shell (*). Then from the command line you can run Python via py -2, py -3, py -2.6, py -3.3-32 (32-bit), and so on. The launcher is handy when combined with -m to run a module as a script using a particular version of the interpreter, e.g. py -3 -m pip install.
(*) The new installer in 3.5+ defaults to "%LocalAppData%\Programs\Python\Launcher" for a per-user installation of the launcher, instead of installing it beside "python.exe", and it automatically adds this directory to PATH.
Unless you are using cygwin, windows has no shebang support. However, when you install python, it add as file association for .py files. If you put just the name of your script on the command line, or double click it in windows explorer, then it will run through python.
What I do is include a #!/usr/bin/env python shebang in my scripts. This allows for shebang support on linux. If you run it on a windows machine with python installed, then the file association should be there, and it will run as well.
Short answer:
The easiest way is to install git for windows wich comes with GitBash. Then add shebang lines in your scripts to indicate they should be run with python when executed.
#!/usr/bin/env python
More info:
Unlike Cygwin, git bash uses your native windows applications and lets you use bash scripts without any configuration.
It will automatically treat any file with a shebang line as executable the same way Linux shells do.
eg: #!/usr/bin/env php or #!/usr/bin/env node or any other application you want will work as long as you add the paths to your windows ENV path.
You can edit env vars in windows by hitting start and typing env should be the first option.
Git bash also installs git and hooks it up with a credentials manager for you and makes it super easy to sign into 2fa-enabled svn services and a ton of other handy developer features.
Git bash is IMO a must on every developer's machine.
Another option:
Install WSL (Windows Subsystem for Linux) which will work the same.
WSL also lets you install native Linux versions of all your command line applications if you prefer.
Linux binaries will take precedence over windows ones if installed but you can still choose to run the windows version of commands any time you want by specifically adding .exe on the end.
Install pywin32. One of the nice thing is it setups the file association of *.py to the python interpreter.
sorry for open old topic.
I create one file py.cmd and place it in the C:\Windows\System32 folder
py.bat:
#(
#set /p shebang=
)<%1
#set shebang=%shebang:#! =%
#%shebang% %1 %2 %3 %4 %5 %6 %7 %8 %9
py.bat file explain:
Get the first line from *.py file
Remove shebang characters "#! "
Run python file using shebang python path
All windows python script must start with shebang line as the first line in the code:
#! c:\Python27\python.exe
or
#! c:\Python37\python.exe
Then run it:
cmd> py SomePyFile.py param1 param1 paramX
Not with shebang ... but you might be able to set up a file association, see this SO question which deals with Perl and the associated answers which will also be pertinent as there's known problems with Windows and stdin/out redirection...