#πŸ”’ Flask Bcrypt error

210 messages Β· Page 1 of 1 (latest)

orchid ice
#

I have installed flask bcrypt library but when i am calling it it is showing that i haven't installed it, i installed it numerous times and the error is still there, what should i do?

edgy gobletBOT
#

@orchid ice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sinful juniper
#

lets rule out drunk vscode

#

hit ctrl + shift + p and then type reload and hit enter on reload developer window or whatever its called

orchid ice
#

Man you are a wizard!πŸ”₯

#

Thanks!

#

I was considering pycharm but i will surely use it from now

orchid ice
sinful juniper
#

vscode

#

lol

#

vscode is lightweight and works perfectly for my usecase

#

and the extensions are cherry on top

orchid ice
#

bro the error is still there

#

it took this much time to load lol

sinful juniper
#

it is?

#

can u open up terminal

orchid ice
#

yup

sinful juniper
#

and try running the code

orchid ice
#

yeah tell

sinful juniper
#

show the error

orchid ice
sinful juniper
#

does the program run tho

orchid ice
#

yes it does but not doing the encryption this

sinful juniper
#

show error

#

in terminal/cmd prompt

orchid ice
#

yup it does

#

there is no error in terminal

sinful juniper
#

u r not being very clear.

orchid ice
#

I ran the app on terminal

#

and the webapp is running perfectly

#

but the encryption which is the function of that library is not happening

orchid ice
half mortar
#

what is the error?

half mortar
#

what is the problem since you do not have an error?

orchid ice
#

i installed the bcrypt lib

#

for flask

#

and when i am calling it in my vs code

half mortar
#

yes i saw that, and you thought you had an error when you did not

orchid ice
#

it is showing i haven't installed it

half mortar
#

what is showing you have not installed it?

orchid ice
#

Exception has occurred: ModuleNotFoundError
No module named 'flask_bcrypt'
File "C:\Projects\flasktut\market.py", line 6, in <module>
from flask_bcrypt import Bcrypt
ModuleNotFoundError: No module named 'flask_bcrypt'

half mortar
orchid ice
#

from flask_bcrypt import Bcrypt

#

while calling the lib

half mortar
#

im asking about how you run the code, you only get errors in python when you run the code

#

so in one place you say you get an error and in another place you say it work. so you must be running it in two different ways

orchid ice
#

i am building an web app

#

and backend using flask

#

and in that i want to use bcrypt lib for password hashing

half mortar
#

i get that, i use it myself.

orchid ice
#

And it isn't working the way it should

half mortar
#

and im asking how you run your code

orchid ice
#

can you be please more clear

half mortar
#

hmm... what is unclear in my question?

#

you run your code, you get an error

#

and you run your code and you do not get an error

#

have i understood your issue correctly?

orchid ice
#

this is my code- '''from flask import Flask, render_template, redirect, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
import sqlalchemy
from wtforms import PasswordField, StringField, SubmitField, ValidationError
from flask_bcrypt import Bcrypt
app = Flask(name)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///market.db'
app.config['SECRET_KEY'] = 'db2dda154f9dc44d77fbcb52'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)

class User(db.Model):
id = db.Column(db.Integer(), primary_key=True)
username = db.Column(db.String(length=30), nullable=False, unique=True)
email_address = db.Column(db.String(length=50), nullable=False, unique=True)
password_hash = db.Column(db.String(length=60), nullable=False)
budget = db.Column(db.Integer(), nullable=False, default=1000)
items = db.relationship('Item', backref='owned_user', lazy=True)

@property
def password(self):
    return self.password

@password.setter
def password(self, plain_text_password):
    self.password_hash = bcrypt.generate_password_hash(plain_text_password).decode('utf-8')

class Item(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(length=30), nullable=False, unique=True)
price = db.Column(db.Integer(), nullable=False)
barcode = db.Column(db.String(length=12), nullable=False, unique=True)
description = db.Column(db.String(length=1024), nullable=False, unique=True)
owner = db.Column(db.Integer(), db.ForeignKey('user.id'))
def repr(self):
return f'Item {self.name}' '''

#

class RegisterForm(FlaskForm):
def validate_username(self, username_to_check):
user = User.query.filter_by(username=username_to_check.data).first()
if user:
raise ValidationError('Username already exists! Please try a different username.')

def validate_email_address(self, email_address_to_check):
    email_address = User.query.filter_by(email_address=email_address_to_check.data).first()
    if email_address:
        raise ValidationError('Email address already exists! Please try a different email address.')
        
username = StringField(label='User Name:')
email_address = StringField(label='Email Address:')
password1 = PasswordField(label='Password')
password2 = PasswordField(label='Confirm Password')
submit = SubmitField(label='Create Account')

@app.route('/')
@app.route('/home')
def home_page():
return render_template('home.html')

@app.route('/market')
def market_page():
items = Item.query.all()
return render_template('market.html', items=items)

@app.route('/register', methods=['GET', 'POST'])
def register_page():
form = RegisterForm()
if form.validate_on_submit():
user_to_create = User(username=form.username.data,
email_address=form.email_address.data,
password_hash=form.password1.data)
try:
db.session.add(user_to_create)
db.session.commit()
return redirect(url_for('market_page'))
except sqlalchemy.exc.IntegrityError:
flash('Email address already exists. Please choose a different email address.')
db.session.rollback()
if form.errors != {}:
for err_msg in form.errors.values():
flash(f'There was an error with creating a user: {err_msg}', category='danger')

return render_template('register.html', form=form)
edgy gobletBOT
#

Hey @orchid ice!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
half mortar
orchid ice
#

yeag

half mortar
#

alright..

#

so the question is.. how do you run your code

#

you said you used the terminal

orchid ice
#

flask --app market run

half mortar
#

so how did you run it from the termianl

orchid ice
#

in the terminal

half mortar
orchid ice
#

yes it did

half mortar
#

and by terminal you mean windows terminal using powershell ?

orchid ice
#

but the bcrypt is not working

half mortar
#

your answers are very unclear. i just want to understand what you are saying to me

#

you keep not answering the questions im asking and that makes it hard for me to find out your issue

orchid ice
#

i am really sorry if i m unclear

#

i a bit beginner and it's my first solo project

#

I want to use password hashing

half mortar
#

yes. i understand that part

orchid ice
#

for that i installed the flask bcrypt lib

half mortar
orchid ice
#

this means the the website is running but the hashing is not happening when i am creating an account

half mortar
orchid ice
#

i didn't used debug that time

#

but now the page is not opening

half mortar
#

so lets get something sorted out

#

something not working as expected is not the same as an error

#

bcrypt not hashing how you want it to is different from error

#

an error means you get an error in your terminal and python will crash

orchid ice
#

okay, noted!

half mortar
#

so lets start with the error part

half mortar
#

options to run code are, from the windows terminal, or from inside your editor

#

inside your editor you have a run button and youu also have its own terminal

orchid ice
#

windows terminal

half mortar
#

this terminal inside your editor is different from the windows terminal

half mortar
orchid ice
#

i am using vs code

half mortar
orchid ice
#

yes i did

half mortar
#

do you get errors when you run code inside vscode?

orchid ice
#

what will you call this

orchid ice
#

and u are rght it does not seems an error

half mortar
orchid ice
#

it's an warning

half mortar
#

it is your editor that tells you, hey im not sure i can find info about this thing

#

it is vscodes intellisense that is messed up and cant figure it out.

orchid ice
#

should i try reinstalling

half mortar
#

that has nothing to do with the code itself, it is a vscode thing

#

the fix for that is related to vscode, not python

#

do you get a pylance warning?

orchid ice
#

which editor do u use?

#

yes exactly that

half mortar
#

can you copy the exact text from the pylance warning?

half mortar
orchid ice
#

Import "flask_bcrypt" could not be resolvedPylancereportMissingImports

half mortar
#

try doing pip show flask_bcrypt and share the output

orchid ice
half mortar
#

did you run this inside vscode or your windows terminal?

orchid ice
#

windows terminal

half mortar
#

run these for me and share the output

#

pip -V

#

python -m pip -V

#

py -0

orchid ice
#

did

half mortar
#

and the output?

orchid ice
#

-V:3.12 * Python 3.12 (64-bit)
-V:ContinuumAnalytics/Anaconda39-64 Anaconda py39_4.12.0

#

for py -0

#

(base) PS C:\Projects\flasktut> pip -V
pip 21.2.4 from C:\tools\miniconda3\lib\site-packages\pip (python 3.9)
(base) PS C:\Projects\flasktut> python -m pip -V
pip 21.2.4 from C:\tools\miniconda3\lib\site-packages\pip (python 3.9)
(base) PS C:\Projects\flasktut> py -0
-V:3.12 * Python 3.12 (64-bit)
-V:ContinuumAnalytics/Anaconda39-64 Anaconda py39_4.12.0

half mortar
#

you have mutlple versions of python installed

#

you have python 3.12 and anaconda with 3.9 installed

orchid ice
#

so should i delete one of them?

half mortar
#

so when you got the import error, it was because you used the other python version

half mortar
#

you should make a virtual environment for every python project

half mortar
#

if you dont use anaconda, you can uninstall it so that you only python.org vesions of python installed. but that is up to you

orchid ice
#

I used the anaconda 3.9 and the warning disappeared

half mortar
#

that is because vscode now finds bcrypt installed in that version of python

half mortar
orchid ice
half mortar
#

py -m venv .venv from your project folder will make a new virtual environemnt using python 3.12

#

the reason for that is because py has a default python version it uses, the one with a * beside its name

orchid ice
#

yeah!

half mortar
#

if you install python 3.11, as an example, you can run 3.11 using py -3.11

orchid ice
#

so should i unstall 3.9 right?

half mortar
# orchid ice so should i unstall 3.9 right?

if you dont use anaconda there is no reason to have it, it is a great distrobution, but as you have seen, it kinda messes with you since it is bound to the python and pip name

#

on windows you should just use py

#

python can only work with one version of python at the time, while py works with all of them

orchid ice
#

so i can carry on with py as well for preventing these error?

half mortar
#

when you create a new venv, you need to install all packages you need again, since the venv will be empty

half mortar
orchid ice
#

and it will be stored in my pc only right?

half mortar
#

yes, it will be saved inside the .venv folder

#

together with your project files

orchid ice
#

the bcrypt is still not working though!πŸ₯²

half mortar
half mortar
#

the .venv\Scripts\activate is how you do it under windows terminal and powershell

#

alternatives are activate.bat and activate.ps1 for cmd and windows powershell respectivly

orchid ice
#

i m not using the virtual env right now

#

i switched my interprator to python 3.9

#

and the waarning disappeared

half mortar
#

yes, because you have installed the packages inside anaconda

#

you always have to install packages into a new python environment

#

thats why its best to just make a venv and install packages there right away

orchid ice
#

true

half mortar
#

i have even disabled the option to install packages outside a venv

orchid ice
#

will definetely do that in future ones

#

so how should i run it now?

half mortar
#

you can even see that i have activated a venv here

half mortar
#

vscode and even use the venv if you want to, you have to switch to that venv interpreter

orchid ice
#

so van i still shift to venv or is a cumbersome task now?

half mortar
#

i dont use vscode, so im fuzzy about the details

half mortar
half mortar
orchid ice
#

cool i am thinking uninstalling the 3.9 version and reinstall the package showing errors

half mortar
#

remember that you have to install all packages you need in the new venv

#

flask, brypt and anything else you are using

orchid ice
#

will it work smoothly then?

half mortar
#

yes it will, that is why it was invented

orchid ice
#

so how do i set it up?

half mortar
#

py -m venv .venv

#

.venv\Scripts\activate

#

you only create the venv once, and you activate it with the second command

#

deactivate when you are done, or close the terminal window

orchid ice
#

okay

half mortar
#

I have a meeting now, so i cant invest more time with you, you can always ask me later if you want more help

orchid ice
#

Bro it worked

#

it is hashed

#

thanks a lot man

half mortar
#

excellent

#

you see how imporatnt it is to have a venv

#

worked instantly

orchid ice
#

i learned more today that entire python class

half mortar
#

im glad, come back later if you want to learn more!

orchid ice
#

about the flask and stuff

#

thanks again man!

half mortar
#

my pleasure

orchid ice
#

see ya!

edgy gobletBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.