【问题标题】:python vscode ImportError: No module named simplejsonpython vscode ImportError:没有名为simplejson的模块
【发布时间】:2020-11-05 04:59:29
【问题描述】:

我的代码中有 import simplejson,并使用 pip install simplejson using python3 安装 所以当我检查时,它完美地在我打开文件的终端中运行,但是当我尝试在 VScode 中构建时,它显示一个错误,例如, see the error in this link作为

`> 执行任务:python /Users/Tony/Documents/python3.9/helloworld.py

Traceback(最近一次调用最后一次): 文件“/Users/Tony/Documents/python3.9/helloworld.py”,第 1 行,在 将 simplejson 导入为 json ImportError:没有名为 simplejson 的模块 终端进程“/bin/bash '-c', 'python /Users/Tony/Documents/python3.9/helloworld.py'”启动失败(退出代码:1)。

但它仍然在我的终端上运行。如果我检查 pip 列表,simplejson 仍然存在,我也尝试使用不同版本的 python。

我已经激活了虚拟环境enter image description here 我正在使用我最近激活的最后一个。

请帮我找出错误。我真的不知道在哪里解决谢谢

【问题讨论】:

  • 您知道 simplejson 不仅仅是标准库中内置的 json 模块的外部维护版本吗?这是早期 Python 2 版本中的事情。但是现在你应该考虑使用 json 模块。
  • 感谢您提供的信息!我对 python 很陌生,只是按照我支付的教程中的步骤操作......所以.. :( 但是谢谢!

标签: python visual-studio-code import simplejson


【解决方案1】:

我已经重现了这个过程,希望以下步骤可以帮助你

1.激活venv并安装simplejson

2.在终端运行helloworld.py,确保脚本没有错误;

3.配置tasks.json,注意使用venv的pythonpath

"version": "2.0.0",
    "tasks": [
        {
            "label": "run python file",
            "type": "shell",
            "command": "/venv's pythonpath/ helloworld.py",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "problemMatcher": [
                "$tsc"
            ]
        }
    ]

4.Tasks:运行任务;

【讨论】:

  • 很高兴我的回答对您有所帮助,如果您能标记它,我们将不胜感激。
  • :) 我可以再问一个问题吗?如果我想用 python 文件访问整个文件夹怎么办?不仅像 helloworld.py 文件。我需要包含整个项目目录吗?
  • 在launch.json中添加"env": { "PYTHONPATH": "${workspaceFolder}", }, "cwd": "${workspaceFolder}",参数workspaceFolder表示在VS Code中打开的文件夹的路径,即整个当前项目文件夹。
猜你喜欢
  • 2011-02-05
  • 1970-01-01
  • 2013-01-06
  • 2015-12-09
  • 2012-01-26
  • 2020-12-15
  • 2015-11-17
  • 2016-07-24
  • 2017-07-15
相关资源
最近更新 更多