# Reading a session variable from a python scipt

**URL:** <https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [September 16, 2020, 6:22pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760 "2020-09-16T18:22:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg.Bogard](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Greg.Bogard](https://forum.inductiveautomation.com/u/Greg.Bogard)\
**Post date:** [September 16, 2020, 6:22pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/1 "2020-09-16T18:22:49Z")

</div>

I am trying to set a local variable in a python library script function with this syntax:

tareWeight = session.custom.intTare  
I get this error:  
excError: (\<type ‘exceptions.NameError’\>, NameError(“global name ‘session’ is not defined”,), \<traceback object at 0x20e\>)

tareWeight = self.session.custom.intTare

I get this error:  
excError: (\<type ‘exceptions.NameError’\>, NameError(“global name ‘self’ is not defined”,), \<traceback object at 0x212\>)

Do I need to pass the session id in as a parameter to the function? Does anybody know how to reference a session custom property in a library script? ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/5/2548f606e02b077cdcf40968824b3c800ad3a2e9.png)

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 16, 2020, 6:31pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/2 "2020-09-16T18:31:04Z")

</div>

Your function doesn’t have a session to operate on unless you pass one in as a parameter. And then any callers will need to actually pass it in, so callers will have to be originating from somewhere in perspective land where you have access to the current session.

---

<div class="post-metadata">

