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 9185991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:24:49+00:00 2026-06-17T19:24:49+00:00

In Python I have a module myModule.py where I define a few functions and

  • 0

In Python I have a module myModule.py where I define a few functions and a main(), which takes a few command line arguments.

I usually call this main() from a bash script. Now, I would like to put everything into a small package, so I thought that maybe I could turn my simple bash script into a Python script and put it in the package.

So, how do I actually call the main() function of myModule.py from the main() function of MyFormerBashScript.py? Can I even do that? How do I pass any arguments to it?

  • 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-17T19:24:50+00:00Added an answer on June 17, 2026 at 7:24 pm

    It’s just a function. Import it and call it:

    import myModule
    
    myModule.main()
    

    If you need to parse arguments, you have two options:

    • Parse them in main(), but pass in sys.argv as a parameter (all code below in the same module myModule):

      def main(args):
          # parse arguments using optparse or argparse or what have you
      
      if __name__ == '__main__':
          import sys
          main(sys.argv[1:])
      

      Now you can import and call myModule.main(['arg1', 'arg2', 'arg3']) from other another module.

    • Have main() accept parameters that are already parsed (again all code in the myModule module):

      def main(foo, bar, baz='spam'):
          # run with already parsed arguments
      
      if __name__ == '__main__':
          import sys
          # parse sys.argv[1:] using optparse or argparse or what have you
          main(foovalue, barvalue, **dictofoptions)
      

      and import and call myModule.main(foovalue, barvalue, baz='ham') elsewhere and passing in python arguments as needed.

    The trick here is to detect when your module is being used as a script; when you run a python file as the main script (python filename.py) no import statement is being used, so python calls that module "__main__". But if that same filename.py code is treated as a module (import filename), then python uses that as the module name instead. In both cases the variable __name__ is set, and testing against that tells you how your code was run.

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

Sidebar

Related Questions

In a Python system for which I develop, we usually have this module structure.
I have a C/C++ application in which I define a Python module. I set
I have a python file testHTTPAuth.py which uses module deliciousapi and is kept in
I have some python module, which has a class ModuleClass and I can't modify
I'm using boost python. I've exported some function which takes class CL_DomElement in arguments.
I have a module installed in the main python install, however, I'd like to
I'm working on making my first Python C extension, which defines a few functions
I have a Python module which uses some resources in a subdirectory of the
I have a python module that imports a module generated with swig. When I
I have a python module that I've been using over the years to process

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.