'Cannot import flask_wtf into Python 3.9 venv

No matter what I do, I cannot import flask_wtf, or any variation of it - flask-wtf, flask_WTF, flask-WTF, etc. My linter doesn't recognize it (pylance in VSCode) - gives me error:

Import "flask_wtf" could not be resolvedPylancereportMissingImports

When I try to run the code, I get:

    Traceback (most recent call last):
  File "c:\Users\btaylor\repots\brannen\digitalocean\flask_app\forms.py", line 1, in <module>
    from flask_wtf import Flaskform
ModuleNotFoundError: No module named 'flask_wtf'

OS is Windows 10. VSCode 1.66.1

Code:

from flask_wtf import Flaskform
from wtforms import (StringField, TextAreaField, IntegerField, BooleanField,
                     RadioField)
from wtforms.validators import InputRequired, Length

class CourseForm(FlaskForm):
    title = StringField('Title', validators=[InputRequired(),
                                             Length(min=10, max=100)])
    description = TextAreaField('Course Description',
                                validators=[InputRequired(),
                                            Length(max=200)])
    price = IntegerField('Price', validators=[InputRequired()])
    level = RadioField('Level',
                       choices=['Beginner', 'Intermediate', 'Advanced'],
                       validators=[InputRequired()])
    available = BooleanField('Available', default='checked')

I built a fresh directory, pip uninstalled Flask, created venv dir, activated, pip install the specific versions on this site: digital ocean - How To Use and Validate Web Forms with Flask-WTF: https://www.digitalocean.com/community/tutorials/how-to-use-and-validate-web-forms-with-flask-wtf and copied their code into the .py file.

My steps to install the libraries.

  • .\venv\Scripts\activate

  • pip install flask==2.0.2

  • pip install Flask-WTF==1.0.0

  • pip list

    Package Version


    click 8.1.2 colorama 0.4.4 Flask 2.0.2 Flask-WTF 1.0.0 itsdangerous 2.1.2 Jinja2 3.1.1 MarkupSafe 2.1.1 pip 22.0.4 setuptools 58.1.0 Werkzeug 2.1.1 WTForms 3.0.1

pip freeze shows:

(venv) PS C:\Users\btaylor\repots\brannen\digitalocean> pip freeze
click==8.1.2
colorama==0.4.4
Flask==2.0.2
Flask-WTF==1.0.0
itsdangerous==2.1.2
Jinja2==3.1.1
MarkupSafe==2.1.1
Werkzeug==2.1.1
WTForms==3.0.1

Snip of folder structure

Snip of folder structure



Solution 1:[1]

I figured it out. VSCode is running the Global Python. However, if I enter into the cli IDE in the folder, it runs the local installation of Python in the virtual environment.

I found the python executable in the venv\Scripts folder. In VSCode, entering (Windows) control-alt-P , select Python Interpreter, select my workspace that I'm in, "select at the workspace level", and then enter the path to the ..venv\Scripts\Python.exe .

Once VSCode is set to run the Python.exe from inside the virtual environment, then Python sees the additional modules installed in the venv.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pseudocyber