How to run this script (attached) in the mailman shell (or withlist)?

https://dpaste.com/39ZMH3M7S (Text below)
The code works perfectly when pasted into the live shell of mailman. But not when I start "(venv) mailman withlist -r set_member_pref".
... module = import_module(module_path) File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked ModuleNotFoundError: No module named 'set_members_prefs'
What is wrong? Please can anyone help? Please can you add information in the documentation how exactly to run a script like that?
# def set_members_prefs(): from zope.component import getUtility from mailman.interfaces.listmanager import IListManager from mailman.interfaces.member import DeliveryMode, DeliveryStatus hide_address = False delivery_status = DeliveryStatus.enabled lm = getUtility(IListManager) for mlist in lm.mailing_lists: print(f"... members in {mlist.fqdn_listname}") for member in mlist.members.members: prefs = member.preferences prefs.acknowledge_posts = True prefs.hide_address = hide_address prefs.preferred_language = 'de' prefs.receive_list_copy = True prefs.receive_own_postings = True prefs.delivery_mode = DeliveryMode.regular prefs.delivery_status = delivery_status print(f"Updated {member.address.email} in list {mlist.fqdn_listname}")

On 8/5/25 13:56, Wikinaut wrote:
https://dpaste.com/39ZMH3M7S (Text below)
The code works perfectly when pasted into the live shell of mailman. But not when I start "(venv) mailman withlist -r set_member_pref".
... module = import_module(module_path) File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked ModuleNotFoundError: No module named 'set_members_prefs'
What is wrong? Please can anyone help?
Please see my reply at https://lists.mailman3.org/archives/list/mailman-users@mailman3.org/message/...
Please can you add information in the documentation how exactly to run a script like that?
See mailman shell --details
-- Mark Sapiro <mark@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan

Okay, the mileage was long. It now works finally. Thank you for your patience.
How to place the script somewhere else ( outside /opt/mailman/venv/bin ), is this possible to have it in /home/maillistgen for example?

On 8/5/25 15:18, Wikinaut wrote:
Okay, the mileage was long. It now works finally. Thank you for your patience.
How to place the script somewhere else ( outside /opt/mailman/venv/bin ), is this possible to have it in /home/maillistgen for example?
It can be anywhere, but it has to be importable by Python which means it
needs to be in a directory somewhere in sys.path. You could put it in
/home/maillistgen if you export PYTHONPATH='/home/maillistgen'
before
running mailman shell
The PYTHONPATH environment variable will add
that directory to sys.path for subsequent python invocations.
-- Mark Sapiro <mark@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan

You wrote:
You could put it in /home/maillistgen if you export PYTHONPATH='/home/maillistgen' before running mailman shell...
However, it looks as if mailman in its (venv) is stubborn against PYTHONPATH=/outside/opt-mailman-home
Look:
I managed to have my set_member_prefs.py in /opt/mailman (home dir of mailman) or below, for example in /opt/mailman/venv/lib , but mailman does not find it when I use a different location outside mm home (and yes, I set the ownership for it to mailman):
sudo su mailman (venv) /opt/mailman> PYTHONPATH=/home/maillistgen mailman withlist -r set_member_prefs ← does not work 4 -rw-r--r-- 1 mailman mailman 833 5. Aug 22:19 /home/maillistgen/set_member_prefs.py (i.e. present)
(venv) /opt/mailman> PYTHONPATH=/opt/mailman mailman withlist -r set_member_prefs ← works 4 -rw-r--r-- 1 mailman mailman 811 6. Aug 00:34 /opt/mailman/set_member_prefs.py (i.e. present)
Can you say something to my observation? Am I correct or wrong?

On 8/5/25 18:43, Wikinaut wrote:
I managed to have my set_member_prefs.py in /opt/mailman (home dir of mailman) or below, for example in /opt/mailman/venv/lib , but mailman does not find it when I use a different location outside mm home (and yes, I set the ownership for it to mailman):
It works for me:
mark@msapiro:~$ ls -l zzz
total 4
-rw-rw-r-- 1 mark mark 35 Aug 5 19:23 xxx.py
mark@msapiro:~$ cat zzz/xxx.py
def demo():
print('I am demo')
mark@msapiro:~$ . /opt/mailman/mm/venv/bin/activate
(venv) mark@msapiro:~$ PYTHONPATH=/home/mark mailman shell -r zzz.xxx.demo
I am demo
-- Mark Sapiro <mark@msapiro.net> The highway is for gamblers, San Francisco Bay Area, California better use your sense - B. Dylan
participants (2)
-
Mark Sapiro
-
Wikinaut