Post · 11 min read· pp. 208–219

Logging

PyPSA uses the Python standard library logging module to provide informative messages during execution.

Python Logging Module

You can also control the verbosity of Python's logging messages by setting the logging level to ERROR, WARNING, INFO, or DEBUG. This can be configured independently for different modules.

Let's turn on INFO level messages for the optimization module:

In [2]:

Copied! diagram

ipynb
import pypsa

network = pypsa.examples.ac_dc_meshed()

import pypsa network = pypsa.examples.ac_dc_meshed()

text
/tmp/ipykernel_9494/2940017588.py:3: PyPSA2Warning: pandas reads strings as `str` dtype since 3.0. PyPSA converts them back to object dtype on import, but keeps `str` from PyPSA 2.0 on. Set `pypsa.options.future.string_dtype` to True to opt in now or to False to keep the current behaviour.
  network = pypsa.examples.ac_dc_meshed()
text
INFO:pypsa.network.io:Imported network 'AC-DC-Meshed' has buses, carriers, generators, global_constraints, lines, links, loads

In [3]:

Copied! diagram

ipynb
import logging

pypsa.optimization.optimize.logger.setLevel(logging.INFO)

network.optimize()

import logging pypsa.optimization.optimize.logger.setLevel(logging.INFO) network.optimize()

text
/tmp/ipykernel_9494/1123755349.py:5: PyPSA2Warning: From PyPSA 2.0 on, the objective leaves out the capital costs of existing capacity on extendable assets, which improves LP conditioning. Pass `include_objective_constant` to `n.optimize` to choose per call. Set `pypsa.options.future.exclude_objective_constant` to True to opt in now or to False to keep the current behaviour.
  network.optimize()
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='name')
text
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='name')
text
INFO:linopy.model: Solve problem using Gurobi solver
text
INFO:linopy.model:Solver options:
 - log_to_console: False
text
INFO:linopy.io: Writing time: 0.07s
text
Set parameter WLSAccessID
text
Set parameter WLSSecret
text
Set parameter LicenseID to value 2537914
text
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
text
Read LP format model from file /tmp/linopy-problem-uu16vqrd.lp
text
Reading time = 0.00 seconds
text
obj: 468 rows, 188 columns, 1007 nonzeros
text
Set parameter LogToConsole to value 0
text
INFO:linopy.constants: Optimization successful: 
Status: ok
Termination condition: optimal
Solution: 188 primals, 468 duals
Objective: -3.47e+06
Solver: gurobi
Runtime: 0.00s
Dual bound: -3.47e+06
Solver model: available
Solver message: 2
text
INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-ext-p-lower, Generator-ext-p-upper, Line-ext-s-lower, Line-ext-s-upper, Link-ext-p-lower, Link-ext-p-upper, Kirchhoff-Voltage-Law were not assigned to the network.

Out[3]:

text
('ok', 'optimal')

Now let's reduce verbosity by setting the optimization module to only show WARNING level messages.

In [4]:

Copied! diagram

ipynb
pypsa.optimization.optimize.logger.setLevel(logging.WARNING)

network.optimize()

pypsa.optimization.optimize.logger.setLevel(logging.WARNING) network.optimize()

text
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='name')
text
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='name')
text
INFO:linopy.model: Solve problem using Gurobi solver
text
INFO:linopy.model:Solver options:
 - log_to_console: False
text
INFO:linopy.io: Writing time: 0.06s
text
Set parameter WLSAccessID
text
Set parameter WLSSecret
text
Set parameter LicenseID to value 2537914
text
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
text
Read LP format model from file /tmp/linopy-problem-refsrxez.lp
text
Reading time = 0.00 seconds
text
obj: 468 rows, 188 columns, 1007 nonzeros
text
Set parameter LogToConsole to value 0
text
INFO:linopy.constants: Optimization successful: 
Status: ok
Termination condition: optimal
Solution: 188 primals, 468 duals
Objective: -3.47e+06
Solver: gurobi
Runtime: 0.00s
Dual bound: -3.47e+06
Solver model: available
Solver message: 2

