[Tutor] regex to promote Py 2 to Py 3?

eryksun eryksun at gmail.com
Wed Apr 17 09:21:24 CEST 2013


On Wed, Apr 17, 2013 at 1:11 AM, Jim Mooney <cybervigilante at gmail.com> wrote:
>>>> 2to3 loops.py
>   File "<stdin>", line 1
>     2to3 loops.py
>        ^
> SyntaxError: invalid syntax

The script 2to3.py is run from the system's terminal/console shell
(e.g. cmd or PowerShell on Windows), not the python shell.

If the current directory has the file "loops.py" and Python is
installed in C:\Python33, then try running the following in cmd:

    C:\Python33\Tools\Scripts\2to3.py -w loops.py

If that fails, try running 3.3 explicitly:

    C:\Python33\python.exe C:\Python33\Tools\Scripts\2to3.py -w loops.py

The -w option tells 2to3 to write the ported version back to loops.py.
The original will be backed up to loops.py.bak.

----

Running Scripts

Python 3.3 is distributed on Windows with a "py" launcher that's
associated with .py files. This should be set up by the installer as
follows:

    C:\>assoc .py
    .py=Python.File

    C:\>ftype Python.File
    Python.File="C:\Windows\py.exe" "%1" %*

The launcher parses a script's shebang line, which is a special
comment on the first line. A shebang begins with an exclamation point
(i.e. a "bang"), followed by the command used to run the script. The
launcher accepts a literal command (e.g. #!C:\Python33\python.exe), a
preset virtual command, or an alias you've added to the configuration
ini.

For example, this shebang runs a script in the default Python version:

    #!/usr/bin/env python

"/usr/bin/env" is a Unix-ism included for portability. You can simply
use "#!python3" if you want the script to run in 3.x on Windows.

2.x is the default if it's installed. You can configure the launcher
to instead use 3.x as the default. Just edit the configuration ini:

    >notepad %localappdata%\py.ini

to include the following:

[defaults]
python=3

For convenience, you can add your script directories to the
environment PATH. You can also append ;.PY to the environment PATHEXT
to be able to run "script.py" as just "script", without the .py
extension.


More information about the Tutor mailing list