# Error on sys.modules?

**URL:** <https://forum.inductiveautomation.com/t/error-on-sys-modules/6431>\
**Category:** Ignition\
**Created:** [May 9, 2013, 4:51pm UTC](https://forum.inductiveautomation.com/t/error-on-sys-modules/6431 "2013-05-09T16:51:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![agueorguiev](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@agueorguiev](https://forum.inductiveautomation.com/u/agueorguiev)\
**Post date:** [May 9, 2013, 4:51pm UTC](https://forum.inductiveautomation.com/t/error-on-sys-modules/6431/1 "2013-05-09T16:51:41Z")

</div>

I’ve been trying to use the inspect module in a script, however, upon using “inspect.stack()” I would get this error:

[code]Traceback (most recent call last):  
File “event:actionPerformed”, line 2, in   
File “C:\Documents and Settings\andrey.gueorguiev\Application Data\Sun\Java\Deployment\cache\FPMI\script\_lib2\Lib\inspect.py”, line 901, in stack  
return getouterframes(sys.\_getframe(1), context)  
File “C:\Documents and Settings\andrey.gueorguiev\Application Data\Sun\Java\Deployment\cache\FPMI\script\_lib2\Lib\inspect.py”, line 882, in getouterframes  
framelist.append((frame,) + getframeinfo(frame, context))  
File “C:\Documents and Settings\andrey.gueorguiev\Application Data\Sun\Java\Deployment\cache\FPMI\script\_lib2\Lib\inspect.py”, line 853, in getframeinfo  
filename = getsourcefile(frame) or getfile(frame)  
File “C:\Documents and Settings\andrey.gueorguiev\Application Data\Sun\Java\Deployment\cache\FPMI\script\_lib2\Lib\inspect.py”, line 406, in getsourcefile  
if hasattr(getmodule(object, filename), ‘ **loader** ’):  
File “C:\Documents and Settings\andrey.gueorguiev\Application Data\Sun\Java\Deployment\cache\FPMI\script\_lib2\Lib\inspect.py”, line 453, in getmodule  
main = sys.modules[‘ **main** ’]  
KeyError: ‘ **main** ’

Ignition v7.5.6 (b1317)  
Java: Sun Microsystems Inc. 1.6.0\_38[/code]

The error seems to be coming from sys.modules, because “ **main** ” is not a part of it.  
Is there any way I can get around this error, or am I just doing something wrong?

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [May 22, 2013, 6:18pm UTC](https://forum.inductiveautomation.com/t/error-on-sys-modules/6431/2 "2013-05-22T18:18:44Z")

</div>

Honestly I’m not familiar with the inspect module. I’m guessing it is incompatible with the way we use scripting.

---

<div class="post-metadata">

**Author:** ![agueorguiev](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@agueorguiev](https://forum.inductiveautomation.com/u/agueorguiev)\
**Post date:** [June 7, 2013, 9:18pm UTC](https://forum.inductiveautomation.com/t/error-on-sys-modules/6431/3 "2013-06-07T21:18:23Z")

</div>

Fixed by adding this before the calling function:

```auto
class VirtualModule(object):
	def __init__ (self,name):
		import sys
		sys.modules[name]=self
	def __getattr__ (self,name):
		return globals()[name]
VirtualModule(" __main__")
```

---

<div class="post-metadata">

**Author:** ![tyler.bennett](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tyler.bennett](https://forum.inductiveautomation.com/u/tyler.bennett)\
**Post date:** [March 15, 2024, 8:33pm UTC](https://forum.inductiveautomation.com/t/error-on-sys-modules/6431/4 "2024-03-15T20:33:22Z")

</div>

> [@agueorguiev](#):
>
> Fixed by adding this before the calling function:
> 
> ```auto
> class VirtualModule(object):
> def __init__ (self,name):
> import sys
> sys.modules[name]=self
> def __getattr__ (self,name):
> return globals()[name]
> VirtualModule(" __main__")
> 
> ```

Well that response came in handy for me some 11 years later with Ignition v8.1.31.  
I was interested in using the inspect module as a way to determine the name of the current function and/or the calling function for debug log entries..

Code [like this](https://www.stefaanlippens.net/python_inspect/) would work in Script Console however any use of `inspect.stack()` in a Gateway Script. would cause the `KeyError: ' __main__' ` exception.

```python
import inspect
def whoami():
   return inspect.stack()[1][3]
def whosdaddy():
   return inspect.stack()[2][3]

```

Adding the VirtualModule makes `inspect.stack()` behave better.  
I was also able to meet my need by avoiding `inspect.stack()` using this instead, without the Virtual Module.

```python
def whoami():
	return inspect.currentframe().f_back.f_code.co_name
def whosdaddy():
	return inspect.currentframe().f_back.f_back.f_code.co_name

```
