Orangepi 香橙派 Debian 系统下安装 Python2 Python3 填坑记录

香橙派上手Python
本文详细介绍在Orangepi-PC香橙派上安装配置Python2和Python3环境的全过程,包括安装pip,解决依赖冲突,更换国内源,批量更新库至最新版本,以及安装常用库smbus2和wxpy。

硬件准备:Orangepi-PC 香橙派 (早期的型号,1G RAM ,资源还是很丰富的)

软件准备:Debian_jessie_mini.img (来自官网的系统),MobaXterm

一、安装 Python2 Python3

在之前的博文中,已经详细记述了从官网下载Debian系统并烧写至TF卡以及更新系统的过程,下面的操作默认系统已经更新完毕,更新源已换为国内源。

越来越多的Python程序采用Python3环境开发,但是仍有部分程序只支持Python2,所以两个环境都安装,方便使用。先来安装Python2。

先说个题外话,以后连接香橙派都使用MobaXterm了,不再使用SecureCRT

比SecureCRT好用太多

安装Python2

root@OrangePI:/# apt-get install python
Reading package lists... Done
Building dependency tree
Reading state information... Done
python is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

说明系统已经自带了Python2,目前版本最新,来看看具体版本

root@OrangePI:/# python -V
Python 2.7.9

确实是2系列里最新的版本。

安装Python3

apt-get install python3

安装完毕,来看看Python3的版本

root@OrangePI:/# python3 -V
Python 3.4.2
root@OrangePI:/# python -V
Python 2.7.9

可见,python默认指的是python2 。

现在来看看有没有pip

root@OrangePI:/# pip -V
-bash: pip: command not found

那我们来继续安装pip

二、安装pip

安装pip需要指定python的版本,先安装python2的

apt-get install python-pip

哗啦哗啦安装了50多个包。。。再来安装python3的pip

apt-get install python3-pip

又是哗啦哗啦安装了一堆包。现在来看看pip的版本

root@OrangePI:/# pip -V
pip 1.5.6 from /usr/lib/python2.7/dist-packages (python 2.7)
root@OrangePI:/# pip3 -V
pip 1.5.6 from /usr/lib/python3/dist-packages (python 3.4)

可以看到,pip默认指的是对应python2版本的,并且,这个pip的版本也太低了点,1.5.6  。。。 事实上,之前的博文里有一处报错,就是pip的版本太低引起的。

root@OrangePI:~# pip install pillow 
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    load_entry_point('pip==1.5.6', 'console_scripts', 'pip')()
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 356, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2476, in load_entry_point
    return ep.load()
  File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2190, in load
    ['__name__'])
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 74, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/lib/python2.7/dist-packages/pip/vcs/mercurial.py", line 9, in <module>
    from pip.download import path_to_url
  File "/usr/lib/python2.7/dist-packages/pip/download.py", line 25, in <module>
    from requests.compat import IncompleteRead
ImportError: cannot import name IncompleteRead
root@OrangePI:~# 

stackoverflow.com 某个帖子也是说应该升级pip

This problem is caused by a mismatch between your pip installation and your requests installation.

As of requests version 2.4.0 requests.compat.IncompleteRead has been removed. Older versions of pip, e.g. from July 2014, still relied on IncompleteRead. In the current version of pip, the import of IncompleteRead has been removed.

So the one to blame is either:

requests, for removing public API too quickly
Ubuntu for updating pip too slowly
You can solve this issue, by either updating pip via Ubuntu (if there is a newer version) or by installing pip aside from Ubuntu.

那我们来安装最新的pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
python3 get-pip.py

这样就分别安装了对应python2和python3版本的pip。再来看看pip的版本

root@OrangePI:/# pip -V
-bash: /usr/bin/pip: No such file or directory

哦NO,怎么会提示没有这个文件呢?不要着急,其实是在刚刚安装pip的过程中,把前面较低版本的pip给卸载了,系统一时没反应过来,我们重启一下试试。

