# Browse the list of variables initialized on a Project Library

**URL:** <https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157>\
**Category:** Ignition\
**Created:** [August 21, 2023, 2:14pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157 "2023-08-21T14:14:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastien\_ng](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sebastien_ng/32/55297_2.png) [@sebastien\_ng](https://forum.inductiveautomation.com/u/sebastien_ng)\
**Post date:** [August 21, 2023, 2:14pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/1 "2023-08-21T14:14:03Z")

</div>

Hello, is it possible to make a for loop to browse the list of variables initialized on a Project Library file, see the example below ?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/1/c15767752ae987cf39b4619d7bc5f75eb3041d3e.png)

I want do something like:  
for each items in A\_Constante ....  
I want read each value of constant declared

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 21, 2023, 2:23pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/2 "2023-08-21T14:23:11Z")

</div>

Try `dir(VMS.A_Constante)`

Then, in your loop, use `getattr(VMS.A_Constante, someNameVar)`

Note that it will show every name in the module, including system-defined names.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 21, 2023, 2:24pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/3 "2023-08-21T14:24:47Z")

</div>

Why not simply declare your constants in a dictionary?  
Easy looping and easy key retrieval.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 21, 2023, 2:28pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/4 "2023-08-21T14:28:02Z")

</div>

> [@paul-griffith](#):
>
> simply declare your constants in a dictionary?

Blegh. Item retrieval is awfully ugly compared to attribute retrieval.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [August 21, 2023, 2:35pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/5 "2023-08-21T14:35:03Z")

</div>

Yea but if he's going through the whole list he wouldn't be using `['someKey']` notation but hopefully rather `for key, value in myConstants.items(): # Do stuff`. I don't know I use constant dictionaries a decent amount, I'll concede it's slightly ugly but the ability to subnest dictionaries when needed is nice.

---

<div class="post-metadata">

**Author:** ![sebastien\_ng](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sebastien_ng/32/55297_2.png) [@sebastien\_ng](https://forum.inductiveautomation.com/u/sebastien_ng)\
**Post date:** [August 21, 2023, 2:35pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/6 "2023-08-21T14:35:58Z")

</div>

Thank you for your answer.  
I had created a list. I put in the list of each constant and, it worked but on our project we are recommended to use the notation as in the attached image above. but I don't know it is the best solution.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 21, 2023, 3:07pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/7 "2023-08-21T15:07:17Z")

</div>

You could use a simple class to achieve a 'namespace', and then some Python sugar to make it directly iterable as well:

```python
class Constants(object):
    CONSTANT_1 = 1
    CONSTANT_2 = "hello"
    CONSTANT_3 = 3.14

    @classmethod
    def __iter__ (cls):
        return (getattr(cls, attr) for attr in dir(cls) if attr.isupper())

```

```auto
for constant in Constants:
    print(constant)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 21, 2023, 3:11pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/8 "2023-08-21T15:11:11Z")

</div>

And then you risk lifetime problems if you use one of those constants in a persistent place. `dir()` and `getattr()` are perfectly reasonable ways to access constants.

---

<div class="post-metadata">

**Author:** ![sebastien\_ng](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sebastien_ng/32/55297_2.png) [@sebastien\_ng](https://forum.inductiveautomation.com/u/sebastien_ng)\
**Post date:** [August 21, 2023, 3:17pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/10 "2023-08-21T15:17:31Z")

</div>

I try it but I obtained many thinks that I don't understand where it cam from  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/7/271dd6770de9c11c8e02122f7becee309a9ae2b8.png)

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/f/ff1c187de907cf267e14c9cc4432badc4ffeba3a.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 21, 2023, 3:30pm UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/11 "2023-08-21T15:30:59Z")

</div>

What @pturmel is suggesting is something more like this:

```auto
return [
     getattr(VMS.A_Constante,attr)
     for attr in dir(VMS.A_Constante)
     if 'cs_' in attr
]

```

This would return a list of all attributes that contain 'cs\_' in the name.

You could also do this:

```auto
return {
        attr:getattr(VMS.A_Constante,attr)
        for attr in dir(VMS.A_Constante)
        if 'cs_' in attr
        }

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 22, 2023, 9:43am UTC](https://forum.inductiveautomation.com/t/browse-the-list-of-variables-initialized-on-a-project-library/78157/12 "2023-08-22T09:43:03Z")

</div>

Probably want to use startswith('cs\_') instead in case there are other matches
