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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:00:59+00:00 2026-05-28T01:00:59+00:00

I’m trying to get the exact module name that a module was imported with,

  • 0

I’m trying to get the exact module name that a module was imported with, from inside that module. Something like this:

def install():
    print 'Local module name is %s' % __localModuleName__

And when I ran:

import myModule as mm
mm.install()

It would print

Local module name is mm

The script I’m trying to make installs itself into the external application Maya. Maya evaluates the string I pass it on demand in python. Right now, I’m trying to give Maya the string "myModule.run()". In this case, I have to know the exact name of myModule when myModule.install() is first run. __name__ isn’t specific enough if the user imports the module using import myModule as otherModule.

If there’s a better way to do this that doesn’t involve using the exact module name, I’d love to know. Like somehow converting a reference of myModule.run into a string I could store in Maya and later unpack. I’ve been trying to use the inspect module to find this information, but I keep finding situations where it doesn’t work and I have to go back and fiddle with it some more. It also seems kind of messy. This is what I’m using right now, which doesn’t work if myModule is called from __main__.

MODULENAME = None
fr = inspect.currentframe()
try:
    while fr and not MODULENAME:
        if fr.f_globals:
            for name, obj in fr.f_globals.iteritems():
                if hasattr(obj, '__file__') and inspect.ismodule(obj) and obj.__file__ == __file__:
                    MODULENAME = name
        fr = fr.f_back
except:
    pass
finally:
    del fr

If there is no good solution, I plan to remove the above and tell the users that my module can’t be imported using import myModule as ....

  • 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-28T01:00:59+00:00Added an answer on May 28, 2026 at 1:00 am

    An approach close to the one you are trying should work – bt it would be convolutd as you could verify.

    Moreover, the “import module as othername” syntax is just a conveninet way of biinding the module object to another variable.

    It is just the equivalent of

    import module
    othername = module
    del module
    

    And your code should not depend on this for running.
    However, in the global sys.modules dictionary, your module always shows up with its “real” name. Therefore, if you can pass an expression in a string to the host application, you should try to access your module within that dictionary – much less convoluted.

    Instead of passing:
    guessedname.run()

    try passing:

    __import__("sys").modules["<module_name>"].run()
    

    And care not about the variable name that references your module in the running code. (this will even allow the user to do from module import * for your code.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.