在Python 3.3上尝试“repo init”时出现TypeError

2024-04-18 22:01:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Arch Linux Python3.3.0 我已经下载了最新的repo,如果我尝试从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执行一个commit,但是我已经从任何地方更改了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'

Tags: incomhomebytesbininitmainline
3条回答

只是在试图恢复我的P990时偶然发现了这个错误。

第一个修复方法是修改repo命令(在~/bin中,否则请检查要查找的repo位置),并将第一行从

#!/usr/bin/env python

#!/usr/bin/env python2

这允许您重新初始化,但您将遇到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'的shebang(我们想要python2)。

因此,按照上面的第一步编辑包含该行(main.py、repo和wrapper.py)的所有文件,就可以了。对于我,repo现在甚至要求我更新我的全局安装(即,将$basedir/.repo/repo/repo复制到~/bin),这是您可以自由完成的(该版本现在已“修复”)。

Repo还没有完全支持Python 3。已经做了一些工作,例如using the ^{} functionimporting the correct urllib,但这项工作似乎还没有完成。

现在,您需要将它与Python 2一起使用。您可以用python2替换python来编辑repo可执行文件顶部的shebang,也可以运行:

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 '):

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

我遇到了同样的问题,agf的解决方案使repo脚本工作,但它仍然退出,声明它不能与python 3一起工作:

error: Python 3 support is not fully implemented in repo yet.
Please use Python 2.6 - 2.7 instead.

对我来说,解决这个问题的方法(当然相当肮脏)是将python3的python2的符号链接改为python3。

一旦你完成了,记得把它还原!

相关问题 更多 >