13

Is there any sense in using Django framework for developing Command Line Interface tool? In my case there won't be any graphical interface. What benefits can I get using it? Or maybe you know any other useful frameworks for CLI? I'd like to put an accent on making HTTP requests with REST API.

UPDATE: Thanks guys! I would like rather to use REST API than create it in my tool.

asked Aug 19, 2015 at 7:08
1
  • 1
    pyramid is a good, flexible framework, in which you can easily develop a RESTful API. Commented Aug 19, 2015 at 7:20

1 Answer 1

21

While django is primarily for web apps it has a powerful and easy to use ORM that can be used for CLI apps as well. To use django script as a standalone script without a webserver, all you need to do is to add the following to the top of the file.

import os, sys
if __name__ == '__main__':
 # Setup environ
 sys.path.append(os.getcwd())
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings")
 # Setup django
 import django
 django.setup()
 # now you can import your ORM models 
Matt Clegg
5881 gold badge5 silver badges16 bronze badges
answered Aug 19, 2015 at 9:36
Sign up to request clarification or add additional context in comments.

2 Comments

Should have also added that all other imports appear below this section
Anyone knows about a blog or a tutorial with deeper information about using django as CLI application ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.