Out[4]:

text
('ok', 'optimal')

Now let's turn on DEBUG level messages for the power flow module to see the most detailed output.

In [5]:

Copied! diagram

ipynb
pypsa.network.power_flow.logger.setLevel(logging.DEBUG)

out = network.lpf()

pypsa.network.power_flow.logger.setLevel(logging.DEBUG) out = network.lpf()

text
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 0 is Manchester
text
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 1 is Norwich DC
text
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 2 is Frankfurt
text
DEBUG:pypsa.network.power_flow:No slack generator found in sub-network 3, using Norway Wind as the slack generator
text
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 3 is Norway
text
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 0 is Manchester
text
INFO:pypsa.network.power_flow:Performing linear load-flow on AC sub-network <pypsa.SubNetwork object at 0x7c66cc116030> for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)
text
/tmp/ipykernel_9494/535319111.py:3: PyPSA2Warning: From PyPSA 2.0 on, `get_switchable_as_dense` returns columns in `inds` order, keeps duplicates and raises a `KeyError` for unknown names. Unique, known names in component order give the same result in both. Set `pypsa.options.future.inds_order` to True to opt in now or to False to keep the current behaviour.
  out = network.lpf()
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 1 is Norwich DC
text
INFO:pypsa.network.power_flow:Performing linear load-flow on DC sub-network <pypsa.SubNetwork object at 0x7c66cc116140> for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)
text
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 2 is Frankfurt
text
INFO:pypsa.network.power_flow:Performing linear load-flow on AC sub-network <pypsa.SubNetwork object at 0x7c66ef83b050> for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)
text
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 3 is Norway
text
INFO:pypsa.network.power_flow:Performing linear load-flow on AC sub-network <pypsa.SubNetwork object at 0x7c66cc10b150> for snapshot(s) DatetimeIndex(['2015-01-01 00:00:00', '2015-01-01 01:00:00',
               '2015-01-01 02:00:00', '2015-01-01 03:00:00',
               '2015-01-01 04:00:00', '2015-01-01 05:00:00',
               '2015-01-01 06:00:00', '2015-01-01 07:00:00',
               '2015-01-01 08:00:00', '2015-01-01 09:00:00'],
              dtype='datetime64[ns]', name='snapshot', freq=None)

Finally, let's turn off messages for the power flow module by setting it back to ERROR level.

In [6]:

Copied! diagram

ipynb
pypsa.network.power_flow.logger.setLevel(logging.ERROR)

out = network.lpf()

pypsa.network.power_flow.logger.setLevel(logging.ERROR) out = network.lpf()

Python Warnings versus Logging

PyPSA also uses Python's warnings next to the logging module:

  • logging: For tracking application behavior - records events and provides debugging information
  • Python Warnings: For developers/users - alerts about deprecated features or potential issues

In [8]:

Copied! diagram

ipynb
# Access a deprecated feature to see a warning
network.components.generators.component_names

Access a deprecated feature to see a warning

network.components.generators.component_names

text
/tmp/ipykernel_9494/1732202595.py:2: DeprecationWarning: c.component_names is deprecated, use c.names instead
  network.components.generators.component_names

Out[8]:

text
Index(['Manchester Wind', 'Manchester Gas', 'Norway Wind', 'Norway Gas',
       'Frankfurt Wind', 'Frankfurt Gas'],
      dtype='object', name='name')

In [9]:

Copied! diagram

ipynb
import warnings

warnings.filterwarnings("ignore", category=DeprecationWarning)
network.components.generators.component_names

import warnings warnings.filterwarnings("ignore", category=DeprecationWarning) network.components.generators.component_names

Out[9]:

text
Index(['Manchester Wind', 'Manchester Gas', 'Norway Wind', 'Norway Gas',
       'Frankfurt Wind', 'Frankfurt Gas'],
      dtype='object', name='name')

Solver Output

The log_to_console parameter controls whether the solver (e.g., HiGHS, Gurobi) prints its progress to the console. By default, pypsa.options.params.optimize.log_to_console is True, but in these documentation examples it's set to False for cleaner output.

