Forums

[Flask]Cannot load config file

Hi I'm trying to load a config file and set it in an environment variable but it will throw an exception. Here's the code in wsgi.py file.

import sys
# add your project directory to the sys.path
project_home = u'/home/coinage/mysite'
config_file = project_home + u"/config.py"
if project_home not in sys.path:
    sys.path.append(project_home)

import os 
os.environ['APP_SETTINGS'] = config_file

# import flask app but need to call it "application" for WSGI to work
from coinage import app as application

Here's the error: ImportError: Import by filename is not supported. 2016-08-23 13:29:27,952 :Error running WSGI application Traceback (most recent call last): File "/bin/user_wsgi_wrapper.py", line 154, in call app_iterator = self.app(environ, start_response) File "/bin/user_wsgi_wrapper.py", line 170, in import_error_application raise e ImportStringError: import_string() failed for '/home/coinage/mysite/config.py'. Possible reasons are:

  • missing init.py in a package;
  • package or module path not included in sys.path;
  • duplicated package or module name taking precedence in sys.path;
  • missing module, class, function or variable;

Debugged import:

  • '/home/coinage/mysite/config' not found.

Original exception:

ImportError: Import by filename is not supported.

Any help are greatly appreciated.

Thanks.

It looks like Flask is complaining because you're using a filename for your config file instead of a Python module name. Use config instead of config.py for your config_file variable.

I tried your suggestion above but still the same error encountered.

config_file = project_home + u"/config"

error: ImportStringError: import_string() failed for '/home/coinage/mysite/config'. Possible reasons are:

  • missing init.py in a package;
  • package or module path not included in sys.path;
  • duplicated package or module name taking precedence in sys.path;
  • missing module, class, function or variable;

Debugged import:

  • '/home/coinage/mysite/config' not found.

Ah. Then perhaps h=you can try using only "config" as your config file variable.

Thanks, it works!

Excellent! Glad to hear it.