<p>This is a common Python pattern where packages expose their important classes at a higher level for convenience while maintaining organized internal structure. The actual implementation is in <code>torch.distributions.normal.Normal</code>, but it's <a href="https://github.com/pytorch/pytorch/blob/main/torch/distributions/__init__.py" rel="nofollow noreferrer">registered</a> in <code>torch.distributions.__init__.py</code> to be directly accessible as shown below.</p>
<pre><code>from .normal import Normal

__all__ = [... &quot;Normal&quot;,...]
</code></pre>

CC BY-SA 4.0
2024-12-10T06:41:47.530Z
Thanks! But why use `from .normal import Normal` instead of `from normal import Normal`, since __init__.py and normal.py are in the same directory?
AStudent
CC BY-SA 4.0
2024-12-10T06:48:19.423Z
That is the correct *relative* import to ensure that Python will look for the normal.py module relative to the current package. Yours is *absolute* import from the global Python module namespace which assumes that the normal.py module has been already installed as part of the Python venv or is accessible as a top-level module. Of course the relative one is safer and better as public API. Hope this clarifies which is not AI theory but Python module import programming question.
cinch
CC BY-SA 4.0
2024-12-10T06:49:06.217Z
In addition, since `from .normal import Normal` already gets `Normal`, why do we still need `__all__ = [... "Normal",...]`?
AStudent
CC BY-SA 4.0
2024-12-10T06:51:26.717Z
The ```__all__``` attribute controls what is publicly accessible when someone imports the module or package using import *. This gives you the ability to hide internal implementation details and only expose the necessary classes, functions, or variables that make up the module's "public API". Some programmers might be lazy and just import *, where they can still invoke Normal module.
cinch
CC BY-SA 4.0
2024-12-10T06:58:21.133Z
If you consider my answer is helpful or resolved your lingering confusion, please upvote or accept it. Thank you.
cinch
CC BY-SA 4.0
2024-12-10T07:00:40.013Z
Your answer is really helpful! But I'm a new asker, so I cannot upvote until I have 15 reputations... I don't know why the platform designed this rule.
AStudent
CC BY-SA 4.0
2024-12-10T07:02:19.983Z
Yes I know, but I'm sure you can accept answer by clicking near the answer icon.
cinch
CC BY-SA 4.0
2024-12-10T07:05:58.163Z
Ok! I guess I have done that. Please let me know if I have not successfully done so.
AStudent
CC BY-SA 4.0
2024-12-10T10:47:34.530Z
Programming questions are off-topic here. Please, vote to close them instead of answering them. See https://ai.stackexchange.com/help/on-topic
nbro
CC BY-SA 4.0
2024-12-10T01:10:50.773Z
2024-12-10T01:10:50.773Z
cinch
<p>I only see &quot;torch.distributions.normal.Normal&quot; in the official documents but never see &quot;torch.distributions.Normal&quot;. However, I sometimes see people use &quot;torch.distributions.Normal&quot; in their codes in Github. Could anyone explain if there is any difference between these two usages? Thanks a lot!</p>

CC BY-SA 4.0
2024-12-10T10:47:21.537Z
Programming questions or questions about specific software are off-topic here. See https://ai.stackexchange.com/help/on-topic.
nbro
<pytorch><normal-distribution>
Any Difference between "torch.distributions.normal.Normal" v.s. "torch.distributions.Normal"
AStudent
# Any Difference between "torch.distributions.normal.Normal" v.s. "torch.distributions.Normal"

(Asked by: username_0 on 2024-12-10)

I only see "torch.distributions.normal.Normal" in the official documents but never see "torch.distributions.Normal". However, I sometimes see people use "torch.distributions.Normal" in their codes in Github. Could anyone explain if there is any difference between these two usages? Thanks a lot!

**Tags:** pytorch, normal-distribution

> **Comments:**
> **username_1**: Programming questions or questions about specific software are off-topic here. See https://ai.stackexchange.com/help/on-topic.  

---

## 1 answers

### Answer by username_2 on 2024-12-10. Score: 0 | Accepted answer

This is a common Python pattern where packages expose their important classes at a higher level for convenience while maintaining organized internal structure. The actual implementation is in torch.distributions.normal.Normal, but it's registered in torch.distributions.__init__.py to be directly accessible as shown below.
from .normal import Normal

__all__ = [... "Normal",...]

> **Comments:**
> **username_0**: Thanks! But why use `from .normal import Normal` instead of `from normal import Normal`, since __init__.py and normal.py are in the same directory?  
> **username_2**: That is the correct *relative* import to ensure that Python will look for the normal.py module relative to the current package. Yours is *absolute* import from the global Python module namespace which assumes that the normal.py module has been already installed as part of the Python venv or is accessible as a top-level module. Of course the relative one is safer and better as public API. Hope this clarifies which is not AI theory but Python module import programming question.  
> **username_0**: In addition, since `from .normal import Normal` already gets `Normal`, why do we still need `__all__ = [... "Normal",...]`?  
> **username_2**: The ```__all__``` attribute controls what is publicly accessible when someone imports the module or package using import *. This gives you the ability to hide internal implementation details and only expose the necessary classes, functions, or variables that make up the module's "public API". Some programmers might be lazy and just import *, where they can still invoke Normal module.  
> **username_2**: If you consider my answer is helpful or resolved your lingering confusion, please upvote or accept it. Thank you.  
> **username_0**: Your answer is really helpful! But I'm a new asker, so I cannot upvote until I have 15 reputations... I don't know why the platform designed this rule.  
> **username_2**: Yes I know, but I'm sure you can accept answer by clicking near the answer icon.  
> **username_0**: Ok! I guess I have done that. Please let me know if I have not successfully done so.  
> **username_1**: Programming questions are off-topic here. Please, vote to close them instead of answering them. See https://ai.stackexchange.com/help/on-topic  

---

