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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:28:01+00:00 2026-05-13T23:28:01+00:00

from pack.mod import f How to get from object f information about import –

  • 0
from pack.mod import f

How to get from object f information about import – 'pack.mod'

I can get it using f.__module__
but if function def in module where i get this attribute (f.__module__) it return '__main__'. But i need real path here – 'pack.mod'

I found this way to get this information:

inspect.getmodule(f).__file__

then i can sub start path from sys.path, replace / on . and get path like – 'pack.mod'
But may be exist some more convenient way?

  • 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-13T23:28:02+00:00Added an answer on May 13, 2026 at 11:28 pm

    What inspect.getmodule(f) does internally, per inspect.py’s sources, is essentially sys.modules.get(object.__module__) — I wouldn’t call using that code directly “more convenient”, though (beyond the “essentially” part, inspect has a lot of useful catching and correction of corner cases).

    Why not call directly inspect.getsourcefile(f)?

    Edit: reading between the lines it seems the OP is trying to do something like

    python /foo/bar/baz/bla.py
    

    and within bla.py (which is thus being run as __main__) determine “what from or import statement could a different main script use to import this function from within me?”.

    Problem is, the question is ill-posed, because there might not be any such path usable for the purpose (nothing guarantees the current main script’s path is on sys.path when that different main script gets run later), there might be several different ones (e.g. both /foo/bar and /foo/bar/baz might be on sys.path and /foo/bar/baz/__init__.py exist, in which case from baz.bla import f and from bla import f might both work), and nothing guarantees that some other, previous sys.path item might not “preempt” the import attempt (e.g., say /foo/bar/baz is on sys.path, but before it there’s also /fee/fie/foo, and a completely unrelated file /fee/fie/foo/bla.py also exists — etc, etc).

    Whatever the purpose of this kind of discovery attempt, I suggest finding an alternative architecture — e.g., one where from baz.bla import f is actually executed (as the OP says at the start of the question), so that f.__module__ is correctly set to baz.bla.

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

Sidebar

Related Questions

from tkinter import * app=Tk() app.title( BRAIN SYNCRONIZATION SOFTWARE ) e1=Entry(app).pack() t1=Text(app).pack() def InputFun():
I'm using some custom tasks from MSBuild Extension Pack (MEP). My projects are splitted
from Tkinter import * app = Tk() text_field = Entry(app) text_field.pack() app.mainloop() I want
i am try to get all links from webpage with Html Agility Pack, after
From this context: import itertools lines = itertools.cycle(open('filename')) I'm wondering how I can implement
Why html agility pack is used to parse the information from the html file
I am using the winform datarepeater control from vb.net power pack. All of the
I have this Python code to do this: from struct import pack as _pack
I've noticed that applications developed using MFC Feature Pack (VS2008), when restored from minimized
from Tkinter import * window = Tk() frame=Frame(window) frame.pack() text_area = Text(frame) text_area.pack() text1

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.