question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing a single object from a module results in duplicate imports

See original GitHub issue

Hello, I love the project. It is the only one that works well out of the box and with minimal configuration.

Currently, importing a single object from a module results in a duplicate import in the dependency graph:

  • the parent module
  • the child module

E.g.:

# in test_services.py
from telereddit.services.services_wrapper import ServicesWrapper

results in the following dependency graph:

I would expect the graph to not present the services node, because I’m not importing it.

In general, all modules (even if not imported directly) are present and generate duplicate imports arrows. Am I using it wrong, is this intended behavior or is this a bug?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
thebjorncommented, May 1, 2020

@gsakkis thanks for the input.

pydeps is internally using a (modified) version of the standard-library module modulefinder which looks for import opcodes in they Python byte code - ie. it neither looks at runtime (e.g. by trying to import the module and look at sys.modules or install an import-hook) nor the source code. This brings with it some disadvantages (we don’t catch especially dynamic imports) and some advantages (we catch both branches of a python-version switch).

Skipping __init__.py files can have unintended consequences, and I believe the current flow comes from fixing a problem with the collections module being excluded (cf. https://github.com/thebjorn/pydeps/commit/1ab3837be41c87f6af055c56188b5a528c5e587a).

I’m pro both configurability and sensible defaults, but I’m not convinced that this behavior (at least not in a simplistic version) is a sensible default.

Removing modules that do not import anything - and have submodules (perhaps provided the removal doesn’t disconnect the graph) would potentially be an interesting flag…

2reactions
gsakkiscommented, May 4, 2020

@thebjorn an optional flag for the proposed behavior (either on or off by default) would be great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Happens When a Module Is Imported Twice?
A JavaScript module is evaluated just once. When imported multiple times from the same path, the same module instance is returned.
Read more >
no-duplicate-imports - ESLint - Pluggable JavaScript Linter
Rule Details. This rule requires that all imports from a single module that can be merged exist in a single import statement.
Read more >
Javascript imports, do they duplicate? - Stack Overflow
I'm trying to find a javascript workflow solution to bundle my code in multiple files. I have a question I can't find the...
Read more >
Unnecessary Imports: Be careful what you import - Pluralsight
Qt module is actually redundant and only for convenience. As a result, we actually import a lot of bulky and extraneous modules, such...
Read more >
Importing Objects from a Python Module Can Be Risky
Whether you should import a module (import module) or import objects from a module (from module import obj1, obj2) is not a new...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found