一、背景:
CentOS7.7上默认已经存在python2.7,但需要python3,所以计划源码安装python3。
下载python3.10.8
wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz
二、编译安装
安装用户为普通linux用户,拥有sudo权限
tar -zxf Python-3.10.8.tgz
cd Python-3.10.8.tgz
./configure --prefix=/usr/local/python3108
sudo make && sudo make install
源码安装完成后,有一些warning信息。
三、检查可执行文件
python3 -V
pip3 -V执行报错,报错信息如下:
Traceback (most recent call last):
File "/usr/local/python3108/bin/pip3", line 5, in <module>
from pip._internal.cli.main import main
ModuleNotFoundError: No module named 'pip._internal'
四、考虑更新pip3
执行python3 -m pip install --upgrade pip报错,报错信息如下:
/usr/local/python3108/bin/python3: No module named pip.__main__; 'pip' is a package and cannot be directly executed
五、检查pip模块
执行sudo /usr/local/python3108/bin/python3 -m ensurepip显示已存在相关包
Looking in links: /tmp/tmp3pdc2q20
Requirement already satisfied: setuptools in /usr/local/python3108/lib/python3.10/site-packages (63.2.0)
Requirement already satisfied: pip in /usr/local/python3108/lib/python3.10/site-packages (22.2.2)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
六、检查目录权限
发现/usr/local/python3108/lib/python3.10/site-packages子目录权限不对,普通用户无法调用。
七、再次检查python3和pip3可执行权限,
sudo /usr/local/python3108/bin/python3 -V
sudo /usr/local/python3108/bin/pip3 -V
使用sudo执行成功
八、确认python目录权限
检查发现/usr/local/python3108/lib/python3.10目录下所有的子目录都需要调整。
九、建议
建议使用root安装python3
或者
调用python3和pip3时,添加sudo
sudo /usr/local/python3108/bin/python3 -V
sudo /usr/local/python3108/bin/pip3 -V