最新系统centos安装最新版python 3.8.5以及pip换源、临时指定源,pytest安装后的使用以及出现pytest命令not found无法识别

tech2022-08-19  73

1、如果是从网站上单独下载的centos镜像,大多数一般都带有python2或者python3,如果是使用的docker中的centos镜像,那是不带任何东西的,比较纯净。通过输入 python 查看是否有安装。

2、安装依赖包

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源码时,需要一些依赖包,一次安装完毕

3、安装wget yum install wget 这个包是为了下载python源码用的。

4、下载源码包 wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz

我是下载的最新的python3.8,如果想安装其他版本,去 python官网下载 页面下载对应的版本即可。

但是这个下载链接比较慢,我是用迅雷下载到本地之后,再scp到服务器的。

5、解压安装

# 解压压缩包 tar -zxvf Python-3.8.1.tgz # 进入文件夹 cd Python-3.8.1 # 配置安装位置 ./configure prefix=/usr/local/python3 # 也可以通过 --enable-optimizations 指定使用优化选项 ./configure --enable-optimizations --prefix=/usr/local/python3.8 --with-http_ssl_module --with-ssl # 安装 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 表示添加信任

6、添加软连接

#添加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

7、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

8、pytest

安装 pip install -U pytest 或者 pip install pytest

指定测试用例

nodeid由模块文件名、分隔符、类名、方法名、参数构成,举例如下:

运行模块中的指定用例 pytest test_mod.py::test_func 运行模块中的指定方法 pytest test_mod.py::TestClass::test_method

有时候,python 3.x 中需要如下使用:

运行模块中的指定用例 python -m pytest test_mod.py::test_func 运行模块中的指定方法 python -m pytest test_mod.py::TestClass::test_method

Pytest测试报告 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告。兼容Python 2.x, 3.x 。

安装方式:pip install pytest-html pip install pytest-html 通过命令行方式,生成xml/html格式的测试报告,存储于用户指定路径。插件名称:pytest-html 使用方法: 命令行格式:pytest --html=用户路径/report.html 示例: import pytest class Test_ABC: def setup_class(self): print("------->setup_class") def teardown_class(self): print("------->teardown_class") def test_a(self): print("------->test_a") assert 1 def test_b(self): print("------->test_b") assert 0 # 断言失败

运行方式: 1.修改Test_App/pytest.ini文件,添加报告参数,即:addopts = -s --html=./report.html # -s:输出程序运行信息 # --html=./report.html 在当前目录下生成report.html文件 ️ 若要生成xml文件,可将–html=./report.html 改成 --html=./report.xml 2.命令行进入Test_App目录 3.执行命令: pytest 执行结果: 1.在当前目录会生成assets文件夹和report.html文件


更多有趣的实用技术,请查看对应的专栏。

官网首页:http://www.52014991.xyz/home.html

福利实惠:http://www.52014991.xyz/contact.html

功能jar包:http://www.52014991.xyz/products.html

博客主页:https://me.csdn.net/u014374009

代码主页:https://github.com/YouAreOnlyOne?tab=repositories

团队成员:http://www.52014991.xyz/team.html

更多干货资源请关注作者的专栏。

留言点赞关注,我们一起分享AI学习与发展的干货。


正经的技术,不正经的程序员~

最新回复(0)