#๐Ÿ”’ Organising project

92 messages ยท Page 1 of 1 (latest)

slim scarabBOT
#

@amber sage

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.

amber sage
desert anvil
#

The flaskr project is laid out like this:

#

So there's only Python files, the sql schema and templates in the flaskr package

amber sage
#

mine looks okay then

desert anvil
#

(A Python package is a directory with an __init__.py in it, btw)

amber sage
#

i moved schema into it

amber sage
desert anvil
#

And a module is a Python file

amber sage
#

ahh so a package contains modules

fallow arrow
fallow arrow
desert anvil
amber sage
#

and so a library is from package import module

fallow arrow
#

thats an import statement

desert anvil
amber sage
#

anyway

#

im still getting warnings

#

so i assume i need to change something

desert anvil
#

Verify that you can run flask first

amber sage
#

what command do i need to use now?

desert anvil
#

Try flask --app darts_project run

amber sage
desert anvil
#

Ah, you have to recreate the venv

amber sage
#

whats the command for that again

desert anvil
#
rmdir /s darts_league_app\.venv
python -m venv darts_league_app\.venv
darts_league_app\.venv\Scripts\activate
pip install flask
amber sage
#

oh no a venv inside another dir called darts_league_app

desert anvil
#

Yeah, you want the venv in your project directory

#

Which is called darts_league_app

amber sage
#

right let me try running flask

#
Try 'flask run --help' for help.

Error: Could not import 'darts_project'.
desert anvil
#

How'd you run it?

amber sage
desert anvil
#

What directory are you in?

amber sage
desert anvil
#

Can you push up your changes?

amber sage
#

yeah one sec

#

there you go

desert anvil
#

Ah

#

You named the subdirectory darts-project, but it has to be darts_project

#

With an underscore instead of a hyphen

amber sage
#

ohhhhh

#

i'm getting a 404 when running flask now

desert anvil
#

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`
amber sage
#

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

desert anvil
#

from . import routesshould work

amber sage
#

yeah im getting an error with it

desert anvil
#

what error?

#

Aaah

#

Now I get it

#

Can you show me what your routes.py file looks like?

amber sage
#
# 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

desert anvil
#

and __init__.py?

amber sage
#
# 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'
desert anvil
#

Ah, you have to put the routes import at the end

#

after you create app

#

Should've made that clear

amber sage
#

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

desert anvil
#

I think so...

amber sage
#

let me try

desert anvil
#

I'd put it at the bottom, but it's probably fine.

amber sage
#

woohoo

desert anvil
#

Alright, so do you still see the warning?

amber sage
#

nope all gone

#

it was really bugging me

desert anvil
#

Ok, did vscode select the Python interpreter in the venv?

amber sage
#

must be a mental thing ๐Ÿ˜‚

amber sage
#

i'm pretty sure it has

desert anvil
#

You can see it here:

amber sage
#

yeah 3.13.3 venv

desert anvil
#

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

amber sage
#

yeah its all good now

#

time to go and make this flash message look good

slim scarabBOT
#
Python help channel closed using Discord native close action

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.