root@OrangePI:~# pip -V
pip 18.1 from /usr/local/lib/python3.4/dist-packages/pip (python 3.4)
root@OrangePI:~# pip2 -V
pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

没问题,可以了,并且pip默认对应python3的版本。

三、一些善后处理

pip默认的源在国外,速度太慢。我们来替换成国内源,就用豆瓣的源好了。

先创建文件

~/.pip/pip.conf

编辑pip.conf,写入以下内容

[global]
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com
disable-pip-version-check = true
timeout = 120
[install]
ignore-installed = true
no-dependencies = yes

现在来安装一个用于操作I2C硬件的库 smbus2

root@OrangePI:/# pip install smbus2
Looking in indexes: http://pypi.douban.com/simple
Collecting smbus2
  Downloading http://pypi.doubanio.com/packages/c6/be/6eab4b27693ec2c87f7ff864dfca86c58fbfd1627acbe191dd2f18e0ac3e/smbus2-0.2.1.tar.gz
    Complete output from command python setup.py egg_info:
    error in smbus2 setup command: 'extras_require' must be a dictionary whose values are strings or lists of strings containing valid project/version requirement specifiers.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-ka9nhttv/smbus2/

有点不爽呀,又报错了。这个是 setuptools 版本太低引起的。我们来看看现有的版本

root@OrangePI:/# pip list
Package    Version
---------- -------
chardet    2.3.0
colorama   0.3.2
html5lib   0.999
pip        18.1
requests   2.4.3
setuptools 5.5.1
six        1.8.0
urllib3    1.9.1
wheel      0.24.0

或者直接看和最新的版本对比

root@OrangePI:/# pip list  --outdated --format=columns
Package    Version Latest Type
---------- ------- ------ -----
chardet    2.3.0   3.0.4  wheel
colorama   0.3.2   0.4.0  wheel
html5lib   0.999   1.0.1  wheel
requests   2.4.3   2.20.1 wheel
setuptools 5.5.1   40.6.2 wheel
six        1.8.0   1.11.0 wheel
urllib3    1.9.1   1.24.1 wheel
wheel      0.24.0  0.32.3 wheel

我勒个去,这么多低版本。一个一个升级太麻烦,我们来批量更新。

pip install packaging
pip install pip-review

批量更新

root@OrangePI:/# pip-review --local --interactive
chardet==3.0.4 is available (you have 2.3.0)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit a
colorama==0.4.0 is available (you have 0.3.2)
html5lib==1.0.1 is available (you have 0.999)
requests==2.20.1 is available (you have 2.4.3)
setuptools==40.6.2 is available (you have 5.5.1)
six==1.11.0 is available (you have 1.8.0)
urllib3==1.24.1 is available (you have 1.9.1)
wheel==0.32.3 is available (you have 0.24.0)
Looking in indexes: http://pypi.douban.com/simple
Collecting chardet==3.0.4
  Downloading http://pypi.doubanio.com/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 6.0MB/s
Collecting colorama==0.4.0
  Downloading http://pypi.doubanio.com/packages/0a/93/6e8289231675d561d476d656c2ee3a868c1cca207e16c118d4503b25e2bf/colorama-0.4.0-py2.py3-none-any.whl
Collecting html5lib==1.0.1
  Downloading http://pypi.doubanio.com/packages/a5/62/bbd2be0e7943ec8504b517e62bab011b4946e1258842bc159e5dfde15b96/html5lib-1.0.1-py2.py3-none-any.whl (117kB)
    100% |████████████████████████████████| 122kB 5.7MB/s
Collecting requests==2.20.1
  Downloading http://pypi.doubanio.com/packages/ff/17/5cbb026005115301a8fb2f9b0e3e8d32313142fe8b617070e7baad20554f/requests-2.20.1-py2.py3-none-any.whl (57kB)
    100% |████████████████████████████████| 61kB 5.3MB/s
