vscode中关于python的一些常用配置

文章目录

python cv2 提示配置

第一步 配置提示信息

打开settings

打开扩展--> 找到python --> 打开json配置

将cv2的安装目录配置到如下位置

第二部 重启vs 可能还不行,那就重新安装以下opencv-python

具体为啥不行没搞明白

配置pytest

还是如上,将下入的位置打开

编写测试用例

当方法以test开头的时候会自动识别成测试用例,会有一个标识,鼠标右键点击标识的时候可以执行测试,如果成功就会出现一个对号

python 复制代码
import pytest
from src import base_function as bf

print(bf.getCurrentAsbDir())

def test_getCurrentAsbDir():
    assert bf.getCurrentAsbDir() == "e:\\learning_sources\\cv_py"

def aaa():
    assert 1==1

def test_aaa():
    assert 1==1

配置跨文件import

在工作目录中新建一个.env文件输入内容如下

复制代码
PYTHONPATH="E:/learning_sources/cv_py/src;E:/learning_sources/cv_py/src/tests;E:/learning_sources/cv_py;${env:PYTHONPATH}"

打开.vscode中的setting.json 进行配置 内容如下

json 复制代码
{
    "python.analysis.autoImportCompletions": true,
    // "python.envFile": "E:/learning_sources/cv_py/.env",
    //这里是配置运行时的工作路径
    "terminal.integrated.env.windows": {"PYTHONPATH":"${workspaceFolder};${env:PYTHONPATH}"},
    "python.defaultInterpreterPath": "D:\\ProgramData\\anaconda3\\python.exe" 
}

参考:

https://blog.csdn.net/qq_31654025/article/details/109474175

类似如下的文件结构,就可以在test目录(包)中引入src的内容了

test_base_function.py 内容如下

python 复制代码
import pytest
from src import base_function as bf

print(bf.getCurrentAsbDir())

配置格式化工具

安装插件 Black Formatter 插件

修改setting.json配置内容如下

json 复制代码
{
    "python.formatting.provider": "none",
    "python.formatting.autopep8Args": [
        "--max-line-length=150"
    ],
    "editor.wordWrap": "on",
    "editor.wordWrapColumn": 150,
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true,
        "editor.formatOnPaste": true,
        "editor.formatOnType": true,
    },
    "black-formatter.args": [
        "--line-length",
        "150"
    ],
}
相关推荐
charlee4414 分钟前
《GIS基础原理与技术实践》配套案例(Python版)
python·conda·numpy·gis·环境配置
枫叶林FYL15 分钟前
项目十:事件溯源仓储管理系统(WMS)仿真实现
开发语言·python
渣渣xiong3 小时前
从零开始:前端转型AI agent直到就业第五十七天-第五十八天
前端·人工智能·python
小L~~~3 小时前
基于贪心策略的混合遗传算法求解01背包问题
python·算法
才兄说3 小时前
机器人二次开发机器人动作定制?动作迁移数据优化
python
用户8356290780514 小时前
用 Python 实现 Excel 散点图绘制与定制
后端·python
PAK向日葵4 小时前
从零实现 Python 虚拟机(一):PVM 基本原理介绍
python
神所夸赞的夏天4 小时前
创建虚拟环境提示SSLError错误
python
极光代码工作室4 小时前
基于机器学习的二手商品价格预测系统
人工智能·python·深度学习·机器学习
无情的西瓜皮4 小时前
MCP协议实战:从零搭建一个AI Agent工具服务器
运维·服务器·python