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

Viewed 67

I'm having trouble running a script downloaded from an Android repository. 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.

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. (The shebang doesn't seem to affect the test or the main script.)

What am I missing? How can I run this script using the correct version of 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?

1 Answers

alias only changes the interpretation of commands you directly type into your shell. repo is therefore unaffected, because it doesn't type the python command into your shell.

Usually the best way to run a different Python configuration from the system's is to use virtualenvs.

Related