SomberNight 83bf760c4a plugins: better handle exceptions in __init__
When importing a plugin, if it raised an exception in its `__init__` file, we
ignored it, and still loaded the plugin, in a potentially half-broken state.
This is because maybe_load_plugin_init_method only calls exec_module_from_spec
if the plugin is not already in sys.modules, but exec_module_from_spec
will put the plugin into sys.modules even if it errors.

Consider this patch to test with, enable the "labels" plugin:
```patch
diff --git a/electrum/plugins/labels/__init__.py b/electrum/plugins/labels/__init__.py
index b68127df8e..0d6d95abce 100644
--- a/electrum/plugins/labels/__init__.py
+++ b/electrum/plugins/labels/__init__.py
@@ -21,3 +21,5 @@ async def pull(self: 'Commands', plugin: 'LabelsPlugin' = None, wallet=None, for
     arg:bool:force:pull all labels
     """
     return await plugin.pull_thread(wallet, force=force)
+
+raise Exception("heyheyhey")

```

I would expect we don't load the labels plugin due to the error, but we do:
```
>>> plugins.get_plugin("labels")
<electrum.plugins.labels.qt.Plugin object at 0x7801df30fb50>
```

Log:
```
$ ./run_electrum -v --testnet -o
  0.75 | I | simple_config.SimpleConfig | electrum directory /home/user/.electrum/testnet
  0.75 | E | p/plugin.Plugins | cannot initialize plugin labels: Error pre-loading electrum.plugins.labels: Exception('heyheyhey')
Traceback (most recent call last):
  File "/home/user/wspace/electrum/electrum/plugin.py", line 148, in exec_module_from_spec
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/user/wspace/electrum/electrum/plugins/labels/__init__.py", line 25, in <module>
    raise Exception("heyheyhey")
Exception: heyheyhey

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/user/wspace/electrum/electrum/plugin.py", line 167, in load_plugins
    self.maybe_load_plugin_init_method(name)
  File "/home/user/wspace/electrum/electrum/plugin.py", line 293, in maybe_load_plugin_init_method
    module = self.exec_module_from_spec(init_spec, base_name)
  File "/home/user/wspace/electrum/electrum/plugin.py", line 150, in exec_module_from_spec
    raise Exception(f"Error pre-loading {path}: {repr(e)}") from e
Exception: Error pre-loading electrum.plugins.labels: Exception('heyheyhey')
  0.75 | D | util.profiler | Plugins.__init__ 0.0030 sec
  0.84 | I | simple_config.SimpleConfig | electrum directory /home/user/.electrum/testnet
  0.89 | I | __main__ | get_default_language: detected default as lang='en_UK'
  0.89 | I | i18n | setting language to 'en_UK'
  0.89 | I | logging | Electrum version: 4.5.8 - https://electrum.org - https://github.com/spesmilo/electrum
  0.89 | I | logging | Python version: 3.10.12 (main, Feb  4 2025, 14:57:36) [GCC 11.4.0]. On platform: Linux-6.8.0-52-generic-x86_64-with-glibc2.35
  0.89 | I | logging | Logging to file: /home/user/.electrum/testnet/logs/electrum_log_20250319T161247Z_6605.log
  0.89 | I | logging | Log filters: verbosity '*', verbosity_shortcuts ''
  0.89 | I | exchange_rate.FxThread | using exchange CoinGecko
  0.90 | D | util.profiler | Daemon.__init__ 0.0047 sec
  0.90 | I | daemon.Daemon | starting taskgroup.
  0.90 | I | daemon.CommandsServer | now running and listening. socktype=unix, addr=/home/user/.electrum/testnet/daemon_rpc_socket
  0.90 | I | p/plugin.Plugins | registering hardware bitbox02: ['hardware', 'bitbox02', 'BitBox02']
  0.90 | I | p/plugin.Plugins | registering hardware coldcard: ['hardware', 'coldcard', 'Coldcard Wallet']
  0.90 | I | p/plugin.Plugins | registering hardware digitalbitbox: ['hardware', 'digitalbitbox', 'Digital Bitbox wallet']
  0.90 | I | p/plugin.Plugins | could not find manifest.json of plugin hw_wallet, skipping...
  0.90 | I | p/plugin.Plugins | registering hardware jade: ['hardware', 'jade', 'Jade wallet']
  0.90 | I | p/plugin.Plugins | registering hardware keepkey: ['hardware', 'keepkey', 'KeepKey wallet']
  0.90 | I | p/plugin.Plugins | registering hardware ledger: ['hardware', 'ledger', 'Ledger wallet']
  0.90 | I | p/plugin.Plugins | registering hardware safe_t: ['hardware', 'safe_t', 'Safe-T mini wallet']
  0.90 | I | p/plugin.Plugins | registering hardware trezor: ['hardware', 'trezor', 'Trezor wallet']
  0.90 | I | p/plugin.Plugins | registering wallet type ('2fa', 'trustedcoin')
  1.01 | I | p/plugin.Plugins | loaded plugin 'labels'. (from thread: 'GUI')
  1.01 | D | util.profiler | Plugins.__init__ 0.1183 sec
```
2025-03-19 16:27:23 +00:00
2022-08-10 17:32:23 +02:00
2023-09-05 12:32:38 +00:00
2024-09-18 15:48:38 +00:00
2024-02-21 16:12:22 +00:00
2024-09-19 15:36:45 +00:00
2023-11-13 15:45:05 +00:00
2024-02-16 15:40:45 +01:00