Collecting setuptools==40.6.2
  Downloading http://pypi.doubanio.com/packages/e7/16/da8cb8046149d50940c6110310983abb359bbb8cbc3539e6bef95c29428a/setuptools-40.6.2-py2.py3-none-any.whl (573kB)
    100% |████████████████████████████████| 573kB 5.7MB/s
Collecting six==1.11.0
  Downloading http://pypi.doubanio.com/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting urllib3==1.24.1
  Downloading http://pypi.doubanio.com/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)
    100% |████████████████████████████████| 122kB 6.8MB/s
Collecting wheel==0.32.3
  Downloading http://pypi.doubanio.com/packages/ff/47/1dfa4795e24fd6f93d5d58602dd716c3f101cfd5a77cd9acbe519b44a0a9/wheel-0.32.3-py2.py3-none-any.whl
Installing collected packages: chardet, colorama, html5lib, requests, setuptools, six, urllib3, wheel
Successfully installed chardet-3.0.4 colorama-0.4.0 html5lib-1.0.1 requests-2.20.1 setuptools-40.6.2 six-1.11.0 urllib3-1.24.1 wheel-0.32.3

一次更新完毕。再来安装smbus2看看

root@OrangePI:/# pip install smbus2
Looking in indexes: http://pypi.douban.com/simple
Collecting smbus2
  Downloading http://pypi.doubanio.com/packages/c6/be/6eab4b27693ec2c87f7ff864dfca86c58fbfd1627acbe191dd2f18e0ac3e/smbus2-0.2.1.tar.gz
Building wheels for collected packages: smbus2
  Running setup.py bdist_wheel for smbus2 ... done
  Stored in directory: /root/.cache/pip/wheels/ec/4f/4e/f04ff003ffc92b373e626bf717a7db4ebb0f5e01c488ee22a4
Successfully built smbus2
Installing collected packages: smbus2
Successfully installed smbus2-0.2.1

安装成功。

再来安装另一篇博文里的 wxpy (微信机器人)看看。

root@OrangePI:/# pip install wxpy
Looking in indexes: http://pypi.douban.com/simple
Collecting wxpy
  Downloading http://pypi.doubanio.com/packages/6b/ac/8f1c5bc46f0127f68be5be64c699179b538b7cb21f85dfc4561551489f36/wxpy-0.3.9.8.tar.gz (45kB)
    100% |████████████████████████████████| 51kB 6.2MB/s
Building wheels for collected packages: wxpy
  Running setup.py bdist_wheel for wxpy ... done
  Stored in directory: /root/.cache/pip/wheels/69/0d/dd/4a9a44c95e243eb1a75a27f68a434658a6057a7c8857982aeb
Successfully built wxpy
Installing collected packages: wxpy
Successfully installed wxpy-0.3.9.8

也没问题。

当然,上面操作的都是python3环境下的,如果需要更新python2环境下的,如法炮制即可。

如遇

fatal error: Python.h: No such file or directory

那么

apt-get install python-dev libevent-dev

如遇

fatal error: ffi.h: No such file or directory

那么

apt-get install libffi-dev

如遇

fatal error: openssl/opensslv.h: No such file or directory

那么

apt-get install libssl-dev

 

<think>我们正在处理用户的问题:如何在香橙5上安装Python?根据提供的引用内容,我们可以总结出几个步骤。但是注意,引用内容中提到了多个方法,包括从源代码编译安装以及使用apt安装相关工具。用户使用的是香橙5,我们假设其操作系统是基于Debian的(如Ubuntu或OrangePi自己的OS),因此我们可以使用apt包管理器。根据引用[3]:安装Python3的步骤较为详细,但它是针对特定版本(3.9.10)的编译安装方法。而引用[2]中提到了使用apt安装一些依赖和工具。通常,在Debian系统中,我们可以直接使用apt安装Python,但可能系统已经预装了Python3。因此,我们首先检查系统是否已经安装,如果没有安装或者需要安装特定版本,可以选择从源安装。但考虑到用户可能只需要安装Python(不指定版本,或者安装最新稳定版),我们可以提供两种方法:方法1:使用系统包管理器安装(简单,但版本可能不是最新)方法2:从源代码编译安装(可以安装最新版本,但过程较长)根据引用[2]和[3]的内容,我们结合整理。步骤:方法1:使用apt安装1.更新软件包列表:`sudoaptupdate`2.安装Python3:`sudoaptinstallpython3`3.验证:`python3--version`方法2:从源代码编译安装(以Python3.9.10为例,但可以替换为其他版本)1.安装编译依赖:```bashsudoaptupdatesudoaptinstall-ybuild-essentialzlib1g-devlibncurses5-devlibgdbm-devlibnss3-devlibssl-devlibsqlite3-devlibreadline-devlibffi-devcurllibbz2-dev```2.下载Python源码(例如3.9.10)并解压:```bashwgethttps://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgztarxvfPython-3.9.10.tgz```3.进入源码目录,配置并编译安装:```bashcdPython-3.9.10./configure--enable-optimizations#全志官方指引中使用了这个配置选项make-j4#使用4个核心编译,根据你的CPU核心数调整sudomakeinstall```4.验证:`python3--version`注意:从源代码安装后,通常安装Python可执行文件为`python3`,并且会覆盖系统原有的链接(如果之前有安装的话)。另外,使用`makeinstall`会将Python安装到`/usr/local/bin`,因此优先级可能高于系统包管理器安装的版本。但是,用户可能还需要安装pip(Python包管理工具)。在方法1中,通常安装`python3-pip`包即可:`sudoaptinstallpython3-pip`在方法2中,编译安装Python通常已经包含了pip,如果没有,可以单独安装。另外,引用[2]中提到了安装wiringOP-Python的依赖,其中第一步就是安装git、swig等,但那是为了安装wiringOP-Python库,与安装Python本身关系不大。因此,我们给用户的回答应该包括两种方法,并说明其适用情况。另外,根据用户的需求,他可能只是需要Python环境,所以方法1是最简单的。但如果需要特定版本,则使用方法2。我们还可以参考引用[3]中的步骤,但注意该步骤是针对没有预装Python3的情况,且从源码编译。考虑到香橙5的性能,编译过程可能会比较长。现在,我们按照用户要求的结构化方式回答,并生成相关问题。</think>### 如何在香橙5上安装Python 香橙5(Orange Pi 5)通常预装基于Debian的Linux系统(如Orange Pi OS或Ubuntu),安装Python的推荐方法如下: #### ✅ 方法1:安装系统预编译版本(推荐) ```bash # 1. 更新软件源 sudo apt update # 2. 安装Python3及包管理工具 sudo apt install python3 python3-pip python3-venv -y # 3. 验证安装 python3 --version # 应显示版本号如 Python 3.9.2 pip3 --version # 应显示pip版本 ``` #### ⚙ 方法2:源码编译安装(需特定版本时) ```bash # 1. 安装编译依赖[^3] sudo apt install -y build-essential zlib1g-dev libncurses5-dev \ libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev \ libffi-dev curl libbz2-dev # 2. 下载源码(以Python 3.9.10为例) wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz tar xvf Python-3.9.10.tgz cd Python-3.9.10 # 3. 配置并编译[^3] ./configure --enable-optimizations make -j$(nproc) # 使用所有CPU核心加速编译 # 4. 安装系统 sudo make altinstall # 使用altinstall避免覆盖系统默认Python # 5. 验证 python3.9 --version ``` #### 🔧 关键说明: 1. **预装情况**:香橙5默认已安装Python 3.x,执行 `python3 --version` 确认 2. **虚拟环境**:建议使用 `python3 -m venv myenv` 创建隔离环境 3. **GPIO控制**:若需操作GPIO引脚,需额外安装 `wiringOP-Python`[^1][^2] 4. **pip使用**:通过 `pip3 install 包名` 安装第三方库 > 注:源码编译约需30-60分钟(取决于SD卡速度),建议连接电源操作[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鹏集成

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值