目前我在大公司工作,我们需要将 python2 旧的大 Django 项目转换为 python3 版本,所以我做了很多相关的研究,但仍然无法找到与哪个版本的 Python 和 Django 最适合转换相关的完美答案。
目前,我在旧版本中使用 Python:2.7.16 和 Django:1.9.13。
任何人都可以向我推荐最适合的 Python 和 Django 版本,用于 python2 到 python3 转换的旧版本。
我有一堆用python 2.x编写的模块,我需要它们在3.x中才能工作.我在整个文件夹上运行了2to3,它似乎工作正常,但当我再次查看文件时它们是相同的.我没有使用2to3,并想知道它是否将转换后的文件保存在其他目录中.
我正在查看包含此更改的2to3的输出:
- for file_prefix in output.keys():
+ for file_prefix in list(output.keys()):
Run Code Online (Sandbox Code Playgroud)
哪里output是字典.
这种变化有什么意义?为什么2to3这样做?
这种变化如何使代码Python 3兼容?
我正在尝试编写一些代码将数据放入管道,我希望解决方案是python 2.6+和3.x兼容.例:
from __future__ import print_function
import subprocess
import sys
if(sys.version_info > (3,0)):
print ("using python3")
def raw_input(*prmpt):
"""in python3, input behaves like raw_input in python2"""
return input(*prmpt)
class pipe(object):
def __init__(self,openstr):
self.gnuProcess=subprocess.Popen(openstr.split(),
stdin=subprocess.PIPE)
def putInPipe(self,mystr):
print(mystr, file=self.gnuProcess.stdin)
if(__name__=="__main__"):
print("This simple program just echoes what you say (control-d to exit)")
p=pipe("cat -")
while(True):
try:
inpt=raw_input()
except EOFError:
break
print('putting in pipe:%s'%inpt)
p.putInPipe(inpt)
Run Code Online (Sandbox Code Playgroud)
上面的代码适用于python 2.6但在python 3.2中失败(请注意,上面的代码主要是用2to3生成的 - 我只是稍微搞砸了它,使它与python 2.6兼容.)
Traceback (most recent call last):
File "test.py", line 30, in <module>
p.putInPipe(inpt) …Run Code Online (Sandbox Code Playgroud) 我跑 2to3 -f all -f idioms -f buffer -f set_literal -f ws_comma foo.py
输出:
RefactoringTool: No changes to foo.py
RefactoringTool: Files that need to be modified:
RefactoringTool: foo.py
Run Code Online (Sandbox Code Playgroud)
内容foo.py:
print("Hi")
Run Code Online (Sandbox Code Playgroud)
我该如何解释这个输出?
我有点困惑,为什么2to3要麻烦拥抱我已经采用功能样式的打印参数,将其包装在一组额外的括号中。例如
print("\t[Warn] Can not connect {}".format(ssid))
Run Code Online (Sandbox Code Playgroud)
变成
print(("\t[Warn] Can not connect {}".format(ssid)))
Run Code Online (Sandbox Code Playgroud)
这些本质上是保守的假阳性吗?我在想,也许)format函数的尾部抛出了它的逻辑。
我有一个在Python 2.7中定义的类,如下所示:
from future.builtins import object
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
Run Code Online (Sandbox Code Playgroud)
在PyCharm中,这会__init__在行中发出警告:
Signature is not compatible to __new__.
Run Code Online (Sandbox Code Playgroud)
我不明白这个警告告诉我的是什么.有人可以给出一个例子,这个警告会正确地发现错误或者是否可以关闭此警告?
有一个PyCharm线程,但它对我没有帮助:https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000254530-PyCharm-init-Signature-is-not-compatible -to-新-
我有一个需要同时支持Python 2和3的setup.py.
代码当前可以工作,可以在Python 2.x中安装
如果我将该use_2to3 = True子句添加到我的setup.py中,那么该模块可以安装在Python 3中,但是,执行以下操作:
python setup.py test
Run Code Online (Sandbox Code Playgroud)
导致失败,因为其中一个测试使用StringIO类,并且导入行在Python 3 from StringIO import StringIO中蠢(目前,在Python3中它应该是from io import StringIO
我想虽然一旦你添加use_2to3关键字,所有测试(包括unittes)都会在测试之前由2to3处理.
我错过了什么?如果它有帮助,我的setup.py的大部分看起来像:
from setuptools import setup
setup(
name='myproject',
version='1.0',
description='My Cool project',
classifiers = [
'Programming Language :: Python',
'Programming Language :: Python :: 3',
],
py_modules=['mymodule'],
test_suite='test_mymodule',
zip_safe=False,
use_2to3 = True,
)
Run Code Online (Sandbox Code Playgroud)
编辑:我觉得2to3没有运行的原因python setup.py test是它爆炸了,堆栈跟踪的底部是:
File "/home/aparkin/temp/mymodule/test_mymodule.py", line 18, in <module>
from StringIO import StringIO
Run Code Online (Sandbox Code Playgroud)
但是如果我在test_mymodule.py上运行2to3,那么该导入行应该被重新编写为:
from io import StringIO
Run Code Online (Sandbox Code Playgroud)
而且(最坏的情况下)测试应该单独失败.
我有一个 Python 2 包,我正在尝试升级到 Python 3。它是由曾经在我现在所在的同一团队工作但不再在公司工作的人编写的,不幸的是没有人离开团队能够提供帮助。
在包的文件上运行 2to3 后,我跑去python setup.py sdist创建一个包,将包放在本地存储库中,然后尝试使用pip install来安装包。它最终出现以下错误:
Exception:
Traceback (most recent call last):
File "/home/user/project/lib/python3.5/site-packages/pip/basecommand.py", line 223, in main
status = self.run(options, args)
File "/home/user/project/lib/python3.5/site-packages/pip/commands/install.py", line 297, in run
root=options.root_path,
File "/home/user/project/lib/python3.5/site-packages/pip/req/req_set.py", line 622, in install
**kwargs
File "/home/user/project/lib/python3.5/site-packages/pip/req/req_install.py", line 808, in install
self.move_wheel_files(self.source_dir, root=root)
File "/home/user/project/lib/python3.5/site-packages/pip/req/req_install.py", line 1003, in move_wheel_files
isolated=self.isolated,
File "/home/user/project/lib/python3.5/site-packages/pip/wheel.py", line 340, in move_wheel_files
assert info_dir, "%s .dist-info directory not found" % req
AssertionError: my-package-name .dist-info directory …Run Code Online (Sandbox Code Playgroud) 我正在重构一些 python2 代码并使用 2to3 模块将其更改为 python3。我收到以下解析错误:
RefactoringTool: There was 1 error:
RefactoringTool: Can't parse ./helpers/repo.py: ParseError: bad input: type=22, value='=', context=(' ', (45, 25))
Run Code Online (Sandbox Code Playgroud)
这是产生错误的代码:
except ImportError as error_msg: # pragma: no cover
print(' ', file = sys.stderr) # this is a line that yields error
print("Could not locate modifyrepo.py", file=sys.stderr)
print("That is odd... should be with createrepo", file=sys.stderr)
raise ImportError(error_msg)
Run Code Online (Sandbox Code Playgroud)
我不知道出了什么问题。你能帮忙吗?
python-2to3 ×10
python ×9
python-3.x ×4
django ×1
module ×1
pip ×1
pipe ×1
pycharm ×1
python-2.7 ×1
setup.py ×1
typeerror ×1
unit-testing ×1