# Problems using python with Editor in UE4

**URL:** <https://forums.unrealengine.com/t/problems-using-python-with-editor-in-ue4/244763>\
**Category:** Editor Scripting\
**Tags:** unreal-engine, UE4-26\
**Created:** [August 10, 2021, 7:07am UTC](https://forums.unrealengine.com/t/problems-using-python-with-editor-in-ue4/244763 "2021-08-10T07:07:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_6937c7291](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_6937c7291](https://forums.unrealengine.com/u/anonymous_user_6937c7291)\
**Post date:** [August 10, 2021, 7:07am UTC](https://forums.unrealengine.com/t/problems-using-python-with-editor-in-ue4/244763/1 "2021-08-10T07:07:03Z")

</div>

Hello, I met a problem when i using python with Editor in UE4.  
I find we can use python command “unreal.EditorLevelLibrary.editor\_play\_simulate()” to play the editor and use python command “unreal.EditorLevelLibrary.editor\_end\_play()” to stop the play mode.  
But I write these two commands in a file, for example “test.py”, and then run"py test.py", I find the editor is still in the play mode, the stop command doesn’t work.  
Does anyone have suggestions?  
Thank you.

---

<div class="post-metadata">

**Author:** ![lvoos](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@lvoos](https://forums.unrealengine.com/u/lvoos)\
**Post date:** [July 11, 2022, 2:27pm UTC](https://forums.unrealengine.com/t/problems-using-python-with-editor-in-ue4/244763/2 "2022-07-11T14:27:30Z")

</div>

I experience this as well, UE 4.27

---

<div class="post-metadata">

**Author:** ![Veseliy](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/veseliy/32/600830_2.png) [@Veseliy](https://forums.unrealengine.com/u/Veseliy)\
**Post date:** [July 13, 2022, 1:37am UTC](https://forums.unrealengine.com/t/problems-using-python-with-editor-in-ue4/244763/3 "2022-07-13T01:37:06Z")

</div>

Hello,  
I guess it has to do with python working in one thread, or basically locking the process, and there is no time for editor to start when the end\_play is triggered.

It works with a below snippet. Hope it helps.

```auto
import unreal
from threading import Timer

def hello():
    print("hello, world")
    unreal.EditorLevelLibrary.editor_end_play()

unreal.EditorLevelLibrary.editor_play_simulate()
t = Timer(5.0, hello)
t.start() # after 5 seconds, "hello, world" will be printed and editor will stop

```

Regards,  
Alex.

---

<div class="post-metadata">

**Author:** ![HenryMa96](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HenryMa96](https://forums.unrealengine.com/u/HenryMa96)\
**Post date:** [October 23, 2023, 8:12am UTC](https://forums.unrealengine.com/t/problems-using-python-with-editor-in-ue4/244763/4 "2023-10-23T08:12:52Z")

</div>

Great! It really helps!!

---

<div class="post-metadata">

**Author:** ![HenryMa96](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@HenryMa96](https://forums.unrealengine.com/u/HenryMa96)\
**Post date:** [October 23, 2023, 10:07am UTC](https://forums.unrealengine.com/t/problems-using-python-with-editor-in-ue4/244763/5 "2023-10-23T10:07:22Z")

</div>

The threading Timer may result in “not in game thread” issues.  
one possible solution is below: from [Python in Unreal Tips | Ryan DowlingSoka](https://ryandowlingsoka.com/unreal/python-in-unreal/)

```auto
class MyClass(object):
	def __init__ (self) -> None:
		self.frame_count = 0
		self.max_count = 1000

	def start(self) -> None:
		self.slate_post_tick_handle = unreal.register_slate_post_tick_callback(self.tick)
		self.frame_count = 0

	def tick(self, delta_time: float) -> None:
		print(self.frame_count)
		self.frame_count += 1
		if self.frame_count >= self.max_count:
			unreal.unregister_slate_post_tick_callback(self.slate_post_tick_handle)

test = MyClass()
test.start()

```

---

<div class="post-metadata">

**Author:** ![greg700000](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@greg700000](https://forums.unrealengine.com/u/greg700000)\
**Post date:** [January 21, 2025, 3:01am UTC](https://forums.unrealengine.com/t/problems-using-python-with-editor-in-ue4/244763/6 "2025-01-21T03:01:46Z")

</div>

Finally a working answer to my need. Thx!
