简体   繁体   English

使用Python 3to2修复程序集时出现问题

[英]Problems using the Python 3to2 fixers set

I'm a newbie to programming and need some help with the set of fixers to refactor code written for Python 3.x into code that can run on a 2.x interpreter. 我是编程的新手,需要一些修复程序帮助,以将为Python 3.x编写的代码重构为可以在2.x解释器上运行的代码。

I have Python 3.2, but the admissions for the Spotify Puzzle's ( http://www.spotify.com/us/jobs/tech/best-before/ ) requires that the code is written in 2.6. 我有Python 3.2,但是Spotify Puzzle的入场证( http://www.spotify.com/us/jobs/tech/best-before/ )要求代码必须使用2.6编写。 Therefore, I found the 3to2 coverter. 因此,我找到了3to2掩护。 I have the "3to2_py3k-1.0.tar.gz" vesion which you can download from here: https://bitbucket.org/amentajo/lib3to2 . 我拥有“ 3to2_py3k-1.0.tar.gz”版本,可以从这里下载: https ://bitbucket.org/amentajo/lib3to2。

The README file tells you to README文件告诉您

(...)Run "./3to2" to convert stdin ("-"), files or directories given as arguments.(...) (...)运行“ ./3to2”以转换标准输入(“-”),作为参数指定的文件或目录。(...)

The text on this site is identical to the README file. 该站点上的文本与README文件相同。

I understand this I'm supposed to run the commandline and then write: 我了解这一点,我应该运行命令行,然后编写:

C:\\Python32>3to2 Spotify.py C:\\ Python32> 3to2 Spotify.py

3to2 is not recognized as an intern or extern command, a program a or batch file. 3to2无法识别为内部或外部命令,程序或批处理文件。

(Manually translated from norwegian). (手动翻译自挪威语)。

I have tried to path to the spesific files, but there is no file that is named "3to2". 我试图路径到特殊文件,但是没有名为“ 3to2”的文件。 I Have tried to path to the "main.py" file and "Spotify.py" directly, and it seems like something happens(Like it takes a tiny bit of a second, and gives no errors), but when I open "Spotify.py" again, nothing have happend to the file. 我试图直接路径到“ main.py”文件和“ Spotify.py”,似乎发生了一些事情(就像花了一点时间,并且没有错误),但是当我打开“ Spotify”时.py”,则文件没有任何反应。

I appreciate all help. 我感谢所有帮助。


Thank you spatz, this helped a lot. 谢谢spatz,这很有帮助。 Finally, I managed to install the package by cd C:\\Python32>python.exe setup.py install 最后,我通过cd C:\\Python32>python.exe setup.py install来安装该软件包。

Anyway I still have some problems. 无论如何,我仍然有一些问题。 Take the print("Hello World!") program as an example. print("Hello World!")程序为例。 When I run python.exe 3to2 HelloWorld.py (it did't work without the python.exe) it answers: 当我运行python.exe 3to2 HelloWorld.py (没有python.exe时不起作用)时,它会回答:

 RefractingTool: Skipping implicit fixer: collections
 RefractingTool: Skipping implicit fixer: int
 RefractingTool: Skipping implicit fixer: memoryview
 RefractingTool: Skipping implicit fixer: printfunction
 RefractingTool: Skipping implicit fixer: inittest
 RefractingTool: Refactored HelloWorld.py
 --- HelloWorld.py   (original)
 +++ HelloWorld.py   (refactored)
 @@ -1 +1 @@
 -print("Hello World!")
 +print u"Hello World!"
 RefactoringTool: Files that need to be modified:
 RefactoringTool: HelloWorld.py

This is really mixed signals for me. 这对我来说确实是混合信号。 First of all, it seems like it skips some fixers, for instance the printfunction . 首先,它似乎跳过了一些printfunction ,例如printfunction But later, it definitely looks like it changed print("Hello World!") with print u"Hello World!" 但是后来,它肯定看起来像是用print u"Hello World!" print("Hello World!") print u"Hello World!" as it should. 正如它应该。 The problem is that when I open this file in IDLE 3.2, the file has the original code. 问题是,当我在IDLE 3.2中打开此文件时,该文件具有原始代码。 The exact same happens with the Spotify.py file. 与Spotify.py文件完全相同。 Is IDLE "smart" enough to change this simple code so it is compatible with 3.x, or doesn't the 3to2 fixer set works? IDLE是否“智能”足以更改此简单代码,使其与3.x兼容,还是3to2修复程序集不起作用?


SOLUTION: I figured it out. 解决方案:我想通了。 I had to include -w as an argument. 我必须包含-w作为参数。 That converted the original file in addition to creating a backup fil .bak . 除了创建备份fil .bak之外,还转换了原始文件。

Thanks to anyone that helped me! 感谢任何帮助我的人!

It seems you did not properly install 3to2 . 看来您没有正确安装3to2 To install do the following: 要安装,请执行以下操作:

  1. Unpack the .tar.gz file you downloaded to some directory 将下载的.tar.gz文件解压缩到某个目录
  2. cd to that directory and run python setup.py install cd到该目录并运行python setup.py install

Now you can cd to the directory where Spotify.py is and run 3to2 Spotify.py , and it should work. 现在,您可以cdSpotify.py所在的目录并运行3to2 Spotify.py ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM