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

  • Home
  • SEARCH
  • 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 3633138
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:36:54+00:00 2026-05-19T00:36:54+00:00

In python, one would usually define a main function, in order to allow the

  • 0

In python, one would usually define a main function, in order to allow the script to be used as module (if needed):

def main():
    print("Hello world")
    return 0

if __name__ == "__main__":
    sys.exit(main())

In Lua, the idiom if __name__ == "__main__" isn’t possible as such (that means, I don’t think it is).

That’s what I’m usually doing in order to have a similar behaviour in Lua:

os.exit((function(args)
    print("Hello world")
    return 0
end)(arg))

… But this approach seems rather “heavy on parentheses” 🙂

Is there a more common approach (besides defining a global main function, which seems redundant)?

  • 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-05-19T00:36:54+00:00Added an answer on May 19, 2026 at 12:36 am

    There’s no “proper” way to do this, since Lua doesn’t really distinguish code by where it came from, they are all just functions. That said, this at least seems to work in Lua 5.1:

    matthew@silver:~$ cat hybrid.lua 
    if pcall(getfenv, 4) then
        print("Library")
    else
        print("Main file")
    end
    matthew@silver:~$ lua hybrid.lua 
    Main file
    matthew@silver:~$ lua -lhybrid
    Library
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > ^C
    matthew@silver:~$ lua
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > require "hybrid"
    Library
    > ^C
    matthew@silver:~$
    

    It works by checking whether the stack depth is greater than 3 (the normal depth for a file in the stock Lua interpreter). This test may break between Lua versions though, and even in any embedded/custom Lua builds.

    I’ll also include this (slightly more portable) alternative, although it’s taking an even greater leap in heuristics, and has a failure case (see below):

    matthew@silver:~$ cat hybrid2.lua 
    function is_main(_arg, ...)
        local n_arg = _arg and #_arg or 0;
        if n_arg == select("#", ...) then
            for i=1,n_arg do
                if _arg[i] ~= select(i, ...) then
                    print(_arg[i], "does not match", (select(i, ...)))
                    return false;
                end
            end
            return true;
        end
        return false;
    end
    
    if is_main(arg, ...) then
        print("Main file");
    else
        print("Library");
    end
    matthew@silver:~$ lua hybrid2.lua 
    Main file
    matthew@silver:~$ lua -lhybrid2
    Library
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > ^C
    matthew@silver:~$ lua 
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    > require "hybrid2"
    Library
    >
    

    This one works by comparing the contents of _G.arg with the contents of ‘…’. In the main chunk they will always be the same. In a module _G.arg will still contain the command-line arguments, but ‘…’ will contain the module name passed to require(). I suspect this is closer to the better solution for you, given that you know your module name. The bug in this code lies when the user executes the main script with 1 argument, and this is the exact name of your module:

    matthew@silver:~$ lua -i hybrid2.lua hybrid2
    Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
    Main file
    > require "hybrid2"
    Main file
    > 
    

    Given the above I hope at least you know where you stand, even if it isn’t exactly what you had in mind 🙂

    Update: For a version of hybrid.lua that works in Lua 5.1 and 5.2, you can replace getfenv with debug.getlocal:

    if pcall(debug.getlocal, 4, 1) then
        print("Library")
    else
        print("Main file")
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering how would one get variables inputted in a python script while
How would one write a regular expression to use in Python to split paragraphs?
Is there any python module to convert PDF files into text? I tried one
Let's say I have a basic Python script, test.py : #!/usr/bin/python print Content-type: text/html\n\n
After these instructions in the Python interpreter one gets a window with a plot:
I'd like to include Python scripting in one of my applications, that is written
One of the basic data structures in Python is the dictionary, which allows one
One of the really nice things about python is the simplicity with which you
I installed Python 2.6 for one user on Windows Vista. Python works okay, but
I want to use the macports version of python instead of the one that

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.