# Unable to use Steps, generated with Python Plugin in example from \`TapApiFromPython\`

**URL:** <https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504>\
**Category:** Technical\
**Tags:** python\
**Created:** [November 2, 2021, 4:51pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504 "2021-11-02T16:51:41Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![geomags123](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@geomags123](https://forum.opentap.io/u/geomags123)\
**Post date:** [November 2, 2021, 4:51pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/1 "2021-11-02T16:51:41Z")

</div>

I was trying to run the example from “TapApiFromPython/Misc Examples/DynamicallyGenerateTestPlan.py”, which I reworked to support python3.  
It works fine with built-in BasicSteps but I can’t add my steps, generated by Python Plugin.  
(I can use my steps in TUI)

I always get “System.NullReferenceException: Object reference not set to an instance of an object.”.

example of code:

```csharp
import sys
import clr
import os
from pathlib import Path

tapPath = Path(os.environ["TAP_PATH"])
basic_steps_tapPath = tapPath / "Packages" / "OpenTAP"
my_steps_tapPath = tapPath / "Packages" / "Python" / "my_tap"

sys.path.append(str(tapPath))
sys.path.append(str(basic_steps_tapPath))
sys.path.append(str(my_steps_tapPath))

clr.AddReference("OpenTap")
clr.AddReference('OpenTap.Plugins.BasicSteps')
clr.AddReference('Keysight.OpenTap.Plugins.Python')
clr.AddReference('Python.my_tap')

import OpenTap
import OpenTap.Plugins.BasicSteps as BasicSteps
import Keysight.OpenTap.Plugins.Python as PythonPlugin
import Python.my_tap as MySteps

# Required to find plugins
print("Test Plan Started.")
OpenTap.PluginManager.Search()

OpenTap.SessionLogs.Initialize("generatedPlan_log.txt")

myTestPlan = OpenTap.TestPlan()
mySequenceStep = BasicSteps.SequenceStep()

# myTestPlan.Name = "MyTestplan"
# TestPlan.Name doesn't have a setter anymore

myDelayStep1 = BasicSteps.DelayStep()
myDelayStep1.DelaySecs = 2.5
myDelayStep1.Name = "Delay1"

myDelayStep2 = BasicSteps.DelayStep()
myDelayStep2.DelaySecs = 1.1
myDelayStep2.Name = "Delay2"

myStep = MySteps.RegularStep() # OUTPUT: System.NullReferenceException: Object reference not set to an instance of an object.
myStep.Name = "Regular Step 1"

mySequenceStep.ChildTestSteps.Add(myDelayStep1)
mySequenceStep.ChildTestSteps.Add(myDelayStep2)
mySequenceStep.ChildTestSteps.Add(myStep)
myTestPlan.ChildTestSteps.Add(mySequenceStep)

myTestPlan.Save("MyTestPlan.TapPlan")

myTestPlan.Execute()

print(f"Test Plan Ended.")

```

---

<div class="post-metadata">

**Author:** ![brennen\_direnzo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opentap.io/brennen_direnzo/32/6_2.png) [@brennen\_direnzo](https://forum.opentap.io/u/brennen_direnzo)\
**Post date:** [November 3, 2021, 4:32pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/2 "2021-11-03T16:32:54Z")

</div>

@geomags123 Welcome to the community!

Which version of the Python Plugin are you using?

I think it is easiest if you have this script in the same directory as your custom steps, but @rolf_madsen, @ivan.diep , or @arvind.sundararajan may have a better idea.

---

<div class="post-metadata">

**Author:** ![geomags123](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@geomags123](https://forum.opentap.io/u/geomags123)\
**Post date:** [November 5, 2021, 10:54am UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/3 "2021-11-05T10:54:01Z")

</div>

@brennen_direnzo I have Python Plugin version 2.2.0.

But the issue is not that I can’t open `Python.my_tap.dll`. I can import it and even print the type of steps I created.

```csharp
print(MySteps.MyStep)
# OUTPUT: <class 'Python.my_tap.MyStep'>

```

```csharp
print(type(MySteps.MyStep))
# OUTPUT: <class 'CLR.CLR Metatype'>

```

but when I try to create an object of this step I get the error:

```csharp
my_step = MySteps.MyStep()
# System.NullReferenceException: Object reference not set to an instance of an object.
# at Keysight.OpenTap.Plugins.Python.PythonStepWrapper.load(String name, String moduleName)

```

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [November 8, 2021, 3:39pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/4 "2021-11-08T15:39:17Z")

</div>

Are you trying to create a TestPlan in code? Just wondering why you’re trying to _instantiate_ a TestStep, I don’t think it’s the usual use model. Usually you’d create your TestStep classes in code, and then use those to create a TestPlan in the Editor. My point is - you may be trying something in Python that no one has tried before, not sure. Perhaps there’s some functionality missing.

Also, why try to use the compiled version? I think also the intention with Python is to use the Python classes and then “transpile” them. So your Python would reference your Python code as opposed to your compiled dll. I don’t disagree that you _should_ be able to do it the way you’re trying to do it, I just think you’re running up against the use model here and may be why you’re having trouble. But like I said, that may also explain why there’s functionality missing or a bug, if that’s the case.

---

<div class="post-metadata">

**Author:** ![geomags123](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@geomags123](https://forum.opentap.io/u/geomags123)\
**Post date:** [November 8, 2021, 5:20pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/5 "2021-11-08T17:20:35Z")

</div>

@john.berlien thank you for the response 🙂  
Basically, the idea is to generate OpenTap Test Plan automatically using Python and Tap API.  
I think I’m not the first one who’s trying to achieve it because I use just a slightly reworked example from the OpenTap Python Plugin repo ([link](https://gitlab.com/OpenTAP/Plugins/python/-/blob/d812a48273b406420e9f9d3d6fefbaaf9ecfb1ca/TapApiFromPython/Misc%20Examples/DynamicallyGenerateTestPlan.py)) which was created by @rolf_madsen for Python 2.

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [November 8, 2021, 5:33pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/6 "2021-11-08T17:33:55Z")

</div>

Okay I see. Have you tried building a plugin in Visual Studio and then used in Python this way?

---

<div class="post-metadata">

**Author:** ![geomags123](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@geomags123](https://forum.opentap.io/u/geomags123)\
**Post date:** [November 8, 2021, 6:30pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/7 "2021-11-08T18:30:24Z")

</div>

@john.berlien no, usually I build plugins using `tap python build <plugin_name>`. Is there any difference?

---

<div class="post-metadata">

**Author:** ![john.berlien](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@john.berlien](https://forum.opentap.io/u/john.berlien)\
**Post date:** [November 8, 2021, 7:22pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/8 "2021-11-08T19:22:53Z")

</div>

Sorry I mean building a C# plugin in Visual Studio.

---

<div class="post-metadata">

**Author:** ![rolf\_madsen](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rolf\_madsen](https://forum.opentap.io/u/rolf_madsen)\
**Post date:** [November 9, 2021, 8:26am UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/9 "2021-11-09T08:26:02Z")

</div>

Hi @geomags123 ,

We don’t support inserting python-defined test steps directly in a test plan, they need to be wrapped first, that is why we have the build step. If you can get a reference to the C# wrapper type it should work.

You probably need to import a module called Python.[ModuleName] and then find your types in there.

---

<div class="post-metadata">

**Author:** ![geomags123](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@geomags123](https://forum.opentap.io/u/geomags123)\
**Post date:** [November 9, 2021, 4:11pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/10 "2021-11-09T16:11:32Z")

</div>

@rolf_madsen not sure I understood everything properly but I inserted python steps exactly after wrapping them with `tap python build <ModuleName>` as you said.

And also I imported the module you said:

```csharp
sys.path.append(str(my_steps_tapPath))
clr.AddReference('Python.my_tap')
import Python.my_tap as MySteps
myStep = MySteps.RegularStep()

```

Is this what you were talking about? Or did you mean another wrapper?

---

<div class="post-metadata">

**Author:** ![rolf\_madsen](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rolf\_madsen](https://forum.opentap.io/u/rolf_madsen)\
**Post date:** [November 9, 2021, 4:34pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/11 "2021-11-09T16:34:53Z")

</div>

Ah sorry, I though I understood what the problem was. Seems not so.

Can you provide the stacktrace from that exception? It seems like something may have to be initialized before you can do that.

---

<div class="post-metadata">

**Author:** ![geomags123](https://avatars.discourse-cdn.com/v4/letter/g/a6a055/32.png) [@geomags123](https://forum.opentap.io/u/geomags123)\
**Post date:** [November 9, 2021, 5:03pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/12 "2021-11-09T17:03:42Z")

</div>

@rolf_madsen

```csharp
Traceback (most recent call last):
  File "c:/Program Files/OpenTAP/Packages/Python/test_GenerateTestPlan.py", line 33, in <module>
    myStep = MySteps.myRegularStep()
System.NullReferenceException: Object reference not set to an instance of an object.
   at Keysight.OpenTap.Plugins.Python.PythonStepWrapper.load(String name, String moduleName)
   at Python.tap.myRegularStep.load_instance()
   at Keysight.OpenTap.Plugins.Python.PythonStepWrapper..ctor()
   at Python.tap.BaseTestStep..ctor()
Fatal Python error: auto-releasing thread-state, but no thread-state for this thread

```

---

<div class="post-metadata">

**Author:** ![rolf\_madsen](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rolf\_madsen](https://forum.opentap.io/u/rolf_madsen)\
**Post date:** [November 10, 2021, 12:03pm UTC](https://forum.opentap.io/t/unable-to-use-steps-generated-with-python-plugin-in-example-from-tapapifrompython/504/13 "2021-11-10T12:03:40Z")

</div>

OK, thanks, so I guess the exception comes from this code: [Keysight.OpenTap.Plugins.Python/SDK/PythonStepWrapper.cs · master · OpenTAP / Plugins / python · GitLab](https://gitlab.com/OpenTAP/Plugins/python/-/blob/master/Keysight.OpenTap.Plugins.Python/SDK/PythonStepWrapper.cs#L145)

I have no idea why it occurs though - I cannot immediately see any problem here.

Can you open an issue at [Issues · OpenTAP / Plugins / python · GitLab](https://gitlab.com/OpenTAP/Plugins/python/-/issues) with this information?
