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!
import pypsa
network = pypsa.examples.ac_dc_meshed()import pypsa network = pypsa.examples.ac_dc_meshed()
/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()INFO:pypsa.network.io:Imported network 'AC-DC-Meshed' has buses, carriers, generators, global_constraints, lines, links, loadsIn [3]:
Copied!
import logging
pypsa.optimization.optimize.logger.setLevel(logging.INFO)
network.optimize()import logging pypsa.optimization.optimize.logger.setLevel(logging.INFO) network.optimize()
/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')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')INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io: Writing time: 0.07sSet parameter WLSAccessIDSet parameter WLSSecretSet parameter LicenseID to value 2537914Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.deRead LP format model from file /tmp/linopy-problem-uu16vqrd.lpReading time = 0.00 secondsobj: 468 rows, 188 columns, 1007 nonzerosSet parameter LogToConsole to value 0INFO: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: 2INFO: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]:
('ok', 'optimal')Now let's reduce verbosity by setting the optimization module to only show WARNING level messages.
In [4]:
Copied!
pypsa.optimization.optimize.logger.setLevel(logging.WARNING)
network.optimize()pypsa.optimization.optimize.logger.setLevel(logging.WARNING) 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')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')INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io: Writing time: 0.06sSet parameter WLSAccessIDSet parameter WLSSecretSet parameter LicenseID to value 2537914Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.deRead LP format model from file /tmp/linopy-problem-refsrxez.lpReading time = 0.00 secondsobj: 468 rows, 188 columns, 1007 nonzerosSet parameter LogToConsole to value 0INFO: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: 2Out[4]:
('ok', 'optimal')Now let's turn on DEBUG level messages for the power flow module to see the most detailed output.
In [5]:
Copied!
pypsa.network.power_flow.logger.setLevel(logging.DEBUG)
out = network.lpf()pypsa.network.power_flow.logger.setLevel(logging.DEBUG) out = network.lpf()
DEBUG:pypsa.network.power_flow:Slack bus for sub-network 0 is ManchesterDEBUG:pypsa.network.power_flow:Slack bus for sub-network 1 is Norwich DCDEBUG:pypsa.network.power_flow:Slack bus for sub-network 2 is FrankfurtDEBUG:pypsa.network.power_flow:No slack generator found in sub-network 3, using Norway Wind as the slack generatorDEBUG:pypsa.network.power_flow:Slack bus for sub-network 3 is NorwayDEBUG:pypsa.network.power_flow:Slack bus for sub-network 0 is ManchesterINFO: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)/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 DCINFO: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)DEBUG:pypsa.network.power_flow:Slack bus for sub-network 2 is FrankfurtINFO: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)DEBUG:pypsa.network.power_flow:Slack bus for sub-network 3 is NorwayINFO: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!
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!
# Access a deprecated feature to see a warning
network.components.generators.component_namesAccess a deprecated feature to see a warning
network.components.generators.component_names
/tmp/ipykernel_9494/1732202595.py:2: DeprecationWarning: c.component_names is deprecated, use c.names instead
network.components.generators.component_namesOut[8]:
Index(['Manchester Wind', 'Manchester Gas', 'Norway Wind', 'Norway Gas',
'Frankfurt Wind', 'Frankfurt Gas'],
dtype='object', name='name')In [9]:
Copied!
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
network.components.generators.component_namesimport warnings warnings.filterwarnings("ignore", category=DeprecationWarning) network.components.generators.component_names
Out[9]:
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!
# 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)
WARNING:pypsa.consistency:Encountered nan's in varying data 'p0' for columns ['Norwich Converter', 'Norway Converter', 'Bremen Converter', 'DC link'] of component 'Link'.WARNING:pypsa.consistency:Encountered nan's in varying data 'p1' for columns ['Norwich Converter', 'Norway Converter', 'Bremen Converter', 'DC link'] of component 'Link'.WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='name')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')INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: FalseINFO:linopy.io: Writing time: 0.06sSet parameter WLSAccessIDSet parameter WLSSecretSet parameter LicenseID to value 2537914Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.deRead LP format model from file /tmp/linopy-problem-ufk51mvm.lpReading time = 0.00 secondsobj: 468 rows, 188 columns, 1007 nonzerosSet parameter LogToConsole to value 0INFO: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: 2Out[10]:
('ok', 'optimal')In [11]:
Copied!
# 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)
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='name')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')INFO:linopy.model: Solve problem using Gurobi solverINFO:linopy.model:Solver options:
- log_to_console: TrueINFO:linopy.io: Writing time: 0.06sSet parameter WLSAccessIDSet parameter WLSSecretSet parameter LicenseID to value 2537914Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.deRead LP format model from file /tmp/linopy-problem-60k3czwy.lpReading time = 0.00 secondsobj: 468 rows, 188 columns, 1007 nonzerosSet parameter LogToConsole to value 1Gurobi Optimizer version 13.0.3 build v13.0.3rc0 (linux64 - "Ubuntu 26.04.1 LTS")CPU model: AMD EPYC 7763 64-Core Processor, instruction set [SSE2|AVX|AVX2]Thread count: 2 physical cores, 4 logical processors, using up to 4 threadsAcademic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.deOptimize a model with 468 rows, 188 columns and 1007 nonzeros (Min)Model fingerprint: 0xdd26a938Model has 78 linear objective coefficientsCoefficient statistics: Matrix range [1e-02, 1e+00] Objective range [9e-03, 3e+03] Bounds range [2e+07, 2e+07] RHS range [9e-01, 1e+03]Presolve removed 190 rows and 71 columnsPresolve time: 0.01sPresolved: 278 rows, 117 columns, 1291 nonzerosIteration Objective Primal Inf. Dual Inf. Time 0 -2.1204529e+07 2.152701e+04 0.000000e+00 0s 172 -3.4742560e+06 0.000000e+00 0.000000e+00 0sSolved in 172 iterations and 0.01 seconds (0.00 work units)Optimal objective -3.474256041e+06INFO: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: 2Out[11]:
('ok', 'optimal')
2026-08-05
2020-02-28