Home > OS >  Python interpreter issue:
Python interpreter issue:

Time:06-11

I am following along with a tutorial for a "notes" app, I cloned the creator's repository which should work perfectly fine, but I keep running into problems because of the python interpreter in vscode. When I use Python 3.8.2 I get a Unable to import 'flask' error, and when I use Python 3.9.6 I get Instance of 'scoped_session' has no 'add' member errors for the lines where I use db.session.add and db.session.delete. I am really lost on how to fix this.

This is the views.py file below where I get these errors.

from flask import Blueprint, render_template, request, flash, jsonify
from flask_login import login_required, current_user
from .models import Note
from . import db
import json

views = Blueprint('views', __name__)


@views.route('/', methods=['GET', 'POST'])
@login_required
def home():
    if request.method == 'POST':
        note = request.form.get('note')

        if len(note) < 1:
            flash('Note is too short!', category='error')
        else:
            new_note = Note(data=note, user_id=current_user.id)
            db.session.add(new_note)
            db.session.commit()
            flash('Note added!', category='success')

    return render_template("home.html", user=current_user)


@views.route('/delete-note', methods=['POST'])
def delete_note():
    note = json.loads(request.data)
    noteId = note['noteId']
    note = Note.query.get(noteId)
    if note:
        if note.user_id == current_user.id:
            db.session.delete(note)
            db.session.commit()

    return jsonify({})

CodePudding user response:

Maybe you've created python environment and installed flask package within this interpreter. The solutions is to change the default python interpreter inside VSCode.

Click CTRL SHIFT P Then write - >Python: Select Interpreter,

It should auto complete immediately (The '>' is automatically appeneded).

After that you just select a path, i.e.

/path/to/project/.env/bin/python

or

/path/to/project/.env/src/python

I believe that should solve the problem :) .

  • Related