简体   繁体   中英

Python, using variables from __init__ in a parent dir

I have a website I've been writing using Flask, although for this question I do not think that's relevant. This is the folder structure I'm working with. Rhea is the name of the project and the parent directory.

Rhea
|- Scripts
    |-script1.py
|- Static
|- Templates
|- __init__

My problem is I declare variables inside my init that I need to use inside script1.py. How do I import these into script1.py?

For reference this is my init file

import os
from flask import Flask
from flask_appconfig import AppConfig
from flask_bootstrap import Bootstrap
from flask.ext.sqlachemy import SQLAlchemy

from .frontend import frontend
from .nav import nav
from .models import User

basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db', 'userdb.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db')
WTF_CSRF_ENABLED = True
BOOTSTRAP_SERVE_LOCAL = True
SECRET_KEY = --SNIP--

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

AppConfig(app)
Bootstrap(app)
db = SQLAlchemy(app)
app.register_blueprint(frontend)
nav.init_app(app)

app.run(host='0.0.0.0', port=6060, debug=False);

return app

The variables I need is db, SQLALCHEMY_DATABASE_URI, and the SQLALCHEMY_MIGRATE_REPO.

Thank's for any help.

A solution is to append the the sys.path with the Rhea 's package parent directory. But it's ugly.

# cript1.py 
import os
import sys

rhea_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.append(rhea_dir)

import Rhea
print Rhea.SQLALCHEMY_DATABASE_URI

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