Electrum - Lightweight Bitcoin client

Licence: MIT Licence
Author: Thomas Voegtlin
Language: Python (>= 3.10)
Homepage: https://electrum.org/

Build Status Test coverage statistics Help translate Electrum online

Getting started

(If you've come here looking to simply run Electrum, you may download it here.)

Electrum itself is pure Python, and so are most of the required dependencies, but not everything. The following sections describe how to run from source, but here is a TL;DR:

$ sudo apt-get install libsecp256k1-dev
$ ELECTRUM_ECC_DONT_COMPILE=1 python3 -m pip install --user ".[gui,crypto]"

Not pure-python dependencies

Qt GUI

If you want to use the Qt interface, install the Qt dependencies:

$ sudo apt-get install python3-pyqt6

libsecp256k1

For elliptic curve operations, libsecp256k1 is a required dependency.

If you "pip install" Electrum, by default libsecp will get compiled locally, as part of the electrum-ecc dependency. This can be opted-out of, by setting the ELECTRUM_ECC_DONT_COMPILE=1 environment variable. For the compilation to work, besides a C compiler, you need at least:

$ sudo apt-get install automake libtool

If you opt out of the compilation, you need to provide libsecp in another way, e.g.:

$ sudo apt-get install libsecp256k1-dev

cryptography

Due to the need for fast symmetric ciphers, cryptography is required. Install from your package manager (or from pip):

$ sudo apt-get install python3-cryptography

hardware-wallet support

If you would like hardware wallet support, see this.

Running from tar.gz

If you downloaded the official package (tar.gz), you can run Electrum from its root directory without installing it on your system; all the pure python dependencies are included in the 'packages' directory. To run Electrum from its root directory, just do:

$ ./run_electrum

You can also install Electrum on your system, by running this command:

$ sudo apt-get install python3-setuptools python3-pip
$ python3 -m pip install --user .

This will download and install the Python dependencies used by Electrum instead of using the 'packages' directory. It will also place an executable named electrum in ~/.local/bin, so make sure that is on your PATH variable.

Development version (git clone)

(For OS-specific instructions, see here for Windows, and for macOS)

Check out the code from GitHub:

$ git clone https://github.com/spesmilo/electrum.git
$ cd electrum
$ git submodule update --init

Run install (this should install dependencies):

$ python3 -m pip install --user -e .

Create translations (optional):

$ sudo apt-get install python3-requests gettext qttools5-dev-tools
$ ./contrib/pull_locale

Finally, to start Electrum:

$ ./run_electrum

Run tests

Run unit tests with pytest:

$ pytest tests -v

To run a single file, specify it directly like this:

$ pytest tests/test_bitcoin.py -v

Creating Binaries

Contributing

Any help testing the software, reporting or fixing bugs, reviewing pull requests and recent changes, writing tests, or helping with outstanding issues is very welcome. Implementing new features, or improving/refactoring the codebase, is of course also welcome, but to avoid wasted effort, especially for larger changes, we encourage discussing these on the issue tracker or IRC first.

Besides GitHub, most communication about Electrum development happens on IRC, in the #electrum channel on Libera Chat. The easiest way to participate on IRC is with the web client, web.libera.chat.

Description
No description provided
Readme 72 MiB
Languages
Python 89%
QML 8.5%
Shell 2.1%
Dockerfile 0.2%
NSIS 0.1%
Other 0.1%