#๐ Organising project
92 messages ยท Page 1 of 1 (latest)
@amber sage
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.
The flaskr project is laid out like this:
So there's only Python files, the sql schema and templates in the flaskr package
mine looks okay then
(A Python package is a directory with an __init__.py in it, btw)
i moved schema into it
thats actually a good bit of info, thanks
And a module is a Python file
ahh so a package contains modules
what is a "library" then? a published package?
and also sub-packages (technically)
Not really a Python term in the same way
and so a library is from package import module
thats an import statement
No, "library" is not a Python term
okay
anyway
im still getting warnings
so i assume i need to change something
Verify that you can run flask first
Try flask --app darts_project run
Ah, you have to recreate the venv
whats the command for that again
rmdir /s darts_league_app\.venv
python -m venv darts_league_app\.venv
darts_league_app\.venv\Scripts\activate
pip install flask
this is gonna create a venv named darts_league_app
oh no a venv inside another dir called darts_league_app
right let me try running flask
Try 'flask run --help' for help.
Error: Could not import 'darts_project'.
How'd you run it?
this
What directory are you in?
darts_league_app
Can you push up your changes?
Ah
You named the subdirectory darts-project, but it has to be darts_project
With an underscore instead of a hyphen
Ok, so the problem is that when you run flask, you get the app instance as defined in __init__.py, but not the code that adds the routes in app.py. You can import the routes into the __init__.py file with
from . import app
but then you get a name clash, because you have the object "app", but also a module named "app" (i. e. the file app.py), so I recommend you rename the app.py file to routes.py (because it contains routes) and then import it like
from . import routes
``` in `__init__.py`
done
let me try that
Traceback (most recent call last):
File "C:\Users\georg\OneDrive\Documents\GitHub\darts_league_app\.venv\Lib\site-packages\flask\cli.py", line 245, in locate_app
__import__(module_name)
~~~~~~~~~~^^^^^^^^^^^^^
File "C:\Users\georg\OneDrive\Documents\GitHub\darts_league_app\darts_project\__init__.py", line 3, in <module>
from . import routes
File "C:\Users\georg\OneDrive\Documents\GitHub\darts_league_app\darts_project\routes.py", line 3, in <module>
from darts_project import app
ImportError: cannot import name 'app' from partially initialized module 'darts_project' (most likely due to a circular import) (C:\Users\georg\OneDrive\Documents\GitHub\darts_league_app\darts_project\__init__.py)
my bad actually
i don't think i put .
should i put from darts_project.routes import routes
from . import routesshould work
yeah im getting an error with it
what error?
Aaah
Now I get it
Can you show me what your routes.py file looks like?
# app.py
from flask import Flask, render_template, request, redirect, url_for, flash, session
from darts_project import app
from darts_project.db import get_db
# When flask recieves this request, it will call the index function
@app.route('/')
def index():
return render_template('index.html')
@app.route('/admin', methods=('GET', 'POST'))
def admin():
if '_flashes' in session:
session['_flashes'].clear()
if request.method == 'POST':
teamname = request.form['teamname']
error = None
if not teamname:
error = 'Team name is required.'
if error is not None:
flash(error)
else:
db = get_db()
db.execute('INSERT INTO teams (teamname) VALUES (?)', (teamname,))
db.commit()
return redirect(url_for('index'))
return render_template('admin.html')
thats what it looks like right now
and __init__.py?
# Makes app available for all modules in the package
from flask import Flask
from . import routes
app = Flask(__name__)
app.secret_key = 'eca17809-c94d-4963-a894-b8220b348f86'
Ah, you have to put the routes import at the end
after you create app
Should've made that clear
ohh okay
# Makes app available for all modules in the package
from flask import Flask
app = Flask(__name__)
from . import routes
app.secret_key = 'eca17809-c94d-4963-a894-b8220b348f86'
like that?
i'm so used to importing at the start haha
I think so...
let me try
I'd put it at the bottom, but it's probably fine.
woohoo
Alright, so do you still see the warning?
Ok, did vscode select the Python interpreter in the venv?
must be a mental thing ๐
must have
i'm pretty sure it has
You can see it here:
yeah 3.13.3 venv
Alright, that's good
You always wanna have the interpreter in the current venv selected
Otherwise you'll get missing import warnings and other weird behavior
This help channel has been closed. 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.