RocketSocks22
RocketSocks22

Reputation: 411

Testing scripts with Flask

I'm struggling to create new scripts for the Flask app I'm creating. I have the following directory structure:

    /myapp
        /config.py  
        /app  
            /__init__.py
            /routes.py  
            /models.py  
            /niche_script.py  
            /templates/  
                /...
        /venv

__init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from config Import Config

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)

from app import routes, models, niche_script

niche_script.py

try:
    from app import app, db
    from app.models import User
    print("Import 1")
except:
    print("Import 2")

'''do stuff'''
if __name__="__main__":
     print("do stuff")

When I run the app with flask run and call the functionality of the script, Import 1 is printed. However, if I try to run the script on it's own, Import 2 is printed. I'd like to be able to run the script standalone as a means to test out some database queries without having to run the flask app every time. However, as it stands I'm not sure how to import the db into my script if the script run standalone does not recognize app as a module.

On a side note, Is the usual workflow to just do all of this in a shell? Or is there a better development process that I'm clueless of

Upvotes: 0

Views: 128

Answers (1)

jadkik94
jadkik94

Reputation: 7078

The behavior will be different based on where you're running it from and how you're installing your package.

First thing you can do to get it working quick and easy is to add the full path to /myapp to your PYTHONPATH environment variable. The flask run command most likely adds your current working directory to your python path automatically (or you already configured it to look somewhere specific with FLASK_APP).

Try cd-ing into different directories and see how it differs.

The second "cleaner" approach would be to have your app package as an installable package. That means you create a basic setup.py file for it, and then you can pip install -e /path/to/myapp/ and then everything is setup "the proper way" without you having to mess with the environment variables and so on.

It's worth mentioning that when doing these kind of scripts and Flask is involved you have to keep in mind that an "app context" and "request context" may be needed in some things that you do. When you run the app "the normal way" it is all managed for you, but in such cases you might have to do that yourself: there's plenty of docs and examples about that but it may not be straightforward at first. It's not clear from your post if you already know that, but it doesn't hurt to tell you about it in case you didn't.

Some general recommendations

One thing I would recommend, that is not strictly related to your question, is to move your niche_script.py file out of the app package. If it's only there to test some stuff and as a playground to quickly iterate some features, it probably shouldn't be part of your "main" code base.

The way I do this kind of stuff is with a script in a specific directory that I ignore from git (only on my machine, not the real .gitignore) and then real tests that actually test useful stuff and not some experiments will go into a dedicated tests/ directory (I use pytest for that).

I hope this helps.

Upvotes: 1

Related Questions