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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:07:10+00:00 2026-05-23T10:07:10+00:00

I need a way to find the dependencies for each of my Python package’s

  • 0

I need a way to find the dependencies for each of my Python package’s sub-modules at runtime so I can initialize them in a proper order (see my current [EDIT: former] solution here, which doesn’t work to well), so at first I used the standard Python module modulefinder, but that was way too slow (~1-2 seconds per module).

My next choice was to analyze all the globals of each module, and find from those globals which sub-module each sub-module depends upon. (This is my current solution EDIT: I have a better solution now – see my answer). This algorithm is much faster than modulefinder (it takes <200ms per module), but it only works for relative imports, instead of the fully-qualified import style, which is unacceptable.

So, what I need is either:

  • A quicker alternative to modulefinder
  • An alternative algorithm

NOTE: I call my dependency analyzer at the start of each module, like so:

# File my_package/module3.py

import my_package.module1 # Some misc. module
import my_package.module2 # Some other misc. module
import my_package.dependency_analyzer

my_package.dependency_analyzer.gendeps()

(Just in case it helps you any.)

Thank you!

EDIT: I have a solution now – see my answer.

  • 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-23T10:07:10+00:00Added an answer on May 23, 2026 at 10:07 am

    I think I have a solution to my own question 🙂

    Here’s what would go into the dependency_analyzer module talked about above:

    import sys
    from sys import _getframe as getframe
    import atexit
    
    examined_modules = []
    
    def gendeps():
        """Adds the calling module to the initialization queue."""
        # Get the calling module's name, and add it to the intialization queue
        calling_module_name = getframe(1).f_globals['__name__']
        examined_modules.append(calling_module_name)
    
    def init():
        """Initializes all examined modules in the correct order."""
    
        for module in examined_modules:
            module = sys.modules[module]
            if hasattr(module, 'init'):
                module.init()
            if hasattr(module, 'deinit'):
                # So modules get de-initialized in the correct order,
                # as well
                atexit.register(module.deinit)
    

    Now, at the start of each module (after all the import statements – this is crucial), a call to gendeps is placed. This algorithm works because each time a module is imported, that call to gendeps is executed. However, since all of the import statements are placed before the call to gendeps in your own module, the least-dependent modules are placed in the initialization queue first, and the most-dependent modules are placed in the initialization queue last.

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

Sidebar

Related Questions

I need a Java way to find a running Win process from which I
I need a quick way to find out if a given port is open
I need to find a way to crawl one of our company's web applications
I need to find a way to get at the request/response streams inside of
I need to find a way to hide HTML Rows (or Tables) from view
I need to find a way to monitor the status of a list of
I need to find a way to spin off a thread from a static
I need to find a way to call a vb.net function in my aspx
I need to find some way of displaying a drop-down menu that depending on
I need to find a way to be notified when a System.IO.Pipe.NamedPipeServerStream opened in

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.