# Referencing a session prop from a script library?

**URL:** <https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [March 23, 2020, 1:32pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314 "2020-03-23T13:32:59Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [March 23, 2020, 1:32pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/1 "2020-03-23T13:32:59Z")

</div>

Writing a new script in the script library and I want to change a session property inside of it. There’s no drop down selector to pick a property. How can I reference the session property from a script library? Do I need to pass a reference to self?

---

<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:** [March 23, 2020, 1:35pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/2 "2020-03-23T13:35:07Z")

</div>

You must pass what you need as function arguments. This is true in Perspective, in Vision, and in any gateway events.

---

<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:** [March 23, 2020, 1:40pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/3 "2020-03-23T13:40:45Z")

</div>

Ok I thought session props were analogous to tags, and in Vision I didn’t have to pass anything as long as I knew the tag path I could just do a system.tag.read() on the path.

---

<div class="post-metadata">

**Author:** ![lwood](https://avatars.discourse-cdn.com/v4/letter/l/977dab/32.png) [@lwood](https://forum.inductiveautomation.com/u/lwood)\
**Post date:** [March 23, 2020, 3:32pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/4 "2020-03-23T15:32:11Z")

</div>

So there is nothing to replace Client tags in 8.0? I have almost 100 functions defined in a script library and want to change the database they point to based on the plant selected. I have a session property that does this for everything else but I can’t use it at the beginning of the script library? I would have to pass it into each function separately.

---

<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:** [March 23, 2020, 3:56pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/5 "2020-03-23T15:56:07Z")

</div>

In theory, the `InternalSession.SESSION` [`ThreadLocal`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ThreadLocal.html) would be populated if you’re running a project script from a Perspective thread; so something like this _might_ work:

```python
from com.inductiveautomation.perspective.gateway.session import InternalSession

session = InternalSession.SESSION.get()

if session is not None:
	system.perspective.print(session.custom.something)

```

---

<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:** [March 23, 2020, 4:21pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/6 "2020-03-23T16:21:37Z")

</div>

Hah! And I thought I was bad about exposing unsupported and undocumented implementation details… (:

---

<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:** [March 23, 2020, 4:26pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/7 "2020-03-23T16:26:09Z")

</div>

> [@pturmel](#):
>
> unsupported and undocumented

Definitely true. Phil's suggestion to pass explicit parameters is the only non-magic way to do this. And I don't want any PMs about this thread in three years when we end up breaking someone's code 🙂

---

<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:** [March 23, 2020, 4:31pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/8 "2020-03-23T16:31:35Z")

</div>

Ok so the way to do it would be something like

```auto
def myFunc(self):
    self.session.custom.myCustomProp = 1

```

or to use your undocumented method and then DM you in a few years when it breaks right? 😅

I get how this would work with hardcoded custom session properties I know exists, but say I want to read/write to a dynamic one like

```auto
def myFunc2(self, propertyName):
     #How would I read the session prop
     #How would I write to it?

```

---

<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:** [March 23, 2020, 4:35pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/9 "2020-03-23T16:35:57Z")

</div>

Use Python’s `getattr` builtin; the following examples will both achieve the same result:  
`x = self.session.custom.myCustomProp`  
`x = getattr(self.session.custom, "myCustomProp")`

The corresponding reverse function would be `setattr`:  
`setattr(self.session.custom, "myCustomProp", 1)`

---

<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:** [March 23, 2020, 5:00pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/10 "2020-03-23T17:00:40Z")

</div>

You really should reconsider passing self or session.self to your script libraries, except maybe to helpers to extract specific blocks of Perspective data or to re-encode (json) results for Perspective’s use.

For any script module function, ask yourself if the core logic would be applicable outside Perspective, other than the mechanics of obtaining the arguments and returning a useful result. If so, make it generic, just in case you need to use in in Vision or maybe in WebDev for some future project. Separate the Perspective-specific code from the rest.

---

<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:** [March 23, 2020, 6:57pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/11 "2020-03-23T18:57:01Z")

</div>

I was planning on making getter/setter functions with using undocumented way to try to do that. But, if I want to do it in a fully documented way, and I need to pass the necessary info, and I shouldn’t pass self, how would you recommend doing it? Won’t we at some point necessarily need a self so that we can get self.session etc?

---

<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:** [March 23, 2020, 7:48pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/12 "2020-03-23T19:48:38Z")

</div>

Yes, pass self.session to your Perspective-specific helpers. Pass that result to your generic business/process logic. In simple cases, just do everything needed with self in the event.

---

<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:** [March 23, 2020, 8:35pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/13 "2020-03-23T20:35:23Z")

</div>

Right - invert your assumptions. If you need to do something with session properties, then _only_ assume you have a session object in your project script:

```auto
def doSomething(session):
    session.custom.something = 123

```

And then call it with the shortcut to the session object you get from all perspective components:

```auto
if self.custom.doSomething:
    project.library.doSomething(self.session)

```

Sticking to the `self` nomenclature in project scripts is just making things more confusing than they need to be.

---

<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:** [February 13, 2023, 4:18pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/14 "2023-02-13T16:18:33Z")

</div>

A post was split to a new topic: [Global name 'self' is not defined in script](https://forum.inductiveautomation.com/t/global-name-self-is-not-defined-in-script/69778)

---

<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:** [March 15, 2023, 3:29am UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/15 "2023-03-15T03:29:30Z")

</div>

A long time later...

This line in a project script run from a Perspective component binding:

```auto
from com.inductiveautomation.perspective.gateway.session import InternalSession

```

bombs out saying `ImportError: No module named perspective`

All I want to do in the project script is to check if a user has a role, but without passing in `self`, it seems exceedingly difficult ☹ And I don't really want to have to pass `self` into 7 functions, since I'm only using it to do extra stuff if a user has the `Developer` role.

What about using the `system.util.getContext()` from the Extensions module?

---

<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:** [March 15, 2023, 4:53am UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/16 "2023-03-15T04:53:50Z")

</div>

The problem is the class loaders, I think. On the gateway, each module's classes are independent, so the execution of your script doesn't have access to Perspective internals.

You could, I guess, go from context to the module manager and ask it to resolve the class, which will go through all modules and attempt.

---

<div class="post-metadata">

**Author:** ![Dale\_Wheatley](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@Dale\_Wheatley](https://forum.inductiveautomation.com/u/Dale_Wheatley)\
**Post date:** [July 25, 2024, 1:13pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/17 "2024-07-25T13:13:28Z")

</div>

Hi @paul-griffith I have got a really simple message handler and all I am wanting to do is to retrieve a custom parameter in session props and print it to the logger for the time being. Any chance you know what is happening here. I am assuming it is being returned as null and is stopping the rest of the code from executing. As without the line......  
logger.info(Current)  
the Navigation function is called. i simply just want to check that the correct value is being returned.

```python
def onMessageReceived(self, payload):
	

	Current = getattr(self.session.custom , "CurrentIndex")
	logger = system.util.getLogger('logger')
	logger.info(Current)
   	self.navigateTo(payload['type'], payload['path'], payload['params'])
    
    	return

```

Thanks

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [July 25, 2024, 1:26pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/18 "2024-07-25T13:26:16Z")

</div>

- you don't need to use `getattr` if you know the attribute name.  
Just use `self.session.custom.CurrentIndex`.  
`getattr` is useful if the property you want to get is dynamic, ie:  
`getattr(self.session.custom, payload.get('prop_name'))`
- Even if it was `null`, it wouldn't stop the script. You're not using it anywhere that would cause an exception, and if it did, you'd have that exception in your logs. Logging a `null` will just result in an empty message in your logs.
- The `return` is useless and its indentation is wrong. Just remove it

What exactly is going wrong with your script ? You told us what you'd want to happen, but not what is actually happening.

---

<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:** [July 25, 2024, 1:28pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/19 "2024-07-25T13:28:06Z")

</div>

Well, drop the misindented return statement for one.

The error is probably because logger arguments must be strings. Try `logger.info(str(self.session.custom.CurrentIndex))`  
Note there's no need for `getattr` if you know exactly what property you want at the time you're writing your code.

---

<div class="post-metadata">

**Author:** ![Dale\_Wheatley](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@Dale\_Wheatley](https://forum.inductiveautomation.com/u/Dale_Wheatley)\
**Post date:** [July 25, 2024, 1:54pm UTC](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314/20 "2024-07-25T13:54:18Z")

</div>

Thankyou @paul-griffith, Very frustrating, turns out y value was correct in the first place so I didn't need the logger. Cheers

[Next page](https://forum.inductiveautomation.com/t/referencing-a-session-prop-from-a-script-library/33314.md?page=2)
