简体   繁体   English

尽管我努力运行 python3,为什么我的系统仍在运行 python2?

[英]Why is my system running python2 despite all my efforts to run python3?

I'm having trouble running a script downloaded from an Android repository.我无法运行从 Android 存储库下载的脚本。 Based on related questions and on my own testing, I'm pretty sure that the problem is that it's being interpreted by Python2, when it was written for Python3.基于相关问题和我自己的测试,我很确定问题在于它是由 Python2 解释的,而它是为 Python3 编写的。

Here's my attempt at reproducing the problem in a script of my own:这是我在自己的脚本中重现问题的尝试:

#!/usr/bin/env python3

# test.py
import sys
print('hello', file=sys.stderr)

And here are the test steps:以下是测试步骤:

$ alias python=python3
$ python --version
Python 3.6.9
$ python test.py
hello
$ unalias python
$ python --version
Python 2.7.17
$ python test.py
  File "test.py", line 5
    print('hello', file=sys.stderr)
                       ^
SyntaxError: invalid syntax

What's confusing is that, while this test executes as expected, when I use repo to attempt to install Android Open Source Project, I get the SyntaxError (from essentially an identical line of script), despite running alias python=python3 .令人困惑的是,虽然测试按预期执行,但当我使用repo尝试安装 Android 开源项目时,尽管运行alias python=python3 ,但我得到了SyntaxError (来自基本上相同的脚本行)。 (The shebang doesn't seem to affect the test or the main script.) (shebang 似乎不会影响测试或主脚本。)

What am I missing?我错过了什么? How can I run this script using the correct version of python3?如何使用正确版本的 python3 运行此脚本? And assuming there is a workaround, how do I clean up afterwards so that the rest of my system can still access python2 when it wants to?并假设有一个解决方法,之后我该如何清理,以便我的系统的 rest 仍然可以在需要时访问 python2?

alias only changes the interpretation of commands you directly type into your shell. alias只会更改您直接输入 shell 的命令的解释。 repo is therefore unaffected, because it doesn't type the python command into your shell.因此repo不受影响,因为它不会在 shell 中键入 python 命令。

Usually the best way to run a different Python configuration from the system's is to use virtualenvs.通常从系统运行不同的 Python 配置的最佳方法是使用 virtualenvs。

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

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