Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] __init__.py is not required in package root. #626

Closed
deathaxe opened this issue Mar 21, 2017 · 4 comments
Closed

[Question] __init__.py is not required in package root. #626

deathaxe opened this issue Mar 21, 2017 · 4 comments
Labels

Comments

@deathaxe
Copy link
Contributor

I have a kind question just for my own interest.

Why do you add an __init__.py to package root? None of the default packages do that and there seems absolutely no drawback when removing it.

It is very fine to use it to "merge" all files in the sub directories to one package, but as far as I know, it doesn't make sense in the package root. It is explicitly loaded by ST just like a normal python file but without any functionality.

The suggestions discussed here don't use it, too, even though all python modules are placed in a src sub directory.

I guess the git_savvy.py is the only needed python file in the root.

@stoivo
Copy link
Member

stoivo commented Mar 22, 2017

This is the commit message
a55603d

Other: convert directories with code to proper packages
Add __init__ module to all directories with python modules.

This is mainly to make the relative improts make sense for my
anaconda/jedi when running goto/autocomplete commands.

I assume this would help us when we write unit tests as well

@deathaxe
Copy link
Contributor Author

Yes this is absolutely, correct with all the sub directories. This makes python see all modules in the directories as they were merged in one file on import.

But I was just wondering, whether it is required for package root, too, as all the files there are explicitly loaded by importlib.import_module() (see sublime_plugin.py). ST3 does not import the package root folder as normal python package. It in fact creates a package for each root folder explicitly, even without __init__.py in it.

A print(__package__) statement in a file located in the package root outputs the directory name (GitSavvy or GitGutter in my case). This works without __init__.py, too.

Same with unit testing. I have the UnitTesting package installed. It is used in GitGutter for the promise module. It works with or without __init__.py.

Your reload module and the Automatic Package Reloader plugin recognize packages without init.py, too.

On the opposite, ST3 explicitly loads init.py as it was a normal module implementing some normal functions.

All those facts make me wonder, what's the sense of this file as it obviously does not have any function or meaning in this special case.

So I assume it is not required and won't add such a file to GitGutter. This was the reason for my question.

Thanks.

@asfaltboy
Copy link
Member

The __init__.py in root is only required if you need to reference the "root" package from sub-packages.

For example in core/git_command.py we do:

from ..common import util

Without the __init__.py file the import would not be possible (outside of sublime) since the import function cannot "descend" to a directory outside of root package (top-most dir with __init__.py).

To clarify what I meant in the commit, the use cases for this structure are for following imports outside of sublime, i.e without it the below would not be possible:

  • Using helper plugins: Anaconda -> Goto Definition
  • Running an external test runner: Run unittest directly from OS (for non-sublime related functionality if such exists)

If that answers your question @deathaxe , please go ahead and close the issue.

@deathaxe
Copy link
Contributor Author

Ok, thanks for your answer. Never took into account, that you use some modules directly in python without ST. So I won't need such a file as ST3 always creates a package from the root directory.

chiphogg pushed a commit to chiphogg/libvtd that referenced this issue Sep 12, 2019
python3 disallows _implicit_ relative imports.  So, I made the test
utility imports _explicit_:

```python
from test import libvtd_test
```

(The `from test ` prefix is new.)

There's another problem: the tests can't access the library code in
python3.  Within this repo, `libvtd` and `test` are _sibling_
packages---but with no common _root_ package.  Thus, the `test` package
can't find the `libvtd` package.

The simplest solution I could think of was to just make the root into a
package, even though there's no python code.  (A similar approach has
been taken by others, e.g., timbrel/GitSavvy#626.)

Now instead of 2 errors, we get 30 errors when running under python3.
But that's progress: the old errors were `ImportError`, and they were
masking all the runtime errors we couldn't even reach before.  So we
probably have something like a true burndown list, at last.

Another way to test this commit is to comment out all of the test bodies
(i.e., everything after the `import` section), and the tests will all
pass under python3 (well, all 0 of them).

Helps #3.
chiphogg pushed a commit to chiphogg/libvtd that referenced this issue Sep 12, 2019
python3 disallows _implicit_ relative imports.  So, I made the test
utility imports _explicit_:

```python
from test import libvtd_test
```

(The `from test ` prefix is new.)

There's another problem: the tests can't access the library code in
python3.  Within this repo, `libvtd` and `test` are _sibling_
packages---but with no common _root_ package.  Thus, the `test` package
can't find the `libvtd` package.

The simplest solution I could think of was to just make the root into a
package, even though there's no python code.  (A similar approach has
been taken by others, e.g., timbrel/GitSavvy#626.)

Now instead of 2 errors, we get 30 errors when running under python3.
But that's progress: the old errors were `ImportError`, and they were
masking all the runtime errors we couldn't even reach before.  So we
probably have something like a true burndown list, at last.

Another way to test this commit is to comment out all of the test bodies
(i.e., everything after the `import` section), and the tests will all
pass under python3 (well, all 0 of them).

Helps #3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants