简体   繁体   中英

Using Mixer with Flask-SQLAlchemy

I am trying to use mixer to generate test data for a website powered by Flask+SQLAlchemy.

I have created a file called generate_test_data.py in the root directory of my project that looks like this:

# -*- coding: utf-8 -*-

import app
from mixer.backend.flask import mixer
from models import *

def generate_test_data():
        user = mixer.blend(Users)

if __name__ == "__main__":
        generate_test_data()

Yet, every time I run the file I get the following error:

Traceback (most recent call last):
  File "generate_test_data.py", line 3, in <module>
    import app
ImportError: No module named app

I am doing this with the correct virtualenv activated.

Update

My project structure looks like this:

/
 - generate_test_data.py
 - app/
 -- __init__.py
 -- models.py

** Update**

My __init__.py :

# -*- coding: utf-8 -*-

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.uwsgi_websocket import GeventWebSocket
from config import REDIS_HOST, REDIS_PORT
import redis


app = Flask(__name__)
db = SQLAlchemy(app)
ws = GeventWebSocket(app)

POOL = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=0)
redis = redis.Redis(connection_pool=POOL)

from app import views, models

I would re-structure your project strucuture as:

myproject/
    generate_test_data.py
    __init__.py
    models.py

Then, in your generate_test_data.py , you can just do

from myproject import app

This will be better because you are removing any name conflicts. Also, you are explicitly importing your app variable which you really want. You can of course name "myproject" to whatever you want but don't name it "app" to be safe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM