UE4.27 crashes while running python script on CMD

When i execute this script in UE4, it works successfully.
When i run this script on command line with:

“C:\Program Files\Epic Games\UE_4.27\Engine\Binaries\Win64\UE4Editor-Cmd.exe” “C:\Users\Documents\Unreal Projects\test\test.uproject” -run=pythonscript -script=“D:/test_unreal.py”

I get this error and UE4 crashes:

The python script has only these lines:

import unreal
import time
unreal.EditorLevelLibrary.load_level(asset_path=’/Game/StarterContent/Maps/Minimal_Default’)
unreal.EditorLevelLibrary.spawn_actor_from_object(unreal.CameraActor(name=‘camera1’), location=(0,0,0))

Up to the third line it works fine, but its the 4th line that crashes UE when running it from command line. Anyone knows why??

It can be because you are running the editor in headless mode and in that case some of the context is not loaded.

Just to check does it work if instead you do
“C:\Program Files\Epic Games\UE_4.27\Engine\Binaries\Win64\UE4Editor-Cmd.exe” “C:\Users\Documents\Unreal Projects\test\test.uproject” -ExecutePythonScript="D:/test_unreal.py"

Drawback is that it is slower and you have a full UE starting.

Do you know how to solve Unreal5.3? I hope to open the Unreal project file in an external Python editor and execute the Python script I execute in Unreal
-It seems that ExecutePythonScript did not execute my Python script
And if I use it, but it only pops up a box and doesn’t actually open the Unreal editor. I look forward to your reply very much, thank you!

import subprocess
subprocess.Popen([
    "path/to/UnrealEditor-Cmd.exe", "path/to/my.uproject", '-run=pythonscript',
    '-script="path/to/myscript.py"'
])