Let's see the difference:

In [10]:

Copied! diagram

ipynb
# With log_to_console=False (default in docs): only Python logging messages
network.optimize(log_to_console=False)

With log_to_console=False (default in docs): only Python logging messages

network.optimize(log_to_console=False)

text
WARNING:pypsa.consistency:Encountered nan's in varying data 'p0' for columns ['Norwich Converter', 'Norway Converter', 'Bremen Converter', 'DC link'] of component 'Link'.
text
WARNING:pypsa.consistency:Encountered nan's in varying data 'p1' for columns ['Norwich Converter', 'Norway Converter', 'Bremen Converter', 'DC link'] of component 'Link'.
text
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='name')
text
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='name')
text
INFO:linopy.model: Solve problem using Gurobi solver
text
INFO:linopy.model:Solver options:
 - log_to_console: False
text
INFO:linopy.io: Writing time: 0.06s
text
Set parameter WLSAccessID
text
Set parameter WLSSecret
text
Set parameter LicenseID to value 2537914
text
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
text
Read LP format model from file /tmp/linopy-problem-ufk51mvm.lp
text
Reading time = 0.00 seconds
text
obj: 468 rows, 188 columns, 1007 nonzeros
text
Set parameter LogToConsole to value 0
text
INFO:linopy.constants: Optimization successful: 
Status: ok
Termination condition: optimal
Solution: 188 primals, 468 duals
Objective: -3.47e+06
Solver: gurobi
Runtime: 0.00s
Dual bound: -3.47e+06
Solver model: available
Solver message: 2

Out[10]:

text
('ok', 'optimal')

In [11]:

Copied! diagram

ipynb
# With log_to_console=True (default setting): verbose solver output
network.optimize(log_to_console=True)

With log_to_console=True (default setting): verbose solver output

network.optimize(log_to_console=True)

text
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='name')
text
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='name')
text
INFO:linopy.model: Solve problem using Gurobi solver
text
INFO:linopy.model:Solver options:
 - log_to_console: True
text
INFO:linopy.io: Writing time: 0.06s
text
Set parameter WLSAccessID
text
Set parameter WLSSecret
text
Set parameter LicenseID to value 2537914
text
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
text
Read LP format model from file /tmp/linopy-problem-60k3czwy.lp
text
Reading time = 0.00 seconds
text
obj: 468 rows, 188 columns, 1007 nonzeros
text
Set parameter LogToConsole to value 1
text
Gurobi Optimizer version 13.0.3 build v13.0.3rc0 (linux64 - "Ubuntu 26.04.1 LTS")
text
text
CPU model: AMD EPYC 7763 64-Core Processor, instruction set [SSE2|AVX|AVX2]
text
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
text
text
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
text
Optimize a model with 468 rows, 188 columns and 1007 nonzeros (Min)
text
Model fingerprint: 0xdd26a938
text
Model has 78 linear objective coefficients
text
Coefficient statistics:
text
  Matrix range     [1e-02, 1e+00]
text
  Objective range  [9e-03, 3e+03]
text
  Bounds range     [2e+07, 2e+07]
text
  RHS range        [9e-01, 1e+03]
text
text
Presolve removed 190 rows and 71 columns
text
Presolve time: 0.01s
text
Presolved: 278 rows, 117 columns, 1291 nonzeros
text
text
Iteration    Objective       Primal Inf.    Dual Inf.      Time
text
       0   -2.1204529e+07   2.152701e+04   0.000000e+00      0s
text
     172   -3.4742560e+06   0.000000e+00   0.000000e+00      0s
text
text
Solved in 172 iterations and 0.01 seconds (0.00 work units)
text
Optimal objective -3.474256041e+06
text
INFO:linopy.constants: Optimization successful: 
Status: ok
Termination condition: optimal
Solution: 188 primals, 468 duals
Objective: -3.47e+06
Solver: gurobi
Runtime: 0.01s
Dual bound: -3.47e+06
Solver model: available
Solver message: 2

Out[11]:

text
('ok', 'optimal')

diagram 2026-08-05

diagram 2020-02-28