How to Build Config Files in python?
As I'm new and learning python, exploring different ways to build a config file for python based framework.
I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.
# config.py
class Config:
APP_NAME = 'myapp'
SECRET_KEY = 'secret-key-of-myapp'
ADMIN_NAME = 'administrator'
AWS_DEFAULT_REGION = 'ap-northeast-2'
STATIC_PREFIX_PATH = 'static'
ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
MAX_IMAGE_SIZE = 5242880 # 5MB
class DevelopmentConfig(Config):
DEBUG = True
AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'
DATABASE_URI = 'database-uri-for-dev'
class TestConfig(Config):
DEBUG = True
TESTING = True
AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'
DATABASE_URI = 'database-uri-for-dev'
class ProductionConfig(Config):
DEBUG = False
AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'
DATABASE_URI = 'database-uri-for-dev'
class CIConfig:
SERVICE = 'travis-ci'
HOOK_URL = 'web-hooking-url-from-ci-service'
# main.py
import sys
import config
...
if __name__ == '__main__':
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'
if env == 'dev':
app.config = config.DevelopmentConfig
elif env == 'test':
app.config = config.TestConfig
elif env == 'prod':
app.config = config.ProductionConfig
else:
raise ValueError('Invalid environment name')
app.ci = config.CIConfig
What is app.config and app.ci ? How is it being used ?
- And also, what all other best possible pythonic way to manage config files ?
- If I have multiple set of profiles/credentials (username-password), how do i manage them ?
- Any possible encryption to files containing credentials ?
Will be of great learning to me.
json python-2.7 config
add a comment |
As I'm new and learning python, exploring different ways to build a config file for python based framework.
I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.
# config.py
class Config:
APP_NAME = 'myapp'
SECRET_KEY = 'secret-key-of-myapp'
ADMIN_NAME = 'administrator'
AWS_DEFAULT_REGION = 'ap-northeast-2'
STATIC_PREFIX_PATH = 'static'
ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
MAX_IMAGE_SIZE = 5242880 # 5MB
class DevelopmentConfig(Config):
DEBUG = True
AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'
DATABASE_URI = 'database-uri-for-dev'
class TestConfig(Config):
DEBUG = True
TESTING = True
AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'
DATABASE_URI = 'database-uri-for-dev'
class ProductionConfig(Config):
DEBUG = False
AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'
DATABASE_URI = 'database-uri-for-dev'
class CIConfig:
SERVICE = 'travis-ci'
HOOK_URL = 'web-hooking-url-from-ci-service'
# main.py
import sys
import config
...
if __name__ == '__main__':
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'
if env == 'dev':
app.config = config.DevelopmentConfig
elif env == 'test':
app.config = config.TestConfig
elif env == 'prod':
app.config = config.ProductionConfig
else:
raise ValueError('Invalid environment name')
app.ci = config.CIConfig
What is app.config and app.ci ? How is it being used ?
- And also, what all other best possible pythonic way to manage config files ?
- If I have multiple set of profiles/credentials (username-password), how do i manage them ?
- Any possible encryption to files containing credentials ?
Will be of great learning to me.
json python-2.7 config
Line 55env = sys.argv[1] if len(sys.argv) > 2 else 'dev'is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
– Bishal
Nov 12 at 6:36
thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
– StackGuru
Nov 12 at 6:39
Not sure, why someone downvoted this question.
– StackGuru
Nov 12 at 6:40
@Bishal How about app.config and app.ci in main.py ?
– StackGuru
Nov 12 at 6:57
add a comment |
As I'm new and learning python, exploring different ways to build a config file for python based framework.
I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.
# config.py
class Config:
APP_NAME = 'myapp'
SECRET_KEY = 'secret-key-of-myapp'
ADMIN_NAME = 'administrator'
AWS_DEFAULT_REGION = 'ap-northeast-2'
STATIC_PREFIX_PATH = 'static'
ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
MAX_IMAGE_SIZE = 5242880 # 5MB
class DevelopmentConfig(Config):
DEBUG = True
AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'
DATABASE_URI = 'database-uri-for-dev'
class TestConfig(Config):
DEBUG = True
TESTING = True
AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'
DATABASE_URI = 'database-uri-for-dev'
class ProductionConfig(Config):
DEBUG = False
AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'
DATABASE_URI = 'database-uri-for-dev'
class CIConfig:
SERVICE = 'travis-ci'
HOOK_URL = 'web-hooking-url-from-ci-service'
# main.py
import sys
import config
...
if __name__ == '__main__':
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'
if env == 'dev':
app.config = config.DevelopmentConfig
elif env == 'test':
app.config = config.TestConfig
elif env == 'prod':
app.config = config.ProductionConfig
else:
raise ValueError('Invalid environment name')
app.ci = config.CIConfig
What is app.config and app.ci ? How is it being used ?
- And also, what all other best possible pythonic way to manage config files ?
- If I have multiple set of profiles/credentials (username-password), how do i manage them ?
- Any possible encryption to files containing credentials ?
Will be of great learning to me.
json python-2.7 config
As I'm new and learning python, exploring different ways to build a config file for python based framework.
I have come across using-built-in-data-structure-complicated-py , couldn't understand main.py . Could you help me with how main.py should look like and how the variables from config.py can be accessed in main.py.
# config.py
class Config:
APP_NAME = 'myapp'
SECRET_KEY = 'secret-key-of-myapp'
ADMIN_NAME = 'administrator'
AWS_DEFAULT_REGION = 'ap-northeast-2'
STATIC_PREFIX_PATH = 'static'
ALLOWED_IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'gif']
MAX_IMAGE_SIZE = 5242880 # 5MB
class DevelopmentConfig(Config):
DEBUG = True
AWS_ACCESS_KEY_ID = 'aws-access-key-for-dev'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-dev'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-dev'
DATABASE_URI = 'database-uri-for-dev'
class TestConfig(Config):
DEBUG = True
TESTING = True
AWS_ACCESS_KEY_ID = 'aws-access-key-for-test'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-test'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-test'
DATABASE_URI = 'database-uri-for-dev'
class ProductionConfig(Config):
DEBUG = False
AWS_ACCESS_KEY_ID = 'aws-access-key-for-prod'
AWS_SECERT_ACCESS_KEY = 'aws-secret-access-key-for-prod'
AWS_S3_BUCKET_NAME = 'aws-s3-bucket-name-for-prod'
DATABASE_URI = 'database-uri-for-dev'
class CIConfig:
SERVICE = 'travis-ci'
HOOK_URL = 'web-hooking-url-from-ci-service'
# main.py
import sys
import config
...
if __name__ == '__main__':
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'
if env == 'dev':
app.config = config.DevelopmentConfig
elif env == 'test':
app.config = config.TestConfig
elif env == 'prod':
app.config = config.ProductionConfig
else:
raise ValueError('Invalid environment name')
app.ci = config.CIConfig
What is app.config and app.ci ? How is it being used ?
- And also, what all other best possible pythonic way to manage config files ?
- If I have multiple set of profiles/credentials (username-password), how do i manage them ?
- Any possible encryption to files containing credentials ?
Will be of great learning to me.
json python-2.7 config
json python-2.7 config
edited Nov 12 at 9:38
asked Nov 12 at 6:33
StackGuru
557
557
Line 55env = sys.argv[1] if len(sys.argv) > 2 else 'dev'is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
– Bishal
Nov 12 at 6:36
thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
– StackGuru
Nov 12 at 6:39
Not sure, why someone downvoted this question.
– StackGuru
Nov 12 at 6:40
@Bishal How about app.config and app.ci in main.py ?
– StackGuru
Nov 12 at 6:57
add a comment |
Line 55env = sys.argv[1] if len(sys.argv) > 2 else 'dev'is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.
– Bishal
Nov 12 at 6:36
thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
– StackGuru
Nov 12 at 6:39
Not sure, why someone downvoted this question.
– StackGuru
Nov 12 at 6:40
@Bishal How about app.config and app.ci in main.py ?
– StackGuru
Nov 12 at 6:57
Line 55
env = sys.argv[1] if len(sys.argv) > 2 else 'dev' is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.– Bishal
Nov 12 at 6:36
Line 55
env = sys.argv[1] if len(sys.argv) > 2 else 'dev' is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.– Bishal
Nov 12 at 6:36
thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
– StackGuru
Nov 12 at 6:39
thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
– StackGuru
Nov 12 at 6:39
Not sure, why someone downvoted this question.
– StackGuru
Nov 12 at 6:40
Not sure, why someone downvoted this question.
– StackGuru
Nov 12 at 6:40
@Bishal How about app.config and app.ci in main.py ?
– StackGuru
Nov 12 at 6:57
@Bishal How about app.config and app.ci in main.py ?
– StackGuru
Nov 12 at 6:57
add a comment |
1 Answer
1
active
oldest
votes
Here is a small example of how you could use config files
class Config:
APP_NAME='myapp'
ADMIN='admin'
class DevelopmentConfig(Config):
DEBUG = True
ADMIN = 'dev_admin'
class ProductionConfig(Config):
DEBUG = False
def main():
config = ProductionConfig # Change to DevelopmentConfig to experiment
# You may now use your config where you want
print(config.DEBUG)
print(config.ADMIN)
if __name__ == "__main__":
main()
This example does not use command line arguments like your example but should give you a good idea of building config files and using them.
In your example app.ci refers to configuration for continuous integration(CI) environment.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256964%2fhow-to-build-config-files-in-python%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
Here is a small example of how you could use config files
class Config:
APP_NAME='myapp'
ADMIN='admin'
class DevelopmentConfig(Config):
DEBUG = True
ADMIN = 'dev_admin'
class ProductionConfig(Config):
DEBUG = False
def main():
config = ProductionConfig # Change to DevelopmentConfig to experiment
# You may now use your config where you want
print(config.DEBUG)
print(config.ADMIN)
if __name__ == "__main__":
main()
This example does not use command line arguments like your example but should give you a good idea of building config files and using them.
In your example app.ci refers to configuration for continuous integration(CI) environment.
add a comment |
Here is a small example of how you could use config files
class Config:
APP_NAME='myapp'
ADMIN='admin'
class DevelopmentConfig(Config):
DEBUG = True
ADMIN = 'dev_admin'
class ProductionConfig(Config):
DEBUG = False
def main():
config = ProductionConfig # Change to DevelopmentConfig to experiment
# You may now use your config where you want
print(config.DEBUG)
print(config.ADMIN)
if __name__ == "__main__":
main()
This example does not use command line arguments like your example but should give you a good idea of building config files and using them.
In your example app.ci refers to configuration for continuous integration(CI) environment.
add a comment |
Here is a small example of how you could use config files
class Config:
APP_NAME='myapp'
ADMIN='admin'
class DevelopmentConfig(Config):
DEBUG = True
ADMIN = 'dev_admin'
class ProductionConfig(Config):
DEBUG = False
def main():
config = ProductionConfig # Change to DevelopmentConfig to experiment
# You may now use your config where you want
print(config.DEBUG)
print(config.ADMIN)
if __name__ == "__main__":
main()
This example does not use command line arguments like your example but should give you a good idea of building config files and using them.
In your example app.ci refers to configuration for continuous integration(CI) environment.
Here is a small example of how you could use config files
class Config:
APP_NAME='myapp'
ADMIN='admin'
class DevelopmentConfig(Config):
DEBUG = True
ADMIN = 'dev_admin'
class ProductionConfig(Config):
DEBUG = False
def main():
config = ProductionConfig # Change to DevelopmentConfig to experiment
# You may now use your config where you want
print(config.DEBUG)
print(config.ADMIN)
if __name__ == "__main__":
main()
This example does not use command line arguments like your example but should give you a good idea of building config files and using them.
In your example app.ci refers to configuration for continuous integration(CI) environment.
answered Nov 12 at 22:31
Bishal
558216
558216
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256964%2fhow-to-build-config-files-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Line 55
env = sys.argv[1] if len(sys.argv) > 2 else 'dev'is reading your command line arguments for instantiating your environment, defaulting to dev. The example your have brought up seems like a good way to do it. I would also recommend using python crypto library for your encryption needs.– Bishal
Nov 12 at 6:36
thanks @Bishal. understood about line 55, further app.config and how do i use them ? Can you guide me to build a simple main.py ?
– StackGuru
Nov 12 at 6:39
Not sure, why someone downvoted this question.
– StackGuru
Nov 12 at 6:40
@Bishal How about app.config and app.ci in main.py ?
– StackGuru
Nov 12 at 6:57