On Oct 29, 2019, at 07:54, mer...@gmx.net wrote:
> 
> I noticed some different (strange) behaviour when handling projects like that.

Please quote some text from the message you’re replying to, so we know what 
you’re referencing. It’s very hard to carry on discussions on a mailing list if 
we have to guess which of the four (or, in along thread, forty) posts you might 
be talking about. If your mailing makes it to much of a pain to quote inline 
like I’m doing, you should at least be able to “top-post”, to include part of 
the original message below your reply.

> Imagine following folder structure
> 
> # project_folder
>  # a_submodule
>     * a.py
>     * b.py
>     * __init__.py
> 
>  * main.py
> 
> - content a.py:
> 
> class Foo:
>  pass
> 
> - content b.py:
> 
> from a import Foo
> foo = Foo()
> 
> 
> content main.py:
> 
> from os.path import join as pjoin
> import os
> import sys
> 
> # put a_submodule into path
> curr_dir = os.path.abspath(os.path.dirname(__file__))
> sys.path.append(pjoin(curr_dir, 'a_submodule'))

This is your problem. Never put a package on your sys.path, only put the 
directory containing the package on it.

What happens if you break this rule is exactly what you’re seeing. The way 
Python ensures that doing `import spam` twice results in the same spam module 
object (so your globals don’t all get duplicated) is by storing modules by 
qualified name in sys.modules. So if the same file has two different qualified 
names, it’s two separate modules.

This also means that if you do this:

    import spam
    spam1 = spam
    del sys.modules['spam']
    del spam
    import spam
    assert spam is spam1

… the assert will fail. 

If you actually need this behavior for some reason, you usually want to call 
importlib directly and avoid using the sys.modules cache. But usually you don’t 
want this behavior.

See the docs on the import system 
(https://docs.python.org/3/reference/import.html)for full details on how this 
works. The docs for the importlib module are also helpful.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JUVXRY2ICFCCBLXE3OW4PG4OHRM2X4QL/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to