<< ALL BLOG POSTS

Building Python Command Line Tools, Part 2: Console Scripts

Table of Contents

Over the past few months at Six Feet Up, I have noticed an increasing need to demonstrate the usefulness and efficiency of Python command line tools. Command line tools can be very handy, and, making your own using Python is easier than you might think! The information in this article is part two of our series on building a command line app and will show you how to execute and install console_scripts. Did you miss part one on getting started with Command Line Tools? Read it here.

How Do You Hook This Up to Make an Executable Script in My Pyramid or Django App?

Now that you can read in arguments from the command line using argparse, how do you try it out and actually get the command line tool generated so you can run it?

How to Make a Console Script for your utility

Ok, now that you have read in the arguments to our script, you need to make sure that a script is created when someone installs our product. The setup.py handles this for you by defining a [console_scripts] entry points section like this:

entry_points = """\
  [paste.app_factory]
  main = myapp:main

  [console_scripts]
  import_sessions = myapp.scripts:import_sessions
  """,

This allows your Python function to be registered as a command line tool. With that in place, you will have a script called import_sessions installed into your environment and ready for you to load data with once you have fleshed out the rest of the script.

RELATED POSTS:

  • Part 1: Console Scripts
    • The first post of this series focuses on how to get started with building a command line app.
  • Part 3: Bootstrapping Pyramid
    • In the next post in this series, we put some meat into our script that will actually interact with our Pyramid application. We show bootstrapping the app so we have the full environment ready to use with the database.
  • Part 4: CSV Importing and Time Zones
    • With our application up and running inside our script, we can now manipulate the database and do things like import a CSV file of new data. Our fourth post highlights the strengths of the standard library csv module and shows you how to watch out for datetime timezone issues.

Was this article useful? Be sure to stay tuned for the next installment and sign up for our Python How-To digests to receive more how-to guides as soon as they are published!

Related Posts
How can we assist you?
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.