#π Flask Bcrypt error
210 messages Β· Page 1 of 1 (latest)
@orchid ice
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.
lets rule out drunk vscode
hit ctrl + shift + p and then type reload and hit enter on reload developer window or whatever its called
Man you are a wizard!π₯
Thanks!
I was considering pycharm but i will surely use it from now
Which one do u use btw?
vscode
lol
vscode is lightweight and works perfectly for my usecase
and the extensions are cherry on top
yup
and try running the code
yeah tell
show the error
does the program run tho
yes it does but not doing the encryption this
u r not being very clear.
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
So where might be the problem?
what is the error?
you have no error
what is the problem since you do not have an error?
yes i saw that, and you thought you had an error when you did not
it is showing i haven't installed it
what is showing you have not installed it?
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'
how do you get this error?
and how did you run this?
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
i am building an web app
and backend using flask
and in that i want to use bcrypt lib for password hashing
i get that, i use it myself.
And it isn't working the way it should
and im asking how you run your code
can you be please more clear
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?
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)
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.
yeag
alright..
so the question is.. how do you run your code
you said you used the terminal
flask --app market run
so how did you run it from the termianl
in the terminal
and did this work or not?
yes it did
and by terminal you mean windows terminal using powershell ?
but the bcrypt is not working
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
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
yes. i understand that part
for that i installed the flask bcrypt lib
what does this mean?
but when i am calling it in my market.py file
this means the the website is running but the hashing is not happening when i am creating an account
but it runs without error even if you use bcrypt?
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
okay, noted!
so lets start with the error part
whow do you get this error, how do you run the code
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
windows terminal
this terminal inside your editor is different from the windows terminal
alright..
i am using vs code
do you ever run code with vscode?
yes i did
do you get errors when you run code inside vscode?
what will you call this
yes i got errors before
and u are rght it does not seems an error
this is called a warning
it's an warning
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.
should i try reinstalling
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?
can you copy the exact text from the pylance warning?
i dont run python code in my editor, i run it via my terminal
Import "flask_bcrypt" could not be resolvedPylancereportMissingImports
try doing pip show flask_bcrypt and share the output
Name: Flask-Bcrypt
Version: 1.0.1
Summary: Brcrypt hashing for Flask.
Home-page: https://github.com/maxcountryman/flask-bcrypt
Author: Max Countryman
Author-email: [email protected]
License: BSD
Location: c:\tools\miniconda3\lib\site-packages
Requires: bcrypt, Flask
Required-by:
did you run this inside vscode or your windows terminal?
windows terminal
did
and the output?
-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
you have mutlple versions of python installed
you have python 3.12 and anaconda with 3.9 installed
so should i delete one of them?
so when you got the import error, it was because you used the other python version
not if you dont want to, you just have to make sure you run the correct version of python
you should make a virtual environment for every python project
yes you are right!
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
I used the anaconda 3.9 and the warning disappeared
that is because vscode now finds bcrypt installed in that version of python
through venv?
yes, venv comes with python
so how can i install it in version 3.12
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
yeah!
if you install python 3.11, as an example, you can run 3.11 using py -3.11
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
so i can carry on with py as well for preventing these error?
when you create a new venv, you need to install all packages you need again, since the venv will be empty
yes, that will solve the error you got from bcrypt
and it will be stored in my pc only right?
the bcrypt is still not working though!π₯²
you have to activate the virtual environment and install packages first
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
i m not using the virtual env right now
i switched my interprator to python 3.9
and the waarning disappeared
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
true
i have even disabled the option to install packages outside a venv
you can even see that i have activated a venv here
as long as you have an active venv and installed your packages, it will run yes
vscode and even use the venv if you want to, you have to switch to that venv interpreter
so van i still shift to venv or is a cumbersome task now?
i dont use vscode, so im fuzzy about the details
super easy, i shared the image above me doing it
this is me creating and using a venv
cool i am thinking uninstalling the 3.9 version and reinstall the package showing errors
remember that you have to install all packages you need in the new venv
flask, brypt and anything else you are using
will it work smoothly then?
yes it will, that is why it was invented
so how do i set it up?
from your project folder
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
okay
I have a meeting now, so i cant invest more time with you, you can always ask me later if you want more help
i learned more today that entire python class
im glad, come back later if you want to learn more!
my pleasure
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.