I'm not sure if this is a flask specific issue or if I simply lack an understanding of the python import mechanism, but I'm having some problems with a flask web application. Here's the layout of my application:
/myapp
/INSTALL
/TODO
/run.py
/instance
/application.cfg
/myapp
/static
/templates
/__init__.py
/config.py
/service.py
The config.py file stores the default application configuration, and application.cfg stores instance configuration. They both look like this:
DEBUG = False
TESTING = False
SECRET_KEY = "please_replace_me"
This is how I set up the application in init.py:
import flask
app = Flask(__name__, instance_relative_config=True)
app.config.from_object("myapp.config")
app.config.from_pyfyle("application.cfg", silent=True)
Now what I want is to access the app.config object from service.py, which is not included by init.py or any of the other modules that are part of the web application. I.e. this is how I want my service.py file to read:
from somewhere.somehow import app
def run():
do_stuff(app.config["CONFIG_OPTION"])
The problem is the service.py file has to be inside the package, so I can't move it one folder up and just import myapp. How do I do this?
If you want to import the module which is located in the parent directory, one possible way to do it is to add the parent directory to sys.path before importing:
cmd_folder = os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe() ))[0])
sys.path.append(cmd_folder+'/../')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With