-1

I am running a flask application and connecting to database with Flask-mysqlAlchemy when I am running my script with python 2.7 I am getting below error.

Traceback (most recent call last):
  File "app2.py", line 8, in <module>
    from database.dbconfig import db, myAccounts2
ImportError: No module named database.dbconfig

whereas this is running fine in python3 I need this running in python 2.7 as my server is pre-installed with it. I am not able to figure out what the issue is. I have installed all dependencies in my server and keep getting this where as it works in my local machine with python3.

Here is my main script

My directory is like this

Main folder
|
+--->database
|   |
|   +------> dbconfig.py
|   
+----->app2.py  

Here is my app2.py

#!usr/bin/python
import boto3
import json
import urllib2
import urlparse
#import urllib.request
#import urllib.parse
from database.dbconfig import db, myAccounts2
from flask_sqlalchemy import SQLAlchemy
from flask import Flask,render_template,jsonify,json,request

application = Flask(__name__)

Here is my dbconfig.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:[email protected]:3306/test_pb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']= 'False'
app.config['SECRET_KEY'] = "random string22"
db = SQLAlchemy(app)

class myAccounts2(db.Model):
    #__tablename__ = 'myAccounts'
    id = db.Column(db.Integer, primary_key=True)
    account_name = db.Column(db.String(45), primary_key=True)
    vpc = db.Column(db.String(55))
    subnet = db.Column(db.String(55))
    instance_type = db.Column(db.String(90))

    def __init__(self,  account_name, vpc, subnet, instance_type):
        #self.id = id
        self.account_name = account_name
        self.vpc=vpc
        self.subnet=subnet
        self.instance_type=instance_type
0

2 Answers 2

3

I think that with Python 2, you will have to put an empty

__init__.py 

file in your database directory

1
  • thank you init.py solved my issue. Commented Jul 12, 2017 at 20:33
0

First your database folder isn't a proper python package.

Each package in Python is a directory which MUST contain a special file called __init__.py. This file can be empty, and it indicates that the directory it contains is a Python package, so it can be imported the same way a module can be imported. From learn python website

Second (and this is just a suggestion), to enforce a proper namespace you should create a package named after your app inside of your project directory and on top of every other packages:

myproject/
  requirements.txt
  setup.cfg
  ...
  myapp/
     __init__.py
     app2.py
     database/
         __init__.py
         dbconfig.py

Note that package name double always be lowercase and if needed use underscores as separators instead of a dash.

The namespace will be:

myapp.database.dbconfig.db

So from app2.py you can do for example:

from .database.dbconfig import db

Also your code declare the flask object both in app2.py and dbconfig.py. Therefore the application object in the app2.py module isn't bound to the Sqlalchemy object. You should declare your db in dbconfig then import it in app2.py and use the init_app method.

Finally

flat is better than nested

Zen of python

So this tree can be reduce to just a database.py containing the models and db object and the flask app object creation inside the myapp/__init__.py

Not the answer you're looking for? Browse other questions tagged or ask your own question.