LLL
LLL

Reputation: 289

when __init__ is directly run, it can import a module, but when __init__ is imported, it cannot import the module

I have 3 script structured as below

server.py
database/__init__.py
database/databaseConfig.py

__init__.py imported databaseConfig.py. When __init__.py is ran directly, it executes successfully. in server.py, there is import database. However, when server.py is ran, it throws the following error

ModuleNotFoundError: No module named 'databaseConfig'

Why is this happening, and how to fix it ?

Here is the script for __init__.py

from pony.orm import Database
from databaseConfig import getDatabaseConfig

db = Database()
databaseConfig = getDatabaseConfig()
db.bind(databaseConfig["provider"], 
        user=databaseConfig["database_user"], 
        password=databaseConfig["database_user_password"], 
        host=databaseConfig["host"],
        database=databaseConfig["database_name"])

Here is the script for databaseConfig.py

def getDatabaseConfig():
    databaseConfig = { 
        "provider" : "postgres",
        "database_user" : "FlaCommerceServer",
        "database_user_password" : "password",
        "host" : "localhost",
        "database_name" : "FlaCommerce"
    }
    return databaseConfig

and server.py

from flask import Flask
from serverConfig import ServerConfig
import database

app = Flask(__name__)
app.config.from_object(ServerConfig)

if __name__ == "__main__":
    app.run(host=app.config["SERVER_HOST"],port=app.config["SERVER_PORT"])

Upvotes: 0

Views: 32

Answers (0)

Related Questions