广告
返回顶部
首页 > 资讯 > 后端开发 > Python >关于vscode 默认添加python项目的源目录路径到执行环境的问题
  • 697
分享到

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

2024-04-02 19:04:59 697人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录背景原因:解决方案:背景 在vscode刚刚装好的时候,对于开发人员来说可能需要写一些模块的测试,而这个模块可能又引用了其他模块, 如果是同级目录的话可能会出现ModuleNot

背景

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

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

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

原因:

在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\\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\\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.integrated.env.linux": {
    "PYTHONPATH": "${workspaceFolder}/",
},
"terminal.integrated.env.windows": {
    "PYTHONPATH": "${workspaceFolder}/",
},

然后重启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\\Programs\\Python\\Python310\\lib\\site-packages\\win32\\lib',
'D:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\Pythonwin']

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

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

参考文章:

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

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

--结束END--

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

本文链接: https://www.lsjlt.com/news/139825.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作