Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:56:32+00:00 2026-06-01T16:56:32+00:00

I often have to write data parsing scripts, and I’d like to be able

  • 0

I often have to write data parsing scripts, and I’d like to be able to run them in two different ways: as a module and as a standalone script. So, for example:

def parseData(filename):
    # data parsing code here
    return data

def HypotheticalCommandLineOnlyHappyMagicFunction():
    print json.dumps(parseData(sys.argv[1]), indent=4)

the idea here being that in another python script I can call import dataparser and have access to dataParser.parseData in my script, or on the command line I can just run python dataparser.py and it would run my HypotheticalCommandLineOnlyHappyMagicFunction and shunt the data as json to stdout. Is there a way to do this in python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:56:33+00:00Added an answer on June 1, 2026 at 4:56 pm

    The standard way to do this is to guard the code that should be only run when the script is called stand-alone by

    if __name__ == "__main__":
        # Your main script code
    

    The code after this if won’t be run if the module is imported.

    The __name__ special variable contains the name of the current module as a string. If your file is called glonk.py, then __name__ will be "glonk" if the the file is imported as a module and it will be "__main__" if the file is run as a stand-alone script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often run into the problem that I have one stream full of data
I'm using Squirrel SQL with Oracle. I often have to write quick queries for
I often have data in Excel or text that I need to get into
I often have customer requesting changing properties, like the version history setting on all
I often write queries wherein I pivot data and end up with NULL values
I have the following boilerplate that I do quite often, and would like to
I have to write a bunch of DTOs (Data Transfer Objects) - their sole
I often have data where I want to compare the value of one level
I have a multithreaded application, where a shared list has write-often, read-occasionally behaviour. Specifically,
Often during writing JavaScript, we have to write configuration object. For example Ext.define('User', {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.