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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:22:04+00:00 2026-06-16T23:22:04+00:00

Adding to the list for rake and PHP : Is there a way to

  • 0

Adding to the list for rake and PHP: Is there a way to test whether a Python function or method has been invoked directly from a Python shell, as opposed to being invoked from within a .py script file?

For example I want to define an expression, test_expr that behaves as follows when the expression appears in a module “shelltest.py”,

#!/usr/bin/python
"""Module shelltest.py"""

def test_expr():
    #...

Case (1): it yields True when invoked directly from a shell

>>> import shelltest
>>> shelltest.test_expr()
True

Case (2): it yields False when imported into another module, “other.py” and used in code there

#!/usr/bin/python
"""Module other.py"""

import shelltest

def other_func():
    # ...
    shelltest.test_expr()

which is in turn invoked from a shell

>>> import other
>>> other.other_func()
False
  • 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-16T23:22:05+00:00Added an answer on June 16, 2026 at 11:22 pm

    If you are at the shell, then __name__ == '__main__'. (In addition, as Ned Batchelder notes, this will only tell you where the function was defined.)

    You probably don’t want to test this inside a function – it’s used instead to distinguish whether a module is being called as a main programme or not. Your functions should probably work the same way regardless, and if you need different functions, you should import a different module containing the same function names.

    Do something like this:

    if __name__ == '__main__':
       import formain as bumpf
    else:
       import forscripts as bumpf
    
    bumpf.domagic()
    

    As to determining whether you’re in a web environment – do that in code that will only be called from the web. Python scripts are typically not invoked by CGI, so this doesn’t really arise as a use case.

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

Sidebar

Related Questions

Is there a standard way of adding a list of values to an HTML5
I want to loop through the list adding commas between items. There has to
I have been able to find lots of examples of adding a Dropdown list
Python not support adding a tuple to a list: >>> [1,2,3] + (4,5,6) Traceback
I have been successful in adding list items of <li> to a unordered list
say I have a list like this {1,2,3,4,5,6,7,8}. Is there a fast way to
In my Phonegap android application dynamically adding list items to jqm listview where on
I have problem adding arraylist to list view, will explain about my problem here..
i have two list, i wanted to update the second list while adding a
Hi Programmers! Actually i need the distinct values from the List after adding some

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.