LoginSignup
9

More than 5 years have passed since last update.

2to3でPython2.xのコードを3.xのコードに変換

Last updated at Posted at 2017-10-03

概要

Python2.xで書かれたコードを3.xのコードに自動変換する。

2to3

Pythonに標準で同梱されている2.xのコードから3.xのコードに変換するスクリプト。

前提環境

Python2.xかpython3.xをインストール済みの環境

使い方

Windowsはパスが通っていないので直接指定するかパスを通す。
(Pythonをインストールしたディレクトリ)\Tools\scripts\2to3.py

Linux系はpythonをインストールするだけで使える。

2to3 (pythonファイル)
変換前と変換後の差分が表示される。
ファイルの変更、保存は行われない。

オプション

  • -h --help
    ヘルプを表示
  • -w (pythonファイル) --write (pythonファイル)
    変換したコードを元ファイルに上書きする。
    (pythonファイル).bak に元ファイルがバックアップされる。
  • -n --nobackups
    バックアップファイルを作成しない。

参考サイト

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9