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").
Related
I have two questions:
There is an automated python script. Initially it should install Python2.7 if already installed version is lower than 2.7. (already implemented)
How can i successfully and cleanly change the python interpreter without disturbing python dependencies on the system. So that the next time any python script runs, it runs with python2.7
Linking python to python2.7 worked but it disturbs system dependencies and i cannot run modules e.g yum
Is there a way to continue with the remaining python script (after python2.7 installation and using python2.7 interpreter for rest of the code) without breaking the sequence or exiting the code?
Looking forwards to your responses.
Your best bet is to use a virtual environment. Short of that, you could try adding a "shebang" to the top of the Python script. The recommended shebang for specifying the "default" Python 2 interpreter is #!/usr/bin/env python2. Shebangs essentially allow the author of the script to specify which installation of the Python interpreter should be used when invoking this script directly (so if your script is located in script main.py, you would invoke it with ./main.py as opposed to python main.py in order for the shebang to take effect).
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.
I have always used a mac to write and run python scripts. However, I have a bunch of files on a PC that I need to run a script on. I have never really used a PC and don't know how to run the script.
If I go to the command program on the PC and type in python, nothing happens. How do I find where the python path is in order to get into the python prompt? Also, once I am in the prompt, is the importing of modules the same as in a Unix system?
Python isn't added to the system environment's PATH variable by default on windows. You have to either add the path to the directory containing the Python.exe file to the PATH variable, or call python explicitly.
This issue has been addressed in the Python documentation:
Python Documentation: # How to run a Python program under windows
Assuming Python is installed, it is usually placed in a folder prefixed with "Python" and the major/minor version. E.g. C:\Python26
I have 3 versions of Python (2.5, 2.7, 3.2) installed on a Windows machine, 2.5 being the default one (first in PATH and default for open action).
Now the weird thing appear when I run a python script with filename.py (without specifying the interpreter) or by clicking the file in Explorer: Python 2.5 is running the script (expected) BUT Python 2.7 PATH being inserted before the original system PATH.
Still if I look at the command line, it seems that Python 2.5 was executed.
Where is the first record in PATH (C:\Ptyhon27\) comming from, I can assure you this is added when the scripts runs, but by whom?
As you already observed I do have several versions of Python in PATH, this is not something anormal, because they can also have versioned executables instead and because Windows always picks the first one it the PATH.
Looking closely, it seems you have various versions of Python in your PATH environment variable.
The standard Python installer for Windows doesn't add itself to the PATH; I always do this manually for the version of Python I want to use by default. If you're using a different Python installer (such as ActiveState or Enthought) that may be the cause, but I haven't tried those.
In any case, you can edit your PATH environment variable manually and clean it up, leaving only the path to the version of Python you wish to be the default, as described here (scroll down a bit to get to the relevant section).
You might be interested in the Python Launcher for Windows project, a.k.a. PEP 397. Install it, and remove all Python dirs from PATH, leaving only the launcher one, and use py/pyw instead of python/pythonw.
Ahh, I think I have it! You didn't mention exactly how you're running Python...
Python adds the directory from which the command is run to the PATH! If you run it from the command line, this will be the directory in which the Python file you execute is located, or the directory you ran Python from if you just opened an interpreter. However, various tools, interpreters and development environments start up differently, and some of them will use a certain Python interpreter (depends on their configuration) and add its location to PATH.
If you want more help, please give a detailed description of how you're running Python when this happens.
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...