Python Forum
finding which source files import a module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finding which source files import a module
#1
i would like to know of a script that can be given the name of a particular module to look for and the path names of a bunch of Python source files (some do not have ".py" at the end), and it will determine while files, when run as Python, could import that module, or from that module, import parts of it. it would not need to check flow of control conditions, though it could be helpful if some of the more obvious are checked, such as in if False: and such. but it does need to handle the variety of ways to code doing an import and parse all the names involved.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Aren't you looking for importlib.util.find_spec()?
Reply
#3
i don't think so. i want to scan through a bunch of python source file to check which ones apparently import (something from) a named module which is one of my modules, usually. i have been using grep but it matches on other lines. i want to match only on the various import lines but in a case of "from foo import bar" and i am looking for a module named "bar" then this is not a match. likewise "import foo as bar" is not a match.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
You may find existing tools for this. There is a module modulefinder in the standard library. There is a module named modulegraph in pypi. There are probably other such tools to look for. See these awesome graphs built with the help of the modulegraph module! The code is here
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  looking fo documentation for module files Skaperen 39 89,824 Aug-09-2022, 07:37 AM
Last Post: Gribouillis
  source of python-wstools has two corrupt .gz files in Ubuntu Linux Skaperen 3 2,233 Jul-13-2021, 12:29 AM
Last Post: Skaperen
  visual studio code python how to get source code of import module definitian umen 2 3,923 Jun-13-2020, 06:20 PM
Last Post: umen
  finding a mismatched triple-quote in a huge module Skaperen 1 1,573 Apr-15-2020, 04:29 AM
Last Post: UGuntupalli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020