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

作者:包子铺1234 时间:2022-01-29 06:00:34 

背景

在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\\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 默认添加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\\Programs\\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/

来源:https://www.cnblogs.com/qinfangzhe/p/15917263.html

标签:vscode,python,目录,路径
0
投稿

猜你喜欢

  • Python远程控制Windows服务器的方法详解

    2023-07-13 13:14:03
  • 使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)

    2023-07-01 06:32:45
  • python处理文本文件实现生成指定格式文件的方法

    2022-01-07 21:41:43
  • 你的网页“面目全非”过吗?

    2010-07-02 16:24:00
  • Python常用正则表达式符号浅析

    2022-03-25 08:08:36
  • DW实现鼠标滑过切换图片

    2008-02-03 18:49:00
  • 解决numpy数组互换两行及赋值的问题

    2023-07-26 16:51:24
  • 十“问”DreamWeaver

    2007-02-03 11:39:00
  • python基于itchat模块实现微信防撤回

    2022-03-15 01:41:42
  • 用asp给网站添加rss聚合功能

    2007-11-05 19:08:00
  • css学习笔记:DIV水平垂直居中

    2009-06-19 12:45:00
  • Pyspark读取parquet数据过程解析

    2022-01-21 13:33:38
  • asp检测文件编码方法

    2007-10-03 14:27:00
  • 关于IDEA git 只有Commit没有Push的问题

    2023-07-20 02:26:58
  • python正则实现提取电话功能

    2022-04-30 03:32:46
  • 使用Python3编写抓取网页和只抓网页图片的脚本

    2023-08-15 08:00:17
  • Mac下python包管理工具pip的安装

    2023-11-19 11:09:46
  • pytorch 自定义卷积核进行卷积操作方式

    2021-07-24 22:54:39
  • Python中的bytes类型用法及实例分享

    2022-12-19 01:50:42
  • python DataFrame 修改列的顺序实例

    2023-07-21 12:55:40
  • asp之家 网络编程 m.aspxhome.com