一、安裝依賴包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
yum install gcc gcc-c++ openssl-devel libffi-devel tk-devel
編譯python源碼時,需要一些依賴包,一次安裝完畢
二、安裝wget
yum install wget
這個包是為了下載python源碼用的。
三、下載源碼包
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz
我是下載的python3.8,如果想安裝其他版本,去 python官網下載 頁面下載對應的版本即可。
四、解壓安裝
# 解壓壓縮包
tar -zxvf Python-3.8.1.tgz
# 進入文件夾
cd Python-3.8.1
# 配置安裝位置
./configure prefix=/usr/local/python3
# 安裝
make && make install
如果最后沒提示出錯,就代表正確安裝了,在/usr/local/目錄下就會有python3目錄。
五、Python更換pip源
在使用Python時,需要使用各種各樣的庫,通常會使用pip直接安裝,這樣最為簡單也最方便。但最為崩潰的地方在於有時候速度出奇的慢,因為pip默認使用的源為官方源,而官方源在國外。通常的解決方法是更換源,常見的國內源如下所示:
https://pypi.tuna.tsinghua.edu.cn/simple/ 清華
http://pypi.doubanio.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
https://pypi.mirrors.ustc.edu.cn/simple/ 中國科學技術大學
http://mirrors.163.com/pypi/simple/ 網易
Windows下永久更換源
1.在運行窗口或資源管理器中輸入%APPDATA%
2.進入目錄后,新建一個文件夾pip,並在該文件夾里面新建文件pip.ini,並輸入以下內容:
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
3.下次再使用pip安裝的時候,就可以更換為國內的pip源。
Linux下永久更換源
1、在家目錄中創建.pip目錄
mkdir -p ~/.pip
2、創建pip.conf文件
vim ~/.pip/pip.conf
3、輸入以下內容並保存
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
臨時更改pip源
pip install <包名> -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
-i https://pypi.tuna.tsinghua.edu.cn/simple 表示使用清華源
--trusted-host pypi.tuna.tsinghua.edu.cn 表示添加信任
六、添加軟連接
#添加python3的軟鏈接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
#添加 pip3 的軟鏈接
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
或者
#添加python3的軟鏈接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python
#添加 pip3 的軟鏈接
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip
添加軟連接:
軟連接,為某一個文件在另外一個位置建立一個同步的連接
在此處創建軟連接后,訪問到此處的軟連接,就會定位到軟連接指向的位置。相當於把一個文件夾放到了多個位置,但其實還是只有一份,並不是復制。
具體用法是:ln -s 源文件 目標文件
會針對源文件創建一個軟連接(目標文件),鏈接到源文件。
別搞反了。前面的源文件,是需要在當前位置能訪問到的文件。后面的是目標文件,是新創建出來的鏈接。
例如 ln -s /home/source_file source_file_link
此時在當前窗口 cd source_file_link,就會到達 /home/source_file
刪除軟連接
刪除時,右邊不能加 斜杠
例如上面的那個是 rm -rf source_file_link 或 rm source_file_link
七、openssl
## openssl 版本 要求 OpenSSL 1.0.2+,如果不滿足需升級 openssl
# 然而 yum install openssl openssl-devel 的方式也無法升級到1.0.2版本,那也只能手動編譯並安裝
# 下載 openssl-1.1.1b.tar.gz 新版本的源碼包
wget -c https://www.openssl.org/source/openssl-1.1.1b.tar.gz
tar -zxvf openssl-1.1.1b.tar.gz
cd openssl-1.1.1b
./config --prefix=/usr/local/openssl # 指定安裝目錄為/usr/local/openssl
make && make install #(耗時比較長,需要耐性等待)
# 編譯完成后需要拷貝庫文件
cp /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
cp /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
# 替換軟鏈接
rm -rf /usr/bin/openssl
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
到這就完事了!
八、安裝pytest
安裝
pip install -U pytest 或者 pip install pytest
Pytest測試報告
pytest-HTML是一個插件,pytest用於生成測試結果的HTML報告。兼容Python 2.x, 3.x 。
pip install pytest-html