简体   繁体   中英

On a mac Big Sur v 11.6, the terminal won't let me run anything with Python

I just installed python 3.10 and I followed a tutorial that told me to create an alias so that the pre-installed 2.7.6 won't run. I tried to use

nano ~/.bash_profile

and add

alias python="python3" 

but that wouldn't work to change my version.

Then I tried using

nano ~/.zprofile

and add

alias python="python3"

Now everytime I run a simple python file that tries to execute print("hello world"), it comes up with SyntaxError: invalid decimal literal . There is no variable in the python code.

@Carters-MBP ~ % python3 Desktop/intro.p.py
  File "/Users/carternetzley/Desktop/intro.p.py", line 1
    Python 3.10.6 (v3.10.6:9c7b4bd164, Aug  1 2022, 17:13:48) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
                           ^
SyntaxError: invalid decimal literal

any ideas?

Opinionated answer follows...

Don't use aliases for Python. They don't work very well:

  • in IDEs, or
  • cron jobs or
  • subprocesses or non-interactive logins.

Remove all Python aliases and restart your machine.

Use the type command to find out what is actually run when you execute a command. So if you want to know what your shell will actually run when you do python3 , use:

type python3

If you want to know what your shell will run when you do python , use:

type python

Your shell doesn't "prevent" you running anything. It simply runs the first matching thing you have in your PATH.

So if you have installed Python 3.10 at /Applications/Python3.10/bin/python , simply set your PATH to:

export PATH=/Applications/Python3.10/bin:$PATH

The problem is all in your file. What you have in intro.p.py is not a Python script, it is a transcript of a terminal session. It includes the Python header line which, of course, is not valid code. You need to edit that file and make it a Python script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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