【问题标题】:Same python interpreter but json_set only not working on Visual Studio Code, works everywhere else相同的 python 解释器,但 json_set 仅不适用于 Visual Studio Code,适用于其他任何地方
【发布时间】:2021-06-07 02:22:15
【问题描述】:

我无法绕开我的头。

完全相同的代码,完全相同的解释器,在其他任何地方都可以工作,但不适用于Visual Studio Code

让我们考虑以下示例:

import sqlite3


_connection = sqlite3.connect(":memory:")
_connection.row_factory = sqlite3.Row

create_table = """
    create table food (
        id int not null,
        data json not null,
        primary key(id)
    )
"""

insert = """
    insert into food (id, data)
    values (1, '{"name": "Steak"}')
"""

update = """
    update food
    set data = json_set(data, '$.name', 'Bacon and Eggs')
    where id = 1
"""

fetch_query = "select json_extract(data, '$.name') as name from food where id = 1"

with _connection:
        _connection.execute(create_table)
        _connection.execute(insert)
        _connection.execute(update)
        food = _connection.execute(fetch_query)

new_food = dict(food.fetchone()).get('name')
print(new_food)

它应该打印出Bacon and EggsVisual Studio Code 抱怨json_set 不是一个函数。

如何检查Visual Studio Code 内部使用的sqlite3 版本?不知道为什么从 vs 代码运行它的行为与从外部终端运行它的行为不同。

更新 1

Linux 终端

Visual Studio 代码

更新 2

我不依赖 VS 代码来确定要使用哪个版本的 python。我正在明确输入它。我想与pip 无关。反正我用的是同一个版本的pip

VS 代码

Linux 终端

【问题讨论】:

  • 你如何判断“完全一样的翻译”?由于在VS Code中,其内部终端是从系统终端集成的,如果代码可以在系统终端运行,建议使用命令“pip --version”或“python --version”在VS Code的内部终端中检查它的python。
  • 您是否 100% 确定所有东西都使用相同的 python 解释器和安装程序?听起来不像。
  • @Shawn 请参考截图。
  • @JillCheng 如果我没记错的话,我使用的是位于/usr/bin/python3 的同一个解释器。请参考上面的截图。
  • 看来“pwd”命令显示的是可执行文件的路径,而你输入的是“/usr/bin/python3”?它不显示终端当前使用的 python 版本。请使用命令“python --version”(或“python -v”)检查两个终端是否使用相同的python。

标签: python sql json sqlite visual-studio-code


【解决方案1】:

感谢吉尔和肖恩。

原来Visual Studio Code 在后台使用的sqlite3 版本与系统不同。

我只需要pip install pysqlite3-binary

import pysqlite3 as sqlite3
...

其他一切都没有改变。

我很确定还有其他方法可以解决这个问题,但我就是这样做的。

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2020-11-09
    • 2014-08-06
    • 2012-05-01
    • 2014-01-25
    相关资源
    最近更新 更多