树莓派、ubuntu低版本python3安装库

如果遇到树莓派中自带低版本python3,又不想额外去安装python3时,可能会遇到版本过低,无法安装库的情况,以下用我实际情况举例解决方案。

本次遇到的问题是树莓派低版本中,python3为3.7.3,需要安装numpy,出现卡在以下内容的情况:

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/45/b7/de7b8e67f2232c26af57c205aaad29fe17754f793404f59c8a730c7a191a/numpy-1.21.6.zip (10.3MB)
    100% |████████████████████████████████| 10.3MB 51kB/s 
  Installing build dependencies ... done
Building wheels for collected packages: numpy
  Running setup.py bdist_wheel for numpy ... \

 一、升级软件包管理

sudo apt-get update
sudo apt-get upgrade

二、安装pip

树莓派中不默认安装pip,需要额外安装:

sudo apt-get install python3-pip

 也可以额外再装一个虚拟环境来进行python的虚拟环境创建:

sudo apt-get install python3-venv

三、升级setuptools

sudo pip3 install -U pip setuptools 

解决的关键就在于升级一下setuptools,再安装相应库即可:

pi@raspberrypi:~ $ pip3 install numpy
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting numpy
  Downloading numpy-1.21.6-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.metadata (2.1 kB)
Downloading numpy-1.21.6-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (13.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.0/13.0 MB 6.5 MB/s eta 0:00:00
Installing collected packages: numpy
  WARNING: The scripts f2py, f2py3 and f2py3.7 are installed in '/home/pi/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.21.6

四、注意事项

如果使用systemctl执行应用,由于用的是root用户来执行,而不是用pi,因此会发现安装的库无法引用,例如:

ModuleNotFoundError: No module named 'serial'

 这是由于在pi用户下安装的python3-pip在/home/pi/.local进行配置,因此root用户下无法获取到/home/pi/.local,而是使用的是/root/.local的内容。

4.1使用虚拟环境解决 

 使用虚拟环境可以解决上面的问题,但是在虚拟环境中安装numpy还会遇到升级setuptools的问题:

# 更换自己的虚拟环境路径
sudo /home/pi/Env/bin/pip install -U pip setuptools 

 在systemctl中配置:

ExecStart=/home/pi/Env/bin/python /home/pi/run.py

五、总结

安装版本较低的python3的库时,很多东西还是需要升级一下,不然会遇到不知名的情况,网上资料还少。

相关推荐

  1. 树莓5 Ubuntu 23.04 安装 DisplayLink 驱动

    2024-05-12 10:26:05       38 阅读
  2. 树莓4B上安装Ubuntu Server 20

    2024-05-12 10:26:05       19 阅读
  3. 树莓 ubuntu22.04 meta安装 ros2-humble

    2024-05-12 10:26:05       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-12 10:26:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-12 10:26:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-12 10:26:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-12 10:26:05       20 阅读

热门阅读

  1. ubuntu 22.04下面安装cuda、cudnn等的配置过程

    2024-05-12 10:26:05       11 阅读
  2. 云计算的优势与未来发展

    2024-05-12 10:26:05       11 阅读
  3. Android APP 剪切板应用

    2024-05-12 10:26:05       8 阅读
  4. 神经网络模型与前向传播函数

    2024-05-12 10:26:05       8 阅读
  5. iOS collectionViewCell显示选中颜色

    2024-05-12 10:26:05       12 阅读
  6. Python从0到POC编写--模块(二)

    2024-05-12 10:26:05       8 阅读
  7. RF-315MHz和RF-433MHz通用解码程序

    2024-05-12 10:26:05       11 阅读