Authenticating in Python¶
You can authenticate with the LSEG Data Library for Python directly inline using a function in the package’s session module.
It requires that provide your application key, as well as the username and password you use to access the LSEG web portal. In this example, they will be stored in environment variables to avoid exposing private information in the source code.
import os
import lseg.data as ld
session = ld.session.platform.Definition(
app_key=os.getenv('LSEG_APP_KEY'),
grant=ld.session.platform.GrantPassword(
username=os.getenv('LSEG_USERNAME'),
password=os.getenv('LSEG_PASSWORD')
),
signon_control=True
).get_session()
Once that’s successful, you can open a session:
session.open()
<OpenState.Opened: 'Opened'>
Then it as the default for the library.
ld.session.set_default(session)
Verify the session is open by executing a query for the current price of Thomson Reuters stock. You should back a table of data.
ld.get_history('TRI.N')
| TRI.N | TRDPRC_1 | HIGH_1 | LOW_1 | ACVOL_UNS | OPEN_PRC | BID | ASK | TRNOVR_UNS | VWAP | BLKCOUNT | BLKVOLUM | NUM_MOVES | TRD_STATUS | SALTIM | VWAP_VOL |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Date | |||||||||||||||
| 2025-09-18 | 160.56 | 169.32 | 160.52 | 64044 | 169.32 | 159.87 | 160.82 | 10493940 | 163.8552 | <NA> | <NA> | 1601 | <NA> | 71993 | 64044 |
| 2025-09-19 | 162.52 | 162.79 | 160.28 | 38953 | 161.855 | 162.23 | 163.62 | 6295321 | 161.6132 | <NA> | <NA> | 1013 | <NA> | 71997 | 38953 |
| 2025-09-22 | 163.03 | 163.09 | 161.12 | 27899 | 161.51 | 162.85 | 163.21 | 4517233 | 161.9138 | <NA> | <NA> | 777 | <NA> | 71998 | 27899 |
| 2025-09-23 | 160.79 | 163.02 | 160.44 | 15740 | 162.52 | 160.68 | 161.02 | 2542013 | 161.5002 | <NA> | <NA> | 474 | <NA> | 71998 | 15740 |
| 2025-09-24 | 157.71 | 160.83 | 157.6 | 25138 | 160.83 | 157.58 | 157.92 | 3997779 | 159.0333 | <NA> | <NA> | 599 | <NA> | 71985 | 25138 |
| 2025-09-25 | 158.01 | 159.22 | 157.56 | 13687 | 159.21 | 157.89 | 158.25 | 2165748 | 158.234 | <NA> | <NA> | 452 | <NA> | 71997 | 13684 |
| 2025-09-26 | 156.96 | 157.9 | 156.65 | 17707 | 157.1 | 156.83 | 157.15 | 2783522 | 157.199 | <NA> | <NA> | 460 | <NA> | 71991 | 17707 |
| 2025-09-29 | 156.16 | 157.53 | 155.83 | 16320 | 157.53 | 156.01 | 156.35 | 2554292 | 156.513 | <NA> | <NA> | 443 | <NA> | 71985 | 16320 |
| 2025-09-30 | 155.29 | 157.3 | 155.11 | 27077 | 155.97 | 155.16 | 155.5 | 4220310 | 155.8633 | <NA> | <NA> | 548 | <NA> | 71993 | 27077 |
| 2025-10-01 | 152.61 | 155.27 | 152.61 | 33684 | 154.89 | 152.47 | 152.78 | 5169990 | 153.485 | <NA> | <NA> | 595 | <NA> | 71961 | 33684 |
| 2025-10-02 | 151.46 | 152.21 | 149.93 | 62914 | 152.21 | 151.29 | 151.66 | 9495920 | 150.9349 | <NA> | <NA> | 807 | <NA> | 71998 | 62914 |
| 2025-10-03 | 153.0 | 153.57 | 150.77 | 30037 | 151.04 | 152.76 | 153.16 | 4571567 | 152.1978 | <NA> | <NA> | 656 | <NA> | 71997 | 30037 |
| 2025-10-06 | 151.96 | 152.48 | 151.115 | 20566 | 151.515 | 151.86 | 152.17 | 3121929 | 151.8005 | <NA> | <NA> | 525 | <NA> | 71943 | 20566 |
| 2025-10-07 | 151.53 | 152.21 | 150.58 | 15948 | 151.52 | 151.39 | 152.12 | 2414595 | 151.4043 | <NA> | <NA> | 416 | <NA> | 71999 | 15948 |
| 2025-10-08 | 151.64 | 151.92 | 150.97 | 16321 | 151.21 | 151.54 | 151.89 | 2473209 | 151.5354 | <NA> | <NA> | 423 | <NA> | 71996 | 16321 |
| 2025-10-09 | 150.08 | 150.94 | 149.655 | 13269 | 150.94 | 149.94 | 150.2 | 1992302 | 150.1471 | <NA> | <NA> | 437 | <NA> | 71995 | 13269 |
| 2025-10-10 | 151.325 | 152.07 | 149.55 | 30173 | 150.79 | 151.27 | 151.62 | 4559474 | 151.1111 | <NA> | <NA> | 624 | <NA> | 71990 | 30173 |
| 2025-10-13 | 152.66 | 154.11 | 152.66 | 15623 | 152.9 | 152.4 | 152.75 | 2392604 | 153.1463 | <NA> | <NA> | 456 | <NA> | 71887 | 15623 |
| 2025-10-14 | 154.68 | 155.6 | 153.27 | 35461 | 153.52 | 154.51 | 154.81 | 5483428 | 154.6327 | <NA> | <NA> | 646 | <NA> | 71996 | 35461 |
| 2025-10-15 | 159.34 | 160.61 | 157.845 | 33729 | 157.845 | 159.19 | 159.44 | 5376247 | 159.3954 | <NA> | <NA> | 732 | <NA> | 71986 | 33729 |
The library expects you to close your session when you’re finished. You can do so by running the following code:
ld.close_session()