VSCode Python调试器异常路径导入模块报错求助
VSCode调试Scrapy项目时出现FileNotFoundError:路径指向~/.local/lib而非指定conda环境
问题情况
调试Scrapy项目时触发如下报错:
Exception has occurred: FileNotFoundError [Errno 2] No such file or directory: '/home/dmitriy_mikhailov/.local/lib/python3.10' File "/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/lib/python3.10/posixpath.py", line 431, in _joinrealpath st = os.lstat(newpath) File "/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/lib/python3.10/posixpath.py", line 396, in realpath path, ok = _joinrealpath(filename[:0], filename, strict, {}) File "/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, FileNotFoundError: [Errno 2] No such file or directory: '/home/dmitriy_mikhailov/.local/lib/python3.10'
已在VSCode中指定conda环境inst_crawl(路径/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/lib),但调试时系统尝试从~/.local/lib/python3.10加载模块,与指定环境不符。
当前launch.json配置:
{ "version": "0.2.0", "configurations": [ { "name": "Debug scrapy project", "type": "python", "request": "launch", "console": "integratedTerminal", "justMyCode": false, "cwd": "${workspaceFolder}", "program": "/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/bin/scrapy", "args": [ "crawl", "instagram_common_api", "-a", "get_user_profile=True", "-a", "get_user_stories=True", "--logfile", "inst.log", "--output", "inst.out.json" ] } ] }
调试时发现runpy.py第86行的code变量指向VSCode扩展自带的debugpy:
print(code.co_filename) -> '/home/dmitriy_mikhailov/.vscode-server/extensions/ms-python.python-2023.18.0/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py'
环境背景:使用conda管理虚拟环境,环境内用Poetry管理依赖,已尝试在Python扩展设置中指定conda和Poetry路径,但问题未解决。
可能原因
- debugpy与conda环境路径冲突:VSCode Python扩展自带的debugpy未正确适配conda环境,加载时错误读取
~/.local/lib路径 - Scrapy可执行文件shebang配置错误:
inst_crawl环境内的scrapy脚本首行shebang指向系统Python而非环境内的解释器 - 终端环境变量残留:启动VSCode时未激活conda环境,终端
PYTHONPATH等变量指向~/.local/lib,干扰调试路径 - Poetry与conda环境未完全绑定:Poetry默认使用系统Python,导致依赖加载路径未完全落在conda环境内
解决思路
1. 在launch.json中明确指定conda环境的Python解释器
添加python字段,强制使用conda环境内的Python运行debugpy,避免依赖扩展自带的debugpy:
{ "version": "0.2.0", "configurations": [ { "name": "Debug scrapy project", "type": "python", "request": "launch", "console": "integratedTerminal", "justMyCode": false, "cwd": "${workspaceFolder}", "python": "/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/bin/python", "program": "/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/bin/scrapy", "args": [ "crawl", "instagram_common_api", "-a", "get_user_profile=True", "-a", "get_user_stories=True", "--logfile", "inst.log", "--output", "inst.out.json" ] } ] }
2. 检查并修正Scrapy脚本的shebang
查看/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/bin/scrapy首行,确认是否为:
#!/home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/bin/python
若不符,执行以下步骤修复:
- 激活conda环境:
conda activate inst_crawl - 重新安装Scrapy:
pip install --force-reinstall scrapy
3. 清理环境变量并激活conda后启动VSCode
- 终端中激活
inst_crawl环境:conda activate inst_crawl - 从该终端启动VSCode:
code . - 也可在launch.json中添加
env字段重置PYTHONPATH:
"env": { "PYTHONPATH": "${workspaceFolder}", "CONDA_DEFAULT_ENV": "inst_crawl" }
4. 绑定Poetry到conda环境的Python解释器
激活inst_crawl环境后,执行命令让Poetry使用环境内的Python:
poetry env use /home/dmitriy_mikhailov/anaconda3/envs/inst_crawl/bin/python poetry install
5. 重置VSCode Python扩展设置
- 打开VSCode设置,搜索
Python: Conda Path和Python: Poetry Path,删除自定义路径,让扩展自动检测 - 左下角状态栏重新选择
inst_crawl环境,重启VSCode后重试调试
6. 调整VSCode Python扩展版本
当前使用的ms-python.python-2023.18.0版本可能存在兼容性问题:
- 在扩展面板找到Python扩展,点击
管理→安装另一个版本 - 尝试切换到
2023.16.0或最新稳定版本,重启VSCode后测试
内容的提问来源于stack exchange,提问作者Dmitriy Mikhailov
相关产品推荐
相关产品推荐

