How do I confirm my shebang line worked for python? - python

How do I confirm my shebang line worked for python? I'm on a windows machine.
My default python is python3.3 but I would like to use python2.7 for my current script.
How do I know if the shebang worked? This is my quick code to attempt to confirm
#!/usr/bin/python2.7.6
import sys
print (sys.version)
However, I am being told that I'm still on python3.3. I know this is simple but I haven't found anything that can quickly answer my question.

Shebang lines are a Unix-ism, not a Windows thing. On Windows you will need to launch your script with the correct interpreter. One idea would be to change the file extension to .py2 and then associate that with the Python 2 interpreter.
You can also look at PEP 397, which describes a new launcher in Python 3.3 which can locate other Python interpreter versions according to your shebang line. This is a Python 3.3 feature, not a Windows one, just to be clear.

Related

How to force a Python .pyw executable to open in a specific Python version?

I have a program that I wrote a while back and converted into a .pyw executable file. It worked fine but recently I installed a different version of Python so I could use an outdated library, and now my .pyw won't open.
I'm assuming this is because it doesn't know which version of Python to use. The only way I can get it to open is to right-click the file, and choose to edit in IDLE 3.8 32bit, then run from there. Its very long winded compared to just running the file from my desktop. Of course I can also use the command line but its also longwinded.
Is it possible to add a line of code to the file which will force it to open in a specific Python version? I need to be able to specify the version and also x86/64, since I have Python 3.7, 3.8(64) and 3.8(32).
Note: please don't get onto me about having multiple versions installed I'm well aware that its not ideal but it was necessary for a project I'm doing :)
If you had installed Python with the py launcher on Windows, you can set a shebang line with Python 3.8.
For your case, you might want # python3.8-32 or # python3.8-64 or # python3.7.

using #!/usr/bin/env python3 shebang with Windows

