On Oct 28, 2019, at 04:44, Richard Vogel <mer...@gmx.net> wrote:
>
> Current state:
> Python will search for the first TOP-LEVEL hit when resolving an import
> statement and search inside there for the remainder part of the import. If it
> cannot find the symbols it will fail. (Tested on Python 3.8)
> Proposed Change:
>
> If the import fails at some point after finding the first level match: The
> path is evaluated further until it eventually may be able to resolve the
> statement completely-
> --> Fail later
You might be looking for namespace packages
(https://packaging.python.org/guides/packaging-namespace-packages/).
You create a myutils namespace package at spamlib/libs/myutils, that Includes a
module named spam.py (it also works for subpackages, extension modules, etc.,
but let’s keep it simple), and put spamlib/libs on your sys.path.
You create a second myutils namespace package at eggslib/lib/myutils, that
includes a module named eggs.py, and put eggslib/lib on your sys.path.
Now, `from myutils import spam` looks in the first `myutils` directory and
finds `spam.py` and imports it. And `from myutils import eggs` looks in the
first `myutils` directory and sees that there’s no `eggs.py`—but, because it’s
a namespace package, it looks for other package directories names `myutils` to
add to the namespace, and it finds your second directory and imports the
`eggs.py` there.
I’m not sure this is what you want, and I’m not sure it’s a good idea for your
code, but if it _is_ what you want, all you have to do to make this work is not
create a myutils/__init__.py in either directory. (It you need an __init__.py
but also need namespace behavior, you can do that with pkg_resources, but you
probably don’t want to—especially since your whole goal seems to be avoiding
installing packages.)
> My use case scenario:
>
> I have a bunch of different projects built using Python
> I want to use parts of it within a new project
> I place them withina sub-folder (for example libs) within the new project
> using git submodule or just copy / link them there, whatever
> I append the libs to path (sys.path.append)
> Python WILL find the packages and basically import everything right
> Problem:
> if themain package does actually contain a toplevel folder that is named the
> same like one within the other modules (for example a "ui" submodule) python
> will search withon one and only one of these ui modules within exactly one
> project
> Name clashes can only be avoided by renaming
> I know that this is propably not the suggested and best way to reuse existing
> code. But its the most straight-forward and keeps the fastest development
> cycle, which is I think is a reason Python grew so fast.
>
I don’t think it does keep the fastest development cycle. People who haven’t
learned how to use virtual environments, requirements.txt, and a trivial
setup.py usually think this is a whole lot of work that would get in their way.
But it isn’t. There’s a small learning curve, but once you get there, it
actually means only a tiny bit of work up front that saves you a lot more work
in the long run. Especially because working in-place means you keep running
into new problems that have already been solved, but haven’t been solved for
your use case (adding a third-party dependency to a submodule without having to
go edit every one of your top-level projects, needing a build step for one of
your submodules, trying to package the whole thing up for distribution as a
PyPI package or a company-internal package or a .exe or .app binary…).
But I don’t think this is directly relevant to your problem or your
suggestion—e.g., if you actually are looking for namespace packages, they’re
just as occasionally-necessary-and-incredibly-handy-when-that-happens for
installed packages as for in-place submodules.
_______________________________________________
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/L7XTZ3WYQZ5F65ZPOBS4MQRLSW7LBDST/
Code of Conduct: http://python.org/psf/codeofconduct/