关于vscode 默认添加python项目的源目录路径到执行环境的问题 您所在的位置:网站首页 vscode项目文件夹位置 关于vscode 默认添加python项目的源目录路径到执行环境的问题

关于vscode 默认添加python项目的源目录路径到执行环境的问题

2022-09-28 07:47| 来源: 网络整理| 查看: 265

目录背景原因:解决方案:

背景

在vscode刚刚装好的时候,对于开发人员来说可能需要写一些模块的测试,而这个模块可能又引用了其他模块,

如果是同级目录的话可能会出现ModuleNotFoundError: No module named 错误

图文件结构和代码所示,ddd.py文件和ccc.py文件 分别在test1和test2目录下,ccc.py文件需要调用ddd.py文件的函数。

关于vscode默认添加python项目的源目录路径到执行环境的问题

原因:

在test2的ccc.py文件中执行print(sys.path) 查看路径

['g:\\go_code\\first_demo\\test2','D:\\Users\\Administrator\\AppData\\Local\\Programs\\python\\Python310\\python310.zip','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\DLLs','D:\\Users\\Administrator\\AppData\\Local\\Programs\\http://www.cppcns.comPython\\Python310\\lib','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packawww.cppcns.comges','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32\\lib','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\Pythonwin']

返回结果发现并无g:\\go_code\\first_demo\\ 的路径,所以test2下面的文件引用不到test1下面的文件属于正常

解决方案:

在setting.json文件中加入

"terminal.integrated.env.osx": { "PYTHONPATH": "${workspaceFolder}/", }, "terminal.inqHnTbILubtegrated.env.linux": { "PYTHONPATH": "${workqHnTbILubspaceFolder}/", }, "terminal.integrated.env.windows": { "PYTHONPATH": "${workspaceFolder}/", },

关于vscode默认添加python项目的源目录路径到执行环境的问题

然后重启vscode,再次test2的ccc.py文件中执行print(sys.path) 查看路径

['g:\\go_code\\first_demo\\test2','G:\\go_code\\first_demo','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\DLLs','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32','D:\\Users\\Administrator\\AppData\\Local\\Progra编程客栈ms\\Python\\Python310\\lib\\site-packages\\win32\\lib','D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\Pythonwin']

发现多了项目的源目录路径。

接着执行ccc.py模块发现可以正常运行了。

关于vscode默认添加python项目的源目录路径到执行环境的问题

参考文章:

https://www.qualityology.com/tech/marking-a-folder-as-sources-root-equivalent-in-visual-studio-code-for-python/

到此这篇关于vscode 默认添加python项目的源目录路径到执行环境的文章就介绍到这了,更多相关vscode python项目目录路径内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

关于vscode 默认添加python项目的源目录路径到执行环境的问题


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有