I'm trying to run a Python script from the command line as a command on Windows -- so no usage of "Python" or ".py". If my script is named "testing.py", I am attempting to make this name into a command and call "testing" from the command line.
Going through the docs it seems I need to use this shebang #!/usr/bin/env python as long as I have Python in my PATH.
https://docs.python.org/3/using/windows.html#shebang-lines
I also have the script folder in my PATH, so something like
"testing.py" is currently working from the command line.
According to the docs and this tutorial,
https://dbader.org/blog/how-to-make-command-line-commands-with-python
I should be able to evoke my Python script just by "testing" if I have the proper paths within PATH and the above shebang. However, I can't seem to get the script running withouth adding ".py".
The accepted answer by #AKX is incorrect for Windows 10 standard Python 3, certainly in the latest Windows 10 (1903) if not earlier.
(Note: I cannot speak to how this may or may not work under WSL.)
I have several versions of Python installed (2.7, 3.6, 3.7, and most recently Python 3.8b1). I've been using the #!/usr/bin/env shebang for years in my scripts for cross-platform compatibility (usually to distinguish Py2 vs Py3 scripts).
I've created a little script in a folder (C:\so_test\awtest.py):
#!/usr/bin/env python3.6
import sys
print(sys.version)
If I run this with awtest.py or just awtest I get 3.6.x reported (showing it's running with Python 3.6). If I change the shebang to refer to 3.7, I get 3.7.x reported. If I change the shebang to just #!/usr/bin/env python3 I get the latest version of Python installed (3.8).
Now, if I add that folder to my path (path=%PATH%;C:\so_test in the command window you're testing in, or in the main env vars (you will need to restart the command window if you do the latter though)), I can change to a different directory and run awtest or awtest.py and they still work and refer to the folder in the path. If I remove the script folder from the path these files are no longer found.
While I wouldn't necessarily expect this to work on Windows prior to 10 or Python 2.7, this functionality appears to be the way of things going forward.
No, Windows does not support shebang lines.
The documentation you've linked relates to the py launcher installed by Python, which can interpret various shebang lines to choose a Python version to run a script with.
setuptools is able to generate wrapper .exes for your Python scripts, but it gets a little involved and already assumes you have a package with a setup.py and so on.
Locally, if you really, really need this, you probably could add .py to the PATHEXT environment variable, so the Windows command line looks up .pys like it looks up .exes (and various others; the current modern default is .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC). However, this will naturally not scale for distributing apps, as all of your users would need to set that too.
My recommendation is to stick with just that boring old python testing.py, really.
You can use shebang in windows by setting the path of your interpreter as the first line in the file(you will see a marker on VSCode that says 'set as interpreter ' on that line).
Using windows 10,Python version 3.9 see example:
#!C:/Users/waithira/AppData/Local/Programs/Python/Python39/python.exe
print('hello world')
if you're not going to do this often. You can still add a batch file to your path testing.bat containing the command that you need to execute your code.
#echo off
python testing.py
It's a borring workaround but it works without needing to mention the extention since windows interpret batch files the same way it interpret executables.

Run python program from terminal

I have downloaded a python program from git.
This program is python 3.
On my laptop i have both python 2.7 and python 3.4. Python 2.7 is default version.
when i want run this program in terminal it gives some module errors because of it used the wrong version.
how can i force an name.py file to open in an (non) default version of python.
I have tried so search on google but this without any result because of lack of search tags.
also just trying things like ./name.py python3 but with same result(error)
When you type "python", your path is searched to run this version. But, if you specify the absolute path of the other python, you run it the way you want it.
Here, in my laptop, I have /home/user/python3_4 and /home/user/python2_7. If I type python, the 3.4 version is executed, because this directory is set in my path variable. When I want to test some scripts from the 2.7 version, I type in the command line: /home/user/python2_7/bin/python script.py. (Both directory were chosen by me. It's not the default for python, of course).
I hope it can help you.
Use the shebang #!<path_to_python_version_you_want>
As in:
#!/usr/bin/env python
at the very top of your .py file
Also checkout: Should I put #! (shebang) in Python scripts, and what form should it take?
The Method of #Tom Dalton and #n1c9 work for me!
python3 name.py

Python shebang not working

Python is installed at:
C:/Python/Python35
At the top of my program I put:
#!/usr/bin/env python3
I opened windows command prompt and entered:
./words.py
The message I got was:
"." is not recognized
I was told this should work great on Windows so I'm confused?
./words.py will not work on the Windows command prompt, this way of executing scripts is meant for Linux/UNIX shells.
If you're using Python 3.3+:
The shebang lines will be obeyed by the Windows Python Launcher py if you have it installed (https://docs.python.org/3/using/windows.html#shebang-lines).
Make sure you have that installed and try launching using:
py words.py
There are multiple problems here.
Shebangs are a Unix thing. Windows does not recognize them at all. Instead, Windows uses file extensions.
There is no file named /usr/bin/env on Windows, so even if your shebang did work, it wouldn't do what you want.
The Python installer normally associates Python with .py files on installation. If it did not in your case (because you disabled the option?) you need to go fix that.
./something doesn't work on cmd.exe. It's .\something or just something.
Whoever told you, must have either misunderstood you or you misunderstood them.
Shebang lines have no effect with windows unless you're trying to use cygwin. The other reason you'll need them to ensure smooth transition between windows and linux if you're passing the code to someone who might be running on linux.
As far as the "." it is not used in windows. Its reason in linux OS is to inform the command that the script is in the current directory.
For windows all you'll need is: python words.py.
Your first problem is that Windows won't treat / as a path separator here. It has to be .\words.py or just words.py. That's why you get the error you see.
The next problem is that the shebang is not implemented by the windows command shell. It works on linux shells (and linux-like shells on Windows such as cygwin) because those shells read the front bit of executables to see how they should be executed. Windows does this by looking at the extension.

Using older Python 2.x while having both Python 2.x and 3.x installed on Windows

I'm new to python. I've installed both Python 3.2.2 for x64 and Python 2.7 for x86 on my 64-bit windows machine. I've got some python code that are coded for python 2.x versions. But every time I try to run them by double clicking it is interpreted by python 3.x.
How do I force them to use python version 2.7, may be using some directives or using a BATCH script?
Going forward, the solution is to upgrade Python 3.2 to 3.3 or later and use the Python Launcher for Windows.
At the top of each Python 3 program, include the following line:
#!/usr/bin/env python3
At the top of each Python 2 program, include the following line:
#!/usr/bin/env python2
The #! part, called a shebang, indicates to Python Launcher which version of Python is desired. (It also indicates to UNIX that a program should be run with a particular interpreter instead of the shell.) The /usr/bin/env part helps locate the Python interpreter on your PATH when you run a program on UNIX. If you don't plan on using anything but Windows, you can leave it out:
#! python3
[or]
#! python2
You could, e.g., associate the file extension .py2 with Python 2.7 and rename the main file (assuming you regard Python 3.2 as your default version).
open a command line window, and use "c:\python27\python.exe" yourscript.py (or whatever path your python 2.7 appears to be installed in).
of course, you can put that line into a batch file and execute the batch.
also, you can put a shortcut to c:\python27\python.exe on your desktop and drop your script onto that shortcut every time you want to run it.
try editing PATH environment variable, use python 2.7 path and delete python 3.2.2

Categories

Resources