ronaldoussoren / modulegraph

modulegraph determines a dependency graph between Python modules primarily by bytecode analysis for import statements. modulegraph uses similar methods to modulefinder from the standard library, but uses a more flexible internal representation, has more extensive knowledge of special cases, and is extensible.
MIT No Attribution
38 stars 4 forks source link

Does not work properly from different directory (Python 2.7) #44

Closed ronaldoussoren closed 5 years ago

ronaldoussoren commented 5 years ago

Original report by Jack Millist (Bitbucket: Jack, GitHub: Jack).


Suppose we have this directory structure

parent/
    child/
        a.py
        b.py
        c.py

a imports b and b imports c. Now from parent/ we launch the interactive Python 2.7 shell and execute:

from modulegraph.modulegraph import ModuleGraph as MG
g = MG("Test")
g.run_script("Test/a.py")
g.graphreport()

We get the output

digraph G {
    rankdir="LR";
    concentrate="true";
    "<ModuleGraph>" [label="<ModuleGraph>"];
    "/home/jack/Python/Test/a.py" [shape="record",label="<f0> Script| <f1> /home/jack/Python/Test/a.py"];
    "b" [shape="record",label="<f0> MissingModule| <f1> b"];
    "<ModuleGraph>" -> "/home/jack/Python/Test/a.py" [];
    "/home/jack/Python/Test/a.py" -> "b" [];
}

Notice that modulegraph doesn't "see" the import of c by b. Is this intended behavior? If so, how can I correctly analyze modules in a different directory to the CWD (whether using interactive mode or executing a script)?

ronaldoussoren commented 5 years ago

Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


ModuleGraph by default uses the same search path as the running interpreter (that is, sys.path). You can specify a different path when constructing the ModuleGraph instance, see https://modulegraph.readthedocs.io/en/latest/modulegraph.html.

In your case:

g = MG(path=["Test"] + sys.path)
ronaldoussoren commented 5 years ago

Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


This is not a bug.