**Author:** ![Greg.Bogard](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Greg.Bogard](https://forum.inductiveautomation.com/u/Greg.Bogard)\
**Post date:** [September 16, 2020, 6:44pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/3 "2020-09-16T18:44:35Z")

</div>

Cool, Any idea the syntax for the function call from a component. In this case it is a btn on a container on a view in Perspective land?

 ![btnPrint calls fct](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/8/b8be9b0a81f8d44832226b2bb31519cee906151f.png)

btn code:

```auto
	# old code
# calculations.fctBuildBc_Label_Inventory(system.tag.read('[default]scales/strProductID').value, system.tag.read('[default]scales/strLotNumber').value)
# I would like to pass the session in here:
# something like :
				# new code
				logger.info('session.id: ' + str(session.id))
				calculations.fctBuildBc_Label_Inventory(system.tag.read('[default]scales/strProductID').value, system.tag.read('[default]scales/strLotNumber').value, session.id)
				

```

and then pull it into function like so:  
def fctBuildBc\_Label\_Inventory(strProductID, strLotNumber, sessionID):

if I use the system.perspective.getSessionInfo(id) to try and get the session id i get an empty list:  
calculations.fctBuildBc\_Label\_Inventory(system.tag.read(’[default]scales/strProductID’).value, system.tag.read(’[default]scales/strLotNumber’).value, system.perspective.getSessionInfo(id))  
shows: sessionID:

---

<div class="post-metadata">

**Author:** ![Greg.Bogard](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Greg.Bogard](https://forum.inductiveautomation.com/u/Greg.Bogard)\
**Post date:** [September 16, 2020, 7:39pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/4 "2020-09-16T19:39:41Z")

</div>

Does not like sesson.props.id either.  
Been looking thru the user manual, just not finding the correct syntax.

logger.info('session.props.id: ’ + str(session.props.id))

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 16, 2020, 8:36pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/5 "2020-09-16T20:36:02Z")

</div>

`getSessionInfo()` only expects up to two arguments, and neither of those is a session id (see [Docs](https://docs.inductiveautomation.com/display/DOC80/system.perspective.getSessionInfo)). Your button code should be called in the following manner:

```auto
# note that you need to reference session as an attribute of the current component by using "self"
logger.info('session.id: ' + str(self.session.id))
calculations.fctBuildBc_Label_Inventory(system.tag.read('[default]scales/strProductID').value, system.tag.read('[default]scales/strLotNumber').value, self.session)

```

Then, your custom function can directly reference the session object:

```auto
def fctBuildBc_Label_Inventory(strProductID, strLotNumber, session):
    tareWeight = session.custom.intTare

```

> [@Greg.Bogard](#):
>
> if I use the system.perspective.getSessionInfo(id) to try and get the session id i get an empty list:

You're getting an empty list because the function is interpreting the passed `id` arg as the `username` to filter on; since there are no users with a username which matches the session's id you are getting no results.

---

<div class="post-metadata">

**Author:** ![Greg.Bogard](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Greg.Bogard](https://forum.inductiveautomation.com/u/Greg.Bogard)\
**Post date:** [September 16, 2020, 8:33pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/6 "2020-09-16T20:33:38Z")

</div>

> [@\[16426\] Incorrect SessionID from system.perspective.getSessionInfo()](https://forum.inductiveautomation.com/t/16426-incorrect-sessionid-from-system-perspective-getsessioninfo/34036/3):
>
> calling `self.sesion.info.id`

Not having any luck capturing the session to pass to a function in a python library, trying to use the:  
logger.info('''self.sesion.info.id : ''' + str(self.sesion.info.id))  
returns nothing, in fact in the code in a button component on a perspective view, it does not show anything and does not throw and error.  
any suggestions?

Tried a few ways to get the session...  
i.e.

# logger.info('system.perspective.getSessionInfo(id): ' + str(system.perspective.getSessionInfo(id)))

# logger.info('session.props.id: ' + str(session.props.id))

# logger.info('session.pageId: ' + str(session.pageId))

# logger.info('session: ' + str(session))

# logger.info('self.session.info(id): ' + str(self.session.info(id)))

# logger.info('''self.session.info('id'): ''' + str(self.session.info('id')))

```
			logger.info('Hello Line 43: ')
			logger.info('''self.sesion.info.id : ''' + str(self.sesion.info.id))

```

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [September 16, 2020, 8:39pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/7 "2020-09-16T20:39:58Z")

</div>

@Greg.Bogard I moved you other post into this thread because both posts are a result of the same incorrect function usage.

---

<div class="post-metadata">

**Author:** ![Greg.Bogard](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Greg.Bogard](https://forum.inductiveautomation.com/u/Greg.Bogard)\
**Post date:** [September 29, 2020, 2:21pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/8 "2020-09-29T14:21:16Z")

</div>

cmallonee, Kevin.Herron, Kevin McCluskey,

Thank you very much for the help with the syntax of sending a session object into a python script library from a Perspective component on a view.

This is how I solved it:

# call to python library function from perspective component on a view - send in self.

`Preformatted text`  
calculations.fctBuildBc\_Label\_Inventory(self)  
`Preformatted text`

# in the function in side of python library. Define the input parameter sID, reference the session variables with sID.session.custom.Variable name:

i.e.  
`Preformatted text`  
def fctBuildBc\_Label\_Inventory(sID):  
`Preformatted text`

```
`Preformatted text

```

`sID.session.custom.strSerialNumber = calluspGetNewSerialNumber.getOutParamValue("strSerialNumber") `Preformatted text`

---

<div class="post-metadata">

**Author:** ![Greg.Bogard](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Greg.Bogard](https://forum.inductiveautomation.com/u/Greg.Bogard)\
**Post date:** [September 29, 2020, 2:23pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/9 "2020-09-29T14:23:41Z")

</div>

Or to read a session variable in side a function in a python library:  
`Preformatted text`  
strBarcode = strBarcode + str(sID.session.custom.strUPCCode)  
`Preformatted text`

---

<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:** [September 29, 2020, 2:30pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/10 "2020-09-29T14:30:30Z")

</div>

Pssst, Greg! Use the code formatting button for pasted code, log files, and other plain text. This one: \</\>

---

<div class="post-metadata">

**Author:** ![Greg.Bogard](https://avatars.discourse-cdn.com/v4/letter/g/76d3ee/32.png) [@Greg.Bogard](https://forum.inductiveautomation.com/u/Greg.Bogard)\
**Post date:** [September 29, 2020, 2:31pm UTC](https://forum.inductiveautomation.com/t/reading-a-session-variable-from-a-python-scipt/38760/11 "2020-09-29T14:31:30Z")

</div>

Will Do, Thanks for the tip!
