5

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?

2 Answers 2

1

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+'/../')
2
  • I knew this was possible, I guess I was just hoping for a neater solution. I guess I could move some code from init.py into a new file (perhaps "app.py") and only import that.
    – ctrlc-root
    Jun 29, 2012 at 21:30
  • Suppose the application package will be installed with setuptools, then does the user have to package the instance file as part of the setuptools config?
    – variable
    Nov 16, 2019 at 7:50
0

Python relative imports are relative to __main__.

If I assume correctly your run.py is the __main__. In that case you should be able to do:

from myapp import app
6
  • Right, but now I want to import app in the service.py file. Actually, run.py runs the debug server, and it's only there to save typing when debugging the application. Like I said, the problem is service.py is inside the myapp folder, and I would prefer to keep it there.
    – ctrlc-root
    Jun 25, 2012 at 2:10
  • And is service.py the __main__? how do you run your service?
    – Kugel
    Jun 25, 2012 at 19:16
  • No, the only main is in run.py. It imports myapp.app (where app is a variable created in the init.py) and then calls app.run().
    – ctrlc-root
    Jun 29, 2012 at 21:26
  • if run.py is main you should be able to import app, exactly as I posted from any file including service.py.
    – Kugel
    Jul 2, 2012 at 10:07
  • Kugel: I've tried that and it doesn't work. Could it be that my package isn't in the python path and so myapp isn't found? Otherwise, perhaps I really am doing something wrong.
    – ctrlc-root
    Jul 2, 2012 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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