Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

The only thing worse than X Windows: (X Windows) - X


devel / comp.lang.python / Custom importer and errors

SubjectAuthor
o Custom importer and errorsFabiano Sidler

1
Custom importer and errors

<mailman.105.1713173888.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29864&group=comp.lang.python#29864

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.quux.org!2.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: python@aliases.systohc.net (Fabiano Sidler)
Newsgroups: comp.lang.python
Subject: Custom importer and errors
Date: Sat, 13 Apr 2024 20:49:15 +0200
Lines: 46
Message-ID: <mailman.105.1713173888.3468.python-list@python.org>
References: <0aeaf41e-83a5-4b16-8b1b-117193b7f032@aliases.systohc.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de HMmPdqR8beBibvQaDHdPPQdAuzTyZeAGTa8NokKAD6ZQ==
Cancel-Lock: sha1:EAfWmUqE+c7IhxngjW9EXI0WlPo= sha256:fRGddRm7Tebc05wMA+2hggH0HodB6TAiGcQYQrUag/E=
Return-Path: <python@aliases.systohc.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.127
X-Spam-Level: *
X-Spam-Evidence: '*H*': 0.75; '*S*': 0.01; 'this:': 0.03; 'def': 0.04;
'importerror:': 0.07; 'else:': 0.09; 'from:addr:python': 0.09;
'import': 0.15; "'import": 0.16; '@classmethod': 0.16; 'dots':
0.16; 'folder.': 0.16; 'importing': 0.16; 'location)': 0.16;
'to:addr:python-list': 0.20; 'basically': 0.22; "i'd": 0.24;
'cannot': 0.25; 'error': 0.29; 'header:User-Agent:1': 0.30;
'module': 0.31; 'split': 0.32; "i'm": 0.33; 'path': 0.33;
'package': 0.34; 'received:ch': 0.35; 'errors': 0.36; 'name:':
0.37; "skip:' 10": 0.37; 'class': 0.37; 'directory': 0.37;
'received:192.168': 0.37; 'changes': 0.39; 'break': 0.39; 'ide':
0.40; 'best': 0.61; 'skip:o 10': 0.61; 'come': 0.62; 'skip:m 20':
0.63; 'top': 0.65; 'named': 0.65; 'skip:t 20': 0.66; 'skip:1 20':
0.67; 'right': 0.68; 'skip:/ 30': 0.69; 'content': 0.72;
'8bit%:89': 0.75; '8bit%:92': 0.75; '8bit%:94': 0.75; 'name,':
0.75; 'received:94': 0.76; 'skip:1 30': 0.82; 'lying': 0.84;
'\xc2\xa01': 0.84; 'replacing': 0.91
Authentication-Results: borgis.metanet.ch;
spf=pass (sender IP is 94.16.227.145) smtp.mailfrom=python@aliases.systohc.net
smtp.helo=[192.168.42.202]
Received-SPF: pass (borgis.metanet.ch: connection is authenticated)
User-Agent: Mozilla Thunderbird
Content-Language: en-US
X-Mailman-Approved-At: Mon, 15 Apr 2024 05:38:07 -0400
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <0aeaf41e-83a5-4b16-8b1b-117193b7f032@aliases.systohc.net>
 by: Fabiano Sidler - Sat, 13 Apr 2024 18:49 UTC

Hi folks!

I'd like to split my package tree into several IDE projects and build a
custom
importer to import
    'top.child1.child2'
from the directory
    <python-path-entry>/top.child1.child2/__init__.py
so basically replacing the dots with slashes and having the package content
lying directly in the project folder. I have come up with this:

=== usercustomize.py ===
 1 import sys
 2 from importlib.machinery import ModuleSpec
 3 from pathlib import Path
 4
 5 Loader = type(__spec__.loader)
 6
 7 class IdeHelper:
 8     @classmethod
 9     def find_spec(cls, name, path, target=None):
10         for dirname in sys.path:
11             dirobj = Path(dirname)
12             if dirobj.name == name:
13                 break
14         else:
15             return None
16         origin = str(dirobj.joinpath('__init__.py').absolute())
17         ret = ModuleSpec(name, Loader(name, origin), origin=origin)
18         return ret
19
20 sys.meta_path.append(IdeHelper)

which I'm on the right direction with. Unfortunately, I'm getting errors
while
importing a subpackage. With 'import top.child1' the error is
    ModuleNotFoundError: No module named 'top.child1'; 'top' is not a
package
whereas with 'from top import child1' the error changes to
    ImportError: cannot import name 'child1' from 'top' (unknown location)

How can I make this work?

Best wishes,
Fabiano

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor