ImportError:没有名为应用程序的模块

ImportError:没有名为应用程序的模块

问题描述:

当我使用python 2.7运行脚本时,我正在运行Flask应用程序并使用Flask-mysqlAlchemy连接到数据库,而我却遇到了以下错误.

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

尽管它在python3中运行良好,但我需要在python 2.7中运行,因为我的服务器已预先安装了它.我无法弄清楚问题是什么.我已经在服务器中安装了所有依赖项,并使用python3使其在本地计算机中正常运行.

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.

这是我的主要剧本

我的目录就是这样

Main folder
|
+--->database
|   |
|   +------> dbconfig.py
|   
+----->app2.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__)

这是我的dbconfig.py

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:password@pushbuttontest.us-east-1.rds.amazonaws.com: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

我认为使用Python 2时,您将必须放置一个空

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

__init__.py 

文件在您的数据库目录中

file in your database directory