thebjorn / pydeps

Python Module Dependency graphs
https://pydeps.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
1.8k stars 114 forks source link

[Feature Request] Specify module name instead of filename #199

Open furechan opened 1 year ago

furechan commented 1 year ago

Just been using this a while so it might already be possible but I couldn't find it anywhere ...

I'd like to run pydeps on some of my installed modules by specifying a module name instead of a file/folder name.

I suggest to add a flag like -m or --module to specify a module name instead of a file name. pydeps can use importlib.util.find_spec to locate the module and use this as filename.

Thanks for the great software!

thebjorn commented 1 year ago

Is this just convenience functionality to not need to find the location of a module, or is there something special about modules found with find_spec?

furechan commented 1 year ago

Hi, yes it is mostly a convenience. If you have lots of packages installed, some editable some not editable, depending on which env, etc it can be a lot of work to seek them in the folder tree. Also if you want to automate some sort of reporting, it makes sense to have a central place to call pydeps independently of the location of the various project worspaces. I could create a custom wrapper around pydeps but I thought it'd more practical to have this builtin ... Thank You

Hnasar commented 10 months ago

Passing the filename can cause pydeps to incorrectly resolve imports.

If you have modules like:

a.b
a.c
c

and a/b.py has import c

then passing pydeps a/b.py --show-deps will show that import c is resolved to a/c.py which is incorrect. (If a/b.py were to import a/c.py then it would need to do either import a.c or from . import c OR a/b.py must be run as a script (so that pythona adds a/ to sys.path)

But if we were able to run pydeps -m a.b then pydeps ought to know not to resolve import c to a/c.py.

edit: Oh I guess it resolves differently if a is a namespace package (i.e. if a/__init__.py exists or not)

thebjorn commented 10 months ago

Python seems to think that a/c.py is correct, ie.:

\tmp\pydeps199\src> yamldirs .
.:
  __init__.py: ''
  a:
    __init__.py: ''
    b.py: |
      print('a/b.py')
      import c
    c.py: print('a/c.py')
  c.py: print('/c.py')

Python:

\tmp\pydeps199\src> py -3.12 a\b.py
a/b.py
a/c.py

You might be running into the difference in import-behavior between scripts and imports..?