Ubuntu系统下安装Python3

一、更改数据源:

        备份安装源: cp /etc/apt/sources.list   /etc/apt/sources.list.backup

         修改数据:vim /etc/apt/sources.list  

         如下方信息,也可以使用阿里云下载速度也快

deb http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal main multiverse restricted universe
deb http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal-backports main multiverse restricted universe
deb http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal-proposed main multiverse restricted universe
deb http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal-security main multiverse restricted universe
deb http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal-updates main multiverse restricted universe
deb-src http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal main multiverse restricted universe
deb-src http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal-backports main multiverse restricted universe
deb-src http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal-proposed main multiverse restricted universe
deb-src http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal-security main multiverse restricted universe
deb-src http://cmc-cd-mirror.rnd.huawei.com/ubuntu/ focal-updates main multiverse restricted universe

        执行更新信息(如果需要root身份,命令为:sudo apt update):apt update

 

二、安装Python3

        注:好了现在正式进入核心部分

        1. 下载python安装包: https://www.python.org/downloads/release/python-371/

                Ubuntu系统下安装Python3_第1张图片

        

         2. 开始编译安装

                注:3.7中,ctypes模块依赖包 libffi-dev .如果没有,安装会报错:ModuleNotFoundError: No module named '_ctypes'

                解决方案:apt-get install libffi-dev

tar -zxvf Python-3.7.1.tgz
cd Python-3.7.1
./configure --prefix=/usr/local/
make
make install

                

        3. 建立刚安装python3的软连接

                ln -s /usr/local/bin/python3.7 /usr/bin/python3

         4. pip3的检测和安装

                a、查看/usr/local/bin路径有没有pip3 ,在此路径下直接执行pip3看 能不能直接运行,如果可以就直接建立软连接:ln -s /usr/local/bin/pip3 /usr/bin/pip3

                b、如果无法使用,我们需要手动安装:

                        注:安装pip之前,需要安装setuptools,网下载源码:

                                https://pypi.org/project/setuptools/#files

tar -zxvf setuptools-56.2.0.tar.gz

cd setuptools-40.6.2
python3 setup.py build
python3 setup.py install

                c、安装pip:pip官网下载源码:https://pypi.org/project/pip/#files

                     

sudo tar -zxvf pip-10.1.tar.gz
cd pip-10.1
python3 setup.py build
python3 setup.py install


最后建立软连接:ln -sf /usr/local/bin/pip3 /usr/bin/pip3

   

三、安装过程经典问题总结

        1.  ModuleNotFoundError: No module named ‘_bz2‘问题解决

                注:出现这个错误的原因是我用的是python3.7,但是bz2这个库是安装到了python3.6里面,所以找不到。为了解决这个问题,需要将python3.6里面的bz库拷贝到python3.7下面。

                a、找到python3.6路径下的bz库文件,即“_bz2.cpython-36m-x86_64-linux-gnu.so”

                        查看命令:ls /usr/lib/python3.6/lib-dynload/

                        

                b、切换到python3.7对应路径,将该文件复制到该目录下:

cd /usr/local/lib/python3.7/lib-dynload
cp /usr/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so ./

                c、修改文件名称,将"-36m"修改为"-37m"即可

mv _bz2.cpython-36m-x86_64-linux-gnu.so _bz2.cpython-37m-x86_64-linux-gnu.so

                d、如果这些步骤执行完后还不行需要重新编译python源码:

cd Python-3.7.1
./configure --prefix=/usr/local/
make
make install

   

        2.   E: Sub-process /usr/bin/dpkg returned an error code (1) 错误                             

cd /var/lib/dpkg
mv info info.bak
mkdir info

        3. pip3安装时出现"return Command 'lsb_release -a' returned non-zero exit status 1"

mv /usr/bin/lsb_release /usr/bin/lsb_release.bak

        4. xgboost 安装,error: [Errno 2] No such file or directory: cmake’: ‘cmake错误

apt install cmake

    

 

 

 

 

 

 

 

你可能感兴趣的:(服务器配置系列,python)