Running a simple python flask application using .cfg config files. I can't seem to return the config values, getting a keying error with them










0















The aim of this program is just to return the values that are passed from a .cfg configuration file called 'defaults.cfg'.



I totally understand what should be getting passed here and to be honest the code for all intents and purposes is copied from an exercise, but it fails with a 'Keying error: (value)' (all values give the keying error, it's just whatever is first) and I don't know why. I've been unable to find a solution online and the code is the same in principle as a friend's more complicated program running a proper web application and his works just fine.



Apparently using capitals for the config keys is a thing and I've done that and I'm sure I have all the necessary libraries/binaries installed.



I'm doing this on Bash on Windows on Ubuntu.



Thanks in advance for any consideration.



default.cfg



[config]
DEBUG = True
IP_ADDRESS = 0.0.0.0
PORT = 5000


configuration.py



import ConfigParser

from flask import Flask

app = Flask(__name__)

@app.route('/')
def root():
return "Sup! Hollerin' at ya from the configuration testing app"

@app.route('/WTF/')
def tellMeh():
return app.config['PORT']

@app.route('/config/')
def config():
str =
str.append(app.config['DEBUG'])
str.append('port:'+app.config['PORT'])
str.append('ip_address:'+app.config['IP'])
return 't'.join(str)

def init(app):
config = ConfigParser.ConfigParser()
try:
config_location = "etc/defaults.cfg"
config.read(config_location)

app.config['DEBUG'] = config.get("config", "DEBUG")
app.config['IP'] = config.get("config", "IP_ADDRESS")
app.config['PORT'] = config.get("config", "PORT")

print "Succesfully read configs from: ", config_location
except:
print "Couldn't read configs from: ", config_location

if __name__ == '__main__':
init(app)
app.run(
host=app.config['IP'],
port=int(app.config['PORT']))









share|improve this question






















  • Also, I can get 'app.config['DEBUG']' to return but I think this is because it's a value that's held by default, rather than the value I created. See : flask.pocoo.org/docs/0.12/config

    – S.Smoonery
    Nov 14 '18 at 19:59






  • 1





    Are you sure that config.read(config_location) is succeeding? Check the return value. The working directory when the app is run may be such that etc/defaults.cfg doesn't exist.

    – jwodder
    Nov 14 '18 at 20:02















0















The aim of this program is just to return the values that are passed from a .cfg configuration file called 'defaults.cfg'.



I totally understand what should be getting passed here and to be honest the code for all intents and purposes is copied from an exercise, but it fails with a 'Keying error: (value)' (all values give the keying error, it's just whatever is first) and I don't know why. I've been unable to find a solution online and the code is the same in principle as a friend's more complicated program running a proper web application and his works just fine.



Apparently using capitals for the config keys is a thing and I've done that and I'm sure I have all the necessary libraries/binaries installed.



I'm doing this on Bash on Windows on Ubuntu.



Thanks in advance for any consideration.



default.cfg



[config]
DEBUG = True
IP_ADDRESS = 0.0.0.0
PORT = 5000


configuration.py



import ConfigParser

from flask import Flask

app = Flask(__name__)

@app.route('/')
def root():
return "Sup! Hollerin' at ya from the configuration testing app"

@app.route('/WTF/')
def tellMeh():
return app.config['PORT']

@app.route('/config/')
def config():
str =
str.append(app.config['DEBUG'])
str.append('port:'+app.config['PORT'])
str.append('ip_address:'+app.config['IP'])
return 't'.join(str)

def init(app):
config = ConfigParser.ConfigParser()
try:
config_location = "etc/defaults.cfg"
config.read(config_location)

app.config['DEBUG'] = config.get("config", "DEBUG")
app.config['IP'] = config.get("config", "IP_ADDRESS")
app.config['PORT'] = config.get("config", "PORT")

print "Succesfully read configs from: ", config_location
except:
print "Couldn't read configs from: ", config_location

if __name__ == '__main__':
init(app)
app.run(
host=app.config['IP'],
port=int(app.config['PORT']))









share|improve this question






















  • Also, I can get 'app.config['DEBUG']' to return but I think this is because it's a value that's held by default, rather than the value I created. See : flask.pocoo.org/docs/0.12/config

    – S.Smoonery
    Nov 14 '18 at 19:59






  • 1





    Are you sure that config.read(config_location) is succeeding? Check the return value. The working directory when the app is run may be such that etc/defaults.cfg doesn't exist.

    – jwodder
    Nov 14 '18 at 20:02













0












0








0








The aim of this program is just to return the values that are passed from a .cfg configuration file called 'defaults.cfg'.



I totally understand what should be getting passed here and to be honest the code for all intents and purposes is copied from an exercise, but it fails with a 'Keying error: (value)' (all values give the keying error, it's just whatever is first) and I don't know why. I've been unable to find a solution online and the code is the same in principle as a friend's more complicated program running a proper web application and his works just fine.



Apparently using capitals for the config keys is a thing and I've done that and I'm sure I have all the necessary libraries/binaries installed.



I'm doing this on Bash on Windows on Ubuntu.



Thanks in advance for any consideration.



default.cfg



[config]
DEBUG = True
IP_ADDRESS = 0.0.0.0
PORT = 5000


configuration.py



import ConfigParser

from flask import Flask

app = Flask(__name__)

@app.route('/')
def root():
return "Sup! Hollerin' at ya from the configuration testing app"

@app.route('/WTF/')
def tellMeh():
return app.config['PORT']

@app.route('/config/')
def config():
str =
str.append(app.config['DEBUG'])
str.append('port:'+app.config['PORT'])
str.append('ip_address:'+app.config['IP'])
return 't'.join(str)

def init(app):
config = ConfigParser.ConfigParser()
try:
config_location = "etc/defaults.cfg"
config.read(config_location)

app.config['DEBUG'] = config.get("config", "DEBUG")
app.config['IP'] = config.get("config", "IP_ADDRESS")
app.config['PORT'] = config.get("config", "PORT")

print "Succesfully read configs from: ", config_location
except:
print "Couldn't read configs from: ", config_location

if __name__ == '__main__':
init(app)
app.run(
host=app.config['IP'],
port=int(app.config['PORT']))









share|improve this question














The aim of this program is just to return the values that are passed from a .cfg configuration file called 'defaults.cfg'.



I totally understand what should be getting passed here and to be honest the code for all intents and purposes is copied from an exercise, but it fails with a 'Keying error: (value)' (all values give the keying error, it's just whatever is first) and I don't know why. I've been unable to find a solution online and the code is the same in principle as a friend's more complicated program running a proper web application and his works just fine.



Apparently using capitals for the config keys is a thing and I've done that and I'm sure I have all the necessary libraries/binaries installed.



I'm doing this on Bash on Windows on Ubuntu.



Thanks in advance for any consideration.



default.cfg



[config]
DEBUG = True
IP_ADDRESS = 0.0.0.0
PORT = 5000


configuration.py



import ConfigParser

from flask import Flask

app = Flask(__name__)

@app.route('/')
def root():
return "Sup! Hollerin' at ya from the configuration testing app"

@app.route('/WTF/')
def tellMeh():
return app.config['PORT']

@app.route('/config/')
def config():
str =
str.append(app.config['DEBUG'])
str.append('port:'+app.config['PORT'])
str.append('ip_address:'+app.config['IP'])
return 't'.join(str)

def init(app):
config = ConfigParser.ConfigParser()
try:
config_location = "etc/defaults.cfg"
config.read(config_location)

app.config['DEBUG'] = config.get("config", "DEBUG")
app.config['IP'] = config.get("config", "IP_ADDRESS")
app.config['PORT'] = config.get("config", "PORT")

print "Succesfully read configs from: ", config_location
except:
print "Couldn't read configs from: ", config_location

if __name__ == '__main__':
init(app)
app.run(
host=app.config['IP'],
port=int(app.config['PORT']))






python flask






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 19:51









S.SmooneryS.Smoonery

64




64












  • Also, I can get 'app.config['DEBUG']' to return but I think this is because it's a value that's held by default, rather than the value I created. See : flask.pocoo.org/docs/0.12/config

    – S.Smoonery
    Nov 14 '18 at 19:59






  • 1





    Are you sure that config.read(config_location) is succeeding? Check the return value. The working directory when the app is run may be such that etc/defaults.cfg doesn't exist.

    – jwodder
    Nov 14 '18 at 20:02

















  • Also, I can get 'app.config['DEBUG']' to return but I think this is because it's a value that's held by default, rather than the value I created. See : flask.pocoo.org/docs/0.12/config

    – S.Smoonery
    Nov 14 '18 at 19:59






  • 1





    Are you sure that config.read(config_location) is succeeding? Check the return value. The working directory when the app is run may be such that etc/defaults.cfg doesn't exist.

    – jwodder
    Nov 14 '18 at 20:02
















Also, I can get 'app.config['DEBUG']' to return but I think this is because it's a value that's held by default, rather than the value I created. See : flask.pocoo.org/docs/0.12/config

– S.Smoonery
Nov 14 '18 at 19:59





Also, I can get 'app.config['DEBUG']' to return but I think this is because it's a value that's held by default, rather than the value I created. See : flask.pocoo.org/docs/0.12/config

– S.Smoonery
Nov 14 '18 at 19:59




1




1





Are you sure that config.read(config_location) is succeeding? Check the return value. The working directory when the app is run may be such that etc/defaults.cfg doesn't exist.

– jwodder
Nov 14 '18 at 20:02





Are you sure that config.read(config_location) is succeeding? Check the return value. The working directory when the app is run may be such that etc/defaults.cfg doesn't exist.

– jwodder
Nov 14 '18 at 20:02












1 Answer
1






active

oldest

votes


















0














You'll get different behavior from that code depending on how you invoke it.



FLASK_APP=configuration.py flask run will skip the section at the bottom where init(app) is called



python configuration.py will run that section, calling init(app).



You might wish to move the call to init() to right below app = Flask(...).






share|improve this answer























  • Thank you! That seems to have been what was happening. I was running it with 'python -m flask run'. I assumed that as I hadn't set the IP and port in the commandline and the thing was running then it must have been getting the values from the .cfg

    – S.Smoonery
    Nov 16 '18 at 22:53










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307795%2frunning-a-simple-python-flask-application-using-cfg-config-files-i-cant-seem%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You'll get different behavior from that code depending on how you invoke it.



FLASK_APP=configuration.py flask run will skip the section at the bottom where init(app) is called



python configuration.py will run that section, calling init(app).



You might wish to move the call to init() to right below app = Flask(...).






share|improve this answer























  • Thank you! That seems to have been what was happening. I was running it with 'python -m flask run'. I assumed that as I hadn't set the IP and port in the commandline and the thing was running then it must have been getting the values from the .cfg

    – S.Smoonery
    Nov 16 '18 at 22:53















0














You'll get different behavior from that code depending on how you invoke it.



FLASK_APP=configuration.py flask run will skip the section at the bottom where init(app) is called



python configuration.py will run that section, calling init(app).



You might wish to move the call to init() to right below app = Flask(...).






share|improve this answer























  • Thank you! That seems to have been what was happening. I was running it with 'python -m flask run'. I assumed that as I hadn't set the IP and port in the commandline and the thing was running then it must have been getting the values from the .cfg

    – S.Smoonery
    Nov 16 '18 at 22:53













0












0








0







You'll get different behavior from that code depending on how you invoke it.



FLASK_APP=configuration.py flask run will skip the section at the bottom where init(app) is called



python configuration.py will run that section, calling init(app).



You might wish to move the call to init() to right below app = Flask(...).






share|improve this answer













You'll get different behavior from that code depending on how you invoke it.



FLASK_APP=configuration.py flask run will skip the section at the bottom where init(app) is called



python configuration.py will run that section, calling init(app).



You might wish to move the call to init() to right below app = Flask(...).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 20:48









Dave W. SmithDave W. Smith

16.4k22530




16.4k22530












  • Thank you! That seems to have been what was happening. I was running it with 'python -m flask run'. I assumed that as I hadn't set the IP and port in the commandline and the thing was running then it must have been getting the values from the .cfg

    – S.Smoonery
    Nov 16 '18 at 22:53

















  • Thank you! That seems to have been what was happening. I was running it with 'python -m flask run'. I assumed that as I hadn't set the IP and port in the commandline and the thing was running then it must have been getting the values from the .cfg

    – S.Smoonery
    Nov 16 '18 at 22:53
















Thank you! That seems to have been what was happening. I was running it with 'python -m flask run'. I assumed that as I hadn't set the IP and port in the commandline and the thing was running then it must have been getting the values from the .cfg

– S.Smoonery
Nov 16 '18 at 22:53





Thank you! That seems to have been what was happening. I was running it with 'python -m flask run'. I assumed that as I hadn't set the IP and port in the commandline and the thing was running then it must have been getting the values from the .cfg

– S.Smoonery
Nov 16 '18 at 22:53



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307795%2frunning-a-simple-python-flask-application-using-cfg-config-files-i-cant-seem%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin