问题

在用pip安装依赖pip install -r requirements.txt的时候碰到如下报错:

ModuleNotFoundError: No module named '_ctypes'

原因:Python3中有个内置模块叫ctypes,它是Python3的外部函数库模块,它提供兼容C语言的数据类型,并通过它调用Linux系统下的共享库(Shared library),此模块需要使用CentOS7系统中外部函数库(Foreign function library)的开发链接库(头文件和链接库)。由于在CentOS7系统中没有安装外部函数库(libffi)的开发链接库软件包,所以在安装pip的时候就报了ModuleNotFoundError: No module named '_ctypes'的错误。

解决办法

前言

国内云服务器使用pip默认源的下载速度真是一言难尽。

修改配置文件

  • Linux
    在home目录下建立一个隐藏的pip目录,然后再创建一个名为pip.conf的配置文件,在pip.conf文件中输入如下内容即可
mkdir ~.pip
vim ~.pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
:x
  • Windows
    在c:user用户名 目录中创建pip目录,在目录里新建pip.ini文件,输入以下内容:
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

命令行修改

  • 命令行临时修改
    在用pypi下载python包的时候,可以直接使用pip install packagename -i参数,指定下载源的URL
pip install ping3 -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 命令行永久修改
    使用pip config set global.index-url来直接指定下载源的URL,这样就不用手动修改配置文件了
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

国内源

以下是国内互联网常用的pypi安装源URL,选用其中一个即可

pypi 清华大学源:https://pypi.tuna.tsinghua.edu.cn/simple
pypi 腾讯源:http://mirrors.cloud.tencent.com/pypi/simple
pypi 阿里源:https://mirrors.aliyun.com/pypi/simple/
pypi 豆瓣源 :http://pypi.douban.com/simple/

  • 安装依赖和yasm
[root@ECS ~]# yum install autoconf automake bzip2 bzip2-devel cmake freetype-devel gcc gcc-c++ git libtool make pkgconfig zlib-devel openssl-devel
[root@ECS ~]# yum install yasm​
  • 下载并解压源码包
[root@ECS ~]# wget http://ffmpeg.org/releases/ffmpeg-4.4.1.tar.gz
[root@ECS ~]# tar -xvf ffmpeg-4.4.1.tar.gz
  • 编译安装
[root@ECS ~]# cd ffmpeg-4.4.1
[root@ECS ffmpeg-4.4.1]# ./configure --enable-shared --enable-openssl --enable-protocols --enable-protocol=https --prefix=/usr/local/ffmpeg​
[root@ECS ffmpeg-4.4.1]# make && make install
  • 配置动态库路径
[root@ECS ~]# vi /etc/ld.so.conf
# 末尾加入/usr/local/ffmpeg/lib
[root@ECS ~]# ldconfig
  • 配置环境变量
[root@ECS ~] vi /etc/profile
# 文件末尾加入
PATH=$PATH:/usr/local/ffmpeg/bin
export PATH
[root@ECS ~] source /etc/profile
  • 查看FFMPEG版本
[root@ECS ~] ffmpeg -version
ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-44)
configuration: --enable-shared --enable-openssl --enable-protocols --enable-protocol=https --prefix=/usr/local/ffmpeg
libavutil      56. 70.100 / 56. 70.100
libavcodec     58.134.100 / 58.134.100
libavformat    58. 76.100 / 58. 76.100
libavdevice    58. 13.100 / 58. 13.100
libavfilter     7.110.100 /  7.110.100
libswscale      5.  9.100 /  5.  9.100
libswresample   3.  9.100 /  3.  9.100

阅读全文 »

  • 下载并解压源码包
[root@ECS ~]# wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
[root@ECS ~]# tar -xvf yasm-1.3.0.tar.gz
  • 编译安装
[root@ECS ~]# yum install gcc -y
[root@ECS ~]# cd yasm-1.3.0
[root@ECS yasm-1.3.0]# ./configure --prefix=/usr/local/yasm
[root@ECS yasm-1.3.0]# make && make install
  • 创建软连接
[root@ECS yasm-1.3.0]# ln -s /usr/local/yasm/bin/yasm /usr/bin/yasm
  • 验证
[root@ECS yasm-1.3.0]# yasm --version
yasm 1.3.0
Compiled on Apr 13 2022.
Copyright (c) 2001-2014 Peter Johnson and other Yasm developers.
Run yasm --license for licensing overview and summary.

在云服务器上装nvm发现安装脚本报错,提示Failed to connect to ::: No route to host无法访问raw.githubusercontent.com

[root@ecs-09077087-007 ~]# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to ::: No route to host

ping raw.githubusercontent.com发现被重定向到了127.0.0.1

[root@ecs-09077087-007 ~]# ping raw.githubusercontent.com
PING raw.githubusercontent.com (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.014 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.038 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.020 ms
64 bytes from localhost (127.0.0.1): icmp_seq=6 ttl=64 time=0.026 ms
64 bytes from localhost (127.0.0.1): icmp_seq=7 ttl=64 time=0.025 ms
64 bytes from localhost (127.0.0.1): icmp_seq=8 ttl=64 time=0.026 ms
^C
--- raw.githubusercontent.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7009ms
rtt min/avg/max/mdev = 0.014/0.027/0.038/0.010 ms

raw.githubusercontent.com.jpg
可以看到受墙影响国内抽风的还是比较厉害的,根据ipaddress.com查询raw.githubusercontent.com地址

Q:What IP addresses does raw.githubusercontent.com resolve to?
A:raw.githubusercontent.com resolves to 4 IPv4 addresses and 4 IPv6 addresses:
185.199.108.133
185.199.109.133
185.199.110.133
185.199.111.133
2606:50c0:8000::154
2606:50c0:8001::154
2606:50c0:8002::154
2606:50c0:8003::154

在云服务器上选择延迟较低的地址写入hosts文件

[root@ecs-09077087-007 ~]# vi /etc/hosts
185.199.108.133 raw.githubusercontent.com

再ping raw.githubusercontent.com可以看到已经ping通了

[root@ecs-09077087-007 ~]# ping raw.githubusercontent.com
PING raw.githubusercontent.com (185.199.108.133) 56(84) bytes of data.
64 bytes from raw.githubusercontent.com (185.199.108.133): icmp_seq=1 ttl=43 time=83.5 ms
64 bytes from raw.githubusercontent.com (185.199.108.133): icmp_seq=2 ttl=43 time=80.9 ms
64 bytes from raw.githubusercontent.com (185.199.108.133): icmp_seq=3 ttl=43 time=89.1 ms
64 bytes from raw.githubusercontent.com (185.199.108.133): icmp_seq=4 ttl=43 time=78.0 ms
^C
--- raw.githubusercontent.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3010ms
rtt min/avg/max/mdev = 78.028/82.919/89.175/4.115 ms

BTW国内访问github不稳定,推荐个利器dev-sidecar