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.

Removing import in __init__.py

See original GitHub issue

I have a python package with a structure like:

.
├── setup.py
└── mypackage
    ├── __init__.py
    └── mypackage.py

The contents of __init__.py are as follows:

from .mypackage import *

Autoflake is removing all contents from __init__.py breaking the package install. To fix the issue I’m asking autoflake to ignore __init__.py

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
fsouzacommented, Oct 12, 2022

@urtubia thanks for sharing the reproducer. I see what’s going on now. Fix coming soon.

2reactions
urtubiacommented, Oct 12, 2022

Hello! I’m running into a similar issue and I think this unittest shows the problem (I tested by including this test on test_autoflake.py)

  def test_end_to_end_dont_remove_unused_imports_when_not_using_flag(self):
        with temporary_file(
            """\
from . import fake_bar
from . import fake_foo
fake_foo.fake_function()
""",
        ) as filename:
            process = subprocess.Popen(
                AUTOFLAKE_COMMAND
                + [
                    filename,
                ],
                stdout=subprocess.PIPE,
            )
            self.assertEqual(
                "",
                "\n".join(process.communicate()[0].decode().split(os.linesep)[3:]),
            )

I ran this test on different release labels and I noticed that it passed on v1.7.1 and started failing on v1.7.2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why delete modules in __init__.py? - Stack Overflow
It's a way to factor out functionality into submodules without making the submodules part of the public API. A quirk of the import...
Read more >
Don't remove imports from __init__.py · Issue #35 - GitHub
A typical way to set up a project is to have the modules specify __all__ variables and then have the __init__.py from .module_name...
Read more >
Remove an imported module in Python | bobbyhadz
The first step is to delete the sys.modules reference to the module we are trying to unload.
Read more >
Understanding Python imports, __init__.py and pythonpath
The python interpreter tries to look for the directory containing the module we are trying to import in sys.path . It is a...
Read more >
What is __init__.py used for in Python? - Net-Informations.Com
If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will...
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