Using "Edit with IDLE" with a Python 2.6.5 script - python

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.

Related

Is there a way to run Python scrips in Atom with multiple versions of Python?

I am using the Atom text editor to write Python programs. I usually begin a new script with the command atom script_name.py, and the Atom text editor opens. You can execute scripts with the package script with the command cmd+i. (See https://atom.io/packages/script)
There are situations when I would like to use Python 2.7 and other situations when I prefer to use Python 3.4.
From my understanding, the only way to do this is to manually change the $PATH for the version of Python. In my case, the path for Python 3.4 is /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4
and the path for Python 2.7 is /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7.
Is there another way to do this besides manually changing $PATH each time? Is there another package I could be using?
EDIT: Maybe there is some confusion. I think you should be able to run in Atom a script using python2.7 and python3.4. In the command line, one would simply do python script.py for python2.7 and for python3.4 run python3 script.py.
You can have both versions in your PATH at the same time if the executables have different names.
In my case I have:
python 2.7 (python): /usr/bin/python
python 3 (python3): /usr/local/bin/python3

Portable exe-wrappers for Python scripts. In what situations will my solution not work?

I have these wrappers for Python 2 and 3 scripts:
for Unix-like systems:
#!/bin/sh
SCRIPTDIR="$(cd "$(dirname "$0")" && pwd)"
/usr/bin/env python2.7 "$SCRIPTDIR/program27.py" "$#"
#!/bin/sh
SCRIPTDIR="$(cd "$(dirname "$0")" && pwd)"
/usr/bin/env python3 "$SCRIPTDIR/program34.py" "$#"
for Windows:
#py -2 "%~dp0\program27.py" %*
#py -3 "%~dp0\program34.py" %*
The scripts are strictly command-line tools, and they are always placed in the same directory as the wrappers.  There are also version checks inside the scripts.  I'm basically trying to make sure they are invoked using the correct Python, and that they run on as many systems and Python distributions as possible.
The wrappers above are the only ones I've found to work with the standard Python distribution on Windows, Linux and Cygwin, and Darwin (and presumably BSD).
I'm a beginner with these things, and I understand there's no perfect solution, so I'm wondering if anyone can point out in what situations the above won't work, and help me improve on it.
Thanks.
Please see PEP 397 for information on how to handle executing code in the correct Python interpreter on Windows. On the other hand, PEP 394 covers a similar topic when running on Unix-like platforms. The recent changes were made in Python 3.3 to allow defined behavior for where code should be executed.
PEP 397: Python Launcher for Windows
The Python 3.3 Windows installer
now includes a py launcher application that can be used to launch
Python applications in a version independent fashion.
This launcher is invoked implicitly when double-clicking *.py files.
If only a single Python version is installed on the system, that
version will be used to run the file. If multiple versions are
installed, the most recent version is used by default, but this can be
overridden by including a Unix-style “shebang line” in the Python
script.
The launcher can also be used explicitly from the command line as the
py application. Running py follows the same version selection rules as
implicitly launching scripts, but a more specific version can be
selected by passing appropriate arguments (such as -3 to request
Python 3 when Python 2 is also installed, or -2.6 to specifclly
request an earlier Python version when a more recent version is
installed).
In addition to the launcher, the Windows installer now includes an
option to add the newly installed Python to the system PATH.
(Contributed by Brian Curtin in issue 3561.)
In short, preface your code with the following lines:
#! /usr/bin/env python2 for Python 2.x
#! /usr/bin/env python3 for Python 3.x

Using #!python2 not working to run under Python 2

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.

Python 3 installation on windows running from command line

Just curious, is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux? Is there some kind of way to configure Python so that it runs like this? Thanks.
EDIT: Just to add, the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
the reason I am asking is because I have both the Python 2 and 3 interpreter installed on my computer, and so it is ambiguous, as both are run using the command "python".
To run Python 2 executable:
C:\> py -2
To run Python 3 executable:
C:\> py -3
where py is a Python launcher that is bundled with your Python 3 installation.
py recognizes the shebang (e.g., #!/usr/bin/env python3 causes Python 3 executable to be run), it respects virtualenv (if you run py without specifying the explicit python executable version) i.e., run:
C:\> py your_script.py
and the correct python version is used automatically -- you don't need to specify the Python version on the command-line explicitly.
is there a particular reason why Python 3.x is not installed on Windows to run default with the command line "python3", like it does on Mac OSX and Linux?
OSX and Linux have python executable installed by default as a rule and it refers to Python 2 version in most cases at the moment that is why you need a separate python3 name there.
There is no Python on Windows by default. And therefore any version that you've installed is just python (I guess). The recommended way to manage multiple python versions is to use the Python launcher.
Is there some kind of way to configure Python so that it runs like this?
If you want to type python3 some_script.py instead of py some_script.py or even just some_script (assuming .py is in %PATHEXT% and Python launcher is configured to run Python scripts (check assoc .py and ftype Python.File) -- the default) then create a bat-file e.g., python3.cmd and put it in %PATH%:
"C:\path to\Python 3.X\python.exe" %*
You likely missed the checkbox at the bottom of the installer.
Full documentation here: https://docs.python.org/3/using/windows.html
Then, I think you just run python, not python3 from the Command Prompt. The reason Unix systems have python3 is because python defaults to Python2.x in many systems.
You have to add the python bin folder to your path. You can do it manually but when you install python i remember you have an option to do that.
I work with multiple Python 2.x and 3.x distros on Windows. Some of them are "portable" - i.e. not recorded in the Windows registry, and therefore not accessible by the version-selector py.exe delivered with Python 3.3+. To save my sanity, I wrote SelectPython.bat which is available on bitbucket. It configures the PYTHONHOME, PYTHONPATH and PATH variables according to the target you give it (a relative or absolute path to the parent directory of python.exe). You can do so in a way that is sticky for the rest of your command-line session:
> SelectPython C:\Path\To\Desired\Version\Of\Python
> python
or transiently, i.e. to call a particular python command without otherwise affecting the environment of the shell you're calling it from:
> SelectPython C:\Path\To\Desired\Version\Of\Python python -c "import sys;print(sys.version)"
You may find it helpful.

How to control what version of Python is run when double clicking a file?

Is there a way to control what version of python is run when double clicking on a py file? From the command line and in environments such as eclipse I can control what version is run. But from double clicking I am not sure.
I have 2.6 and 2.7 installed. 2.6 is for some application specific stuff and I want to make 2.7 the default. I have added "C:\Python27" to the PATH environment variable and that works well at the command line. C:\path\to\some\file>python someFile.py will run the file in 2.7. But if I double click the same file from explorer it runs 2.6. How to get it to run 2.7?
On Windows, you have to modify the file associations, for example via Right Click → Open with ... → Choose default program or the Control Panel's Folder Settings. You can choose between multiple python installations by navigating to the python.exe you want via the Browse button:
Alternatively, you can change the association in a command shell by typing
ftype Python.File="C:\Python27\python.exe" "%1" %*
Note that this requires administrator rights. If UAC is enabled on your machine, right click cmd in the start menu and select Run as administrator.
On freedesktop.org-compatible desktops, you can configure the association with xdg-mime.
On debian-based distributions, you can change the default python with update-alternatives. On all systems, you can also symlink the python in your path to the correct implementation, like this:
$ sudo ln -sf python2.7 /usr/bin/python
If the file is marked executable, it can also be executed directly from the command line or GUI if it starts with #! and the name of the interpreter:
#!/usr/bin/env python
To choose a specific Python version just for your program, you can start your Python program with one of the following lines:
#!/usr/bin/env python2.7
#!/usr/bin/python2.7
OK I have found the Python Launcher, which does exactly what I am after. Download can be found here. Installing this gave me the option for "Python Launcher for Windows (GUI)" when changing the file association via the right click menu.
Adding the shebang line
#!/usr/bin/python2.7
forces the script to run in 2.7.
This works great as I can control what version of python is running and users never need to know. No need for bat files, or dragging onto shortcuts etc. Nice and clean, and most importantly, no room for user error.
You can use ASSOC and FTYPE
assoc .py=pyfile
ftype pyfile=c:\Python27\python.exe %1
You will find answer in answers to the following questions:
How to execute python scripts in windows?
Using older Python 2.x while having both Python 2.x and 3.x installed on Windows
Temporary file association for single cmd.exe session
How to write a batch file showing path to executable and version of Python handling Python scripts on Windows?

Categories