• python3 package import difference?

    From Tobiah@21:1/5 to All on Wed Aug 7 08:35:36 2024
    I have an old library from 20 some years ago
    for use with python2, that is structured like this:

    rcs
    ├── dbi
    │   ├── __init__.py
    │   ├── dbi.py
    │   └── regos.py
    └── __init__.py -- *empty*


    the __init__.py file under 'rcs' is empty.
    The one under rcs.dbi contains:

    from dbi import *
    from regos import *


    With python2, I'd go:

    import rcs.dbi

    then I'd have access to stuff in regos.py
    as:

    rcs.dbi.feature() (Where 'feature' is defined in regos.py)


    When I do the same import with python3, I get:

    Traceback (most recent call last):
    File "/home/toby/me", line 1, in <module>
    import rcs.dbi
    File "/usr/regos-1.0/lib/python/rcs/dbi/__init__.py", line 1, in <module>
    from dbi import *
    ModuleNotFoundError: No module named 'dbi'


    What's changed, and how do I fix it?


    Thanks!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Tobiah on Wed Aug 7 21:00:13 2024
    Tobiah <toby@tobiah.org> wrote or quoted:
    File "/usr/regos-1.0/lib/python/rcs/dbi/__init__.py", line 1, in <module> >from dbi import *
    ModuleNotFoundError: No module named 'dbi'

    The directory of the outermost script executed is in the system path.

    E.g., the directory "d" when you invoke "python d/s.py".

    To see the system path:

    import sys
    print( sys.path )

    . "dbi" should not be in any of those directories in the system path.
    Therefore, it is not found!

    You could use a relative import:

    from .dbi import *

    or manipulate the sys.path (or an "editable install" of dbi might
    also help, but I'm not sure about this as I have not tested it).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Tobiah on Thu Aug 8 11:17:56 2024
    On 07Aug2024 08:35, Tobiah <toby@tobiah.org> wrote:
    When I do the same import with python3, I get:

    Traceback (most recent call last):
    File "/home/toby/me", line 1, in <module>
    import rcs.dbi
    File "/usr/regos-1.0/lib/python/rcs/dbi/__init__.py", line 1, in <module>
    from dbi import *
    ModuleNotFoundError: No module named 'dbi'

    Python 3 imports are absolute (they start from the top of the package
    tree and you have no `dbi` at the top). You want a relative import i.e.:

    from .dbi import *

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gilmeh Serda@21:1/5 to Tobiah on Thu Aug 8 21:55:58 2024
    On Wed, 7 Aug 2024 08:35:36 -0700, Tobiah wrote:

    from dbi import *
    from regos import *

    If I change that to:

    from .dbi import * ¹
    from .regos import * ²

    It seems to be working.

    In my tests, ¹ contains:
    def bar():
    pass

    ² contains:
    def testing():
    pass
    def foo():
    pass

    rcs.dbi.bar()
    rcs.dbi.testing()
    rcs.dbi.foo()


    I guess in a sense Py2 was smarter figuring out what whent where and where
    it came from. Or it was a bad hack that has been removed.

    --
    Gilmeh

    You can always pick up your needle and move to another groove. -- Tim
    Leary

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cameron Simpson@21:1/5 to Gilmeh Serda on Fri Aug 9 09:53:52 2024
    On 08Aug2024 21:55, Gilmeh Serda <gilmeh.serda@nothing.here.invalid> wrote:
    I guess in a sense Py2 was smarter figuring out what whent where and
    where
    it came from. Or it was a bad hack that has been removed.

    No, Python 2 offered less control.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)