在Python 3.3上执行"repo init"时出现TypeError错误。

7

我使用的是Arch Linux操作系统, Python版本为3.3.0。 我已经下载了最新的代码库,但是如果我尝试从Google提供的示例进行repo init,就会出现以下错误:

 [username@otp-username-l2 teste]$ repo init -u https://android.googlesource.com/platform/manifest
 Traceback (most recent call last):
 File "/home/username/bin/repo", line 738, in <module>
main(sys.argv[1:])
File "/home/username/bin/repo", line 705, in main
_Init(args)
 File "/home/username/bin/repo", line 234, in _Init
_CheckGitVersion()
 File "/home/username/bin/repo", line 274, in _CheckGitVersion
if not ver_str.startswith('git version '):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

我被迫进行新的repo init的原因是,我必须从已初始化的repo中进行提交,但我已经在各处更改了git用户,但仍然出现以下错误:

Writing objects: 100% (12/12), 966 bytes, done.
Total 12 (delta 11), reused 0 (delta 0)
o ssh://new.username@128.224.0.74:29418/stelvio/mm
![remote rejected] branchname -> refs/for/main_dev (you are not committer  oldusername@email.com)
 error: failed to push some refs to 'ssh://new.username@128.224.0.74:29418/project/one'
3个回答

10

仓库还不完全支持Python 3,尽管已经完成了一些工作,例如使用print函数导入正确的urllib库,但这项工作似乎还没有完成。

目前你需要使用Python 2来运行它。你可以通过将repo可执行文件顶部的shebang修改为python2或者运行以下命令:

python2 `which repo`

假设您的计算机上安装了Python 2版本,并将其添加到了路径中,名称为python2
您可以很容易地重现这个问题:
Python 3.2.3 (default, Nov  7 2012, 19:36:04) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> b'asd'.startswith('asd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

以下是与 _CheckGitVersion() 相关的代码:

def _CheckGitVersion():
  cmd = [GIT, '--version']
  try:
    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)

   ...

  ver_str = proc.stdout.read().strip()
  proc.stdout.close()
  proc.wait()

  if not ver_str.startswith('git version '):

调用Popen后读取stdout返回的是bytes,因此传递给startswith的内容也必须是bytes(原始数据的字节),而不是str(Unicode代码点序列)。


如果你仍然遇到相同的错误,那么你仍在使用Python 3运行它。如果是其他错误,请发布详细信息。 - agf
虽然这是一个非常老的帖子,但是有人能否确认当前的repo launcher版本2.30是否支持python3?@agf - santosh verma
1
@santoshverma 是的,这是Python 3。 - agf

8

在尝试恢复P990时,我偶然遇到了这个错误。

第一个解决方案是修改repo命令(在您的情况下位于~/bin中,否则检查哪个repo以找到其位置),并将第一行从以下内容更改为:

#!/usr/bin/env python

to

#!/usr/bin/env python2

这使你能够进行 repo init,但你会遇到 klauspeter 提到的信息:
error: Python 3 support is not fully implemented in repo yet.
Please use Python 2.6 - 2.7 instead.

我不建议更改系统范围的符号链接。相反,进入新创建的.repo文件夹。
如果你在$basedir中开始repo init,则要检查$basedir/.repo/repo。里面你会找到一个本地repo安装,再次使用纯粹的“python”(我们需要python2)。
因此,根据上面的第一步编辑所有包含该行的文件(main.py,repo和wrapper.py),然后就可以继续进行了。对我来说,现在repo甚至要求我更新全局安装(即将$basedir/.repo/repo/repo复制到~/bin),你可以自由地这样做(该版本现在已经“修复”)。

1
我遇到了同样的问题,agf的解决方案使repo脚本能够工作,但它仍然退出并声明它无法与Python 3一起使用:
错误:repo中尚未完全实现Python 3支持。请改用Python 2.6-2.7。
对我而言(虽然有点不太规范),解决方法是将python bin的符号链接从python3更改为python2。
记得在完成后恢复!

1
这是一个糟糕的解决方案。 - Felipe Tonello

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接