How to change the default python version in Raspberry Pi

流过昼夜 提交于 2021-02-08 05:16:33

问题


One day ago I did a fresh installation of Raspberry Pi OS Buster and after that I installed Python3.8 in my Raspberry pi following this tutorial. https://installvirtual.com/how-to-install-python-3-8-on-raspberry-pi-raspbian/

I added python alias to bashrc.

echo "alias python=/usr/local/bin/python3.8" >> ~/.bashrc
source ~/.bashrc

Now typing python in terminal showing Python 3.8.0 (default, Jun 8 2020, 13:17:16)

But when I run python3 it's showing Python Python 3.7.3

I added python3 alias pointing to python3.8 follwing above commands but still no luck. Programs from Geany still showing 3.7. I changed Geany's bulid commands to python(as I set default python to 3.8)

#!/usr/local/bin/python3.8

import sys
print("Python version")
print (sys.version)

Python version 3.7.3 (default, Dec 20 2019, 18:57:59)

I have two questions:

  1. How to run programs in Python3.8?

  2. Can I uninstall python3.7?


回答1:


I've found a couple references to this solution online. I have not seen an "official" solution from Raspberry Pi Foundation but this seems to work well.

Step 1

Add both (all) versions of python installed to the list of "alternatives" for the python binary.

sudo update-alternatives --install $(which python) python $(which $(readlink $(which python2))) 1
sudo update-alternatives --install $(which python) python $(which $(readlink $(which python3))) 2

Step 2

Select desired version:

sudo update-alternatives --config python



回答2:


For those of you who like working directly with the filesystem, here is what I did:

  • type: sudo rm /usr/bin/python
  • type: sudo ln -s /usr/bin/python3 /usr/bin/python
  • type: ls -l /usr/bin/python
    • lrwxrwxrwx 1 root root 16 Jan 18 11:04 /usr/bin/python -> /usr/bin/python3
  • type: python -V
    • Python 3.7.3
  • alternative way:
    • type: sudo update-alternatives --config python
    • In my case no alternatives where found...

While you are at it, change pip default to pip3, slightly different process...

  • type: sudo mv /usr/bin/pip /usr/bin/pip2 # rename
  • type: sudo ln -s /usr/bin/pip3 /usr/bin/pip
  • type: ls -l /usr/bin/pip
    • lrwxrwxrwx 1 root root 13 Jan 18 11:19 /usr/bin/pip -> /usr/bin/pip3
  • type: pip -V
    • pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
    • NB: pip2 gets confused by switchover to default python3, as it starts with: #!/usr/bin/python


来源:https://stackoverflow.com/questions/62275714/how-to-change-the-default-python-version-in-raspberry-pi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!