
今天裝了CentOS服務(wù)器,為其裝python3.6的環(huán)境,具體步驟如下:
自已已經(jīng)提前安裝python2.7,
注:centos 安裝基礎(chǔ)
轉(zhuǎn)移安裝包:
生成requirements.txt文件
pip freeze > requirements.txt
安裝requirements.txt依賴
pip install -r requirements.txt
1. 安裝可能需要的依賴包:
- yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel wget gcc make –y
2. 下載安裝包
解壓
- tar zxvf Python-3.6.5.tgz
創(chuàng)建python3.6文件夾
- mkdir /usr/local/python3.6
進(jìn)入解壓后的文件夾
- cd Python-3.6.5
python3虛擬環(huán)境中解決 ModuleNotFoundError: No module named '_ssl'
3. 指定安裝路徑,編譯安裝
- ./configure --prefix=/usr/local/python3.6
- make
- make install
4. 添加環(huán)境變量
第四步,添加也不可以,失敗,直接跳到二標(biāo)題。
- vim /etc/profile
添加以下
export PATH=$PATH:/usr/local/python3.6/bin
5. 重載環(huán)境變量
上面的第四步可以使用一下命令替換:
export PATH=/usr/local/python3.6/bin:$PATH
$ source /etc/profile
好了,通過以上的步驟就可以實(shí)現(xiàn)python3.6的安裝了,現(xiàn)在在界面輸入python3 就可以打開python3.6.0 了,直接輸入python會(huì)是系統(tǒng)自帶的 python2.7。
6. 修改pip3
python3 -m pip install --upgrade pip --force-reinstall,顯示重新安裝成功。
二、重啟連接服務(wù)器使用python3失敗
1. 建立python3的軟鏈
ln -s /usr/local/python3.6/bin/python3 /usr/bin/python3
2. 并將/usr/local/python3.6/bin加入PATH
vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3.6/bin
export PATH
按ESC,輸入:wq回車退出。
修改完記得執(zhí)行行下面的命令,讓上一步的修改生效:
- source ~/.bash_profile
檢查Python3及pip3是否正??捎茫?/p>
# python3 -V
Python 3.6.1
# pip3 -V
pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)
3. 不行的話嘗試創(chuàng)建一下pip3的軟鏈接
- ln -s /usr/local/python3.6/bin/pip3 /usr/bin/pip3
三、更換pip 安裝源
pip國(guó)內(nèi)的一些鏡像
??阿里云 https://mirrors.aliyun.com/pypi/simple/
??中國(guó)科技大學(xué) https://pypi.mirrors.ustc.edu.cn/simple/
??豆瓣(douban) http://pypi.douban.com/simple/
??清華大學(xué) https://pypi.tuna.tsinghua.edu.cn/simple/
??中國(guó)科學(xué)技術(shù)大學(xué) http://pypi.mirrors.ustc.edu.cn/simple/
修改源方法:
臨時(shí)使用:
可以在使用pip的時(shí)候在后面加上-i參數(shù),指定pip源
pip install opencv-python -i https://mirrors.aliyun.com/pypi/simple
Linux:
永久使用可以這樣
修改 ~/.pip/pip.conf 文件,如下
# 阿里云
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
# 中國(guó)科學(xué)技術(shù)大學(xué)
[global]
index-url=http://pypi.mirrors.ustc.edu.cn/simple/
[install]
trusted-host=pypi.mirrors.ustc.edu.cn
# 清華大學(xué)
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
[install]
trusted-host=pypi.tuna.tsinghua.edu.cn
出現(xiàn)如下報(bào)錯(cuò):
E212: Can't open file for writing
問題解決:
因?yàn)椴淮嬖?pip這個(gè)文件夾所以會(huì)存在這個(gè)問題,新建文件夾~/.pip就可以解決了.
mkdir ~/.pip
vim ~/.pip/pip.conf
Windows:
接在user目錄中創(chuàng)建一個(gè)pip目錄,如:C:\Users\xx\pip,新建文件pip.ini,內(nèi)容如下:
[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/
centos7 安裝python3.8
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
yum install libffi-devel -y
# 1.下載安裝包
wget https://www.python.org/ftp/python/3.8.13/Python-3.8.13.tgz
# 解壓
tar zxvf Python-3.8.13.tgz
# 2.創(chuàng)建python3.8文件夾
mkdir /usr/local/python3.8
# 3.指定安裝路徑,編譯安裝
cd Python-3.8.13
./configure --prefix=/usr/local/python3.8
make
make install
# 4.環(huán)境配置
vim /etc/profile
export PATH=$PATH:/usr/local/python3.8/bin
source /etc/profile
# 5.建立python3的軟鏈
ln -s /usr/local/python3.8/bin/python3 /usr/bin/python3
python3 -m pip install --upgrade pip --force-reinstall
cd /usr/bin
mv pip pip.bak
ln -s /usr/local/python3.8/bin/pip3 /usr/bin/pip
# 6.環(huán)境配置
vim ~/.bash_profile
export PATH=$PATH:$HOME/bin:/usr/local/python3.8/bin
source ~/.bash_profile
# 7.查看
[root@cdh03 ~]# cd /usr/bin/
[root@cdh03 bin]# ls -alh|grep python
lrwxrwxrwx 1 root root 29 Apr 29 15:38 pip3 -> /usr/local/python3.8/bin/pip3
lrwxrwxrwx 1 root root 7 Apr 25 08:52 python -> python2
lrwxrwxrwx 1 root root 9 Apr 25 08:52 python2 -> python2.7
-rwxr-xr-x 1 root root 7.0K Nov 17 2020 python2.7
lrwxrwxrwx 1 root root 32 Apr 29 15:35 python3 -> /usr/local/python3.8/bin/python3
問題:****報(bào)錯(cuò)-ModuleNotFoundError: No module named '_lzma'
最好保留系統(tǒng)原轉(zhuǎn)的Python,否則出很多問題。
解決方法:
第一步:
# centos系統(tǒng)執(zhí)行
yum install xz-devel -y
yum install python-backports-lzma -y
pip install backports.lzma
# ubuntu系統(tǒng)執(zhí)行
apt-get install liblzma-dev -y
pip install backports.lzma
第二步:
vim /usr/local/python3.8/lib/python3.8/lzma.py
第三步:
修改之前廢棄的代碼
try:
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
except ImportError:
from backports.lzma import *
from backports.lzma import _encode_filter_properties, _decode_filter_properties
重啟即可