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

The Archive Base Latest Questions

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

I have a module in python, and based on the script that has called

  • 0

I have a module in python, and based on the script that has called a function in it, I want to take a decision inside that module.

So if we have 2 file file1.py and file2.py, both import the module testmod and call a function in it. Inside the module testmod, I want to know which script has called it ? file1.py or file2.py.

I want to write a code like below in testmod
if then
do this
else if then
do that
else
do something else !

  • 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-12T23:04:55+00:00Added an answer on June 12, 2026 at 11:04 pm

    As already stated in the comments you can avoid this (since it is bad design and complicates things a lot) adding a parameter to that function. Or you could write two versions of this function if the code inside is much different from time to time.

    Anyway, if you want to know from where your function got called you need the inspect module. I’m not an expert at it, but I don’t think it’s too hard to obtain the stack frame that called the function and from there understand which script called it.

    Update:

    If you really want to use inspect and do the ugly thing, here’s a minimal working example:

    #file a.py
    
    import inspect
    def my_func():
        dad_name = inspect.stack()[1][1]
        if inspect.getmodulename(dad_name) == 'b':   #or whatever check on the filename
             print 'You are module b!'
        elif inspect.getmodulename(dad_name) == 'c':
             print 'You are module c!'
        else:
             print 'You are not b nor c!'
    
    #file b.py
    import a
    
    a.my_func()
    
    #file c.py
    
    import a
    a.my_func()
    
    #file d.py
    import a
    a.my_func()
    

    Output:

    $ python b.py
    You are module b!
    $ python c.py
    You are module c!
    $ python d.py
    You are not b nor c!
    

    If you want to add a parameter to the function:

    #file a.py
    def my_func(whichmod=None):
        if whichmod == 'b':
             print 'You are module b!'
        elif whichmod == 'c':
             print 'You are module c!'
        else:
             print 'You are not B nor C!'
    
    #files b.py/c.py
    import a
    a.my_func(whichmod='b')   # or 'c' in module c
    
    #file d.py
    import a
    a.my_func()
    

    The output is the same.

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

Sidebar

Related Questions

I have a simple Python script that uses the socket module to send a
I have a python script that parses an XML file that contains part information
I have a python based tkinter script which executes some commands using subprocess module.
I have a python script that calls a USB-based data-acquisition C# dotnet executable. The
I have a python script that calls a USB-based data-acquisition C# dotnet executable. The
I have a python script that works great when run by itself. Based on
I have a Python script that downloads a GRIB file (weather forecast data) from
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
I have created a simple Python module and want to distribute it with pip.

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.