Same python interpreter but json_set only not working on Visual Studio Code, works everywhere else

Viewed 77

I'm not being able to wrap my head around.

Exactly the same code, exactly the same interpreter, works everywhere else but not on Visual Studio Code.

Let's consider the following example:

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)

It should print out Bacon and Eggs but Visual Studio Code complains json_set is not a function.

How do I check what version of sqlite3 is being used by Visual Studio Code internally? Not sure why running it from vs code doesn't behave the same as running it from an external terminal.

Update 1

Linux Terminal

Works on system terminal

Visual Studio Code no such function: json_set

Update 2

I'm not relying on VS code to figure out which version of python to use. I'm typing it explicitly. Nothing to do with pip I suppose. I'm using the same version of pip anyways.

Vs code json_set function not found

Linux terminal works fine

1 Answers

Thanks Jill and Shawn.

Turns out Visual Studio Code is using a different version of sqlite3 under the hood than the system.

I just had to pip install pysqlite3-binary.

and

import pysqlite3 as sqlite3
...

everything else is unchanged.

I am pretty sure there are other ways to solve this problem but this is how I did it.

Related