Download

Must restart the Editor to use updated Python code?

I’m using 4.21.

I found that if my Python scripts changed in an external editor, the change is not always picked up by an open project in Unreal, even after deleting all the .pyc and caches.
The only way to make sure is to reopen the uproject.

Is there a way to reliably use updated code without closing the project?

I believe you need to “reload” your module for python to resource the py file. if you are using python 3.x and higher inside of UE4 you need to "from importlib import * " then “reload”

python 2.7


import foo as bar
reload(bar)

or python 3.x


import foo as bar
from importlib import *
reload(bar)

Thanks for the tip!
I didn’t know I could run py3x inside UE4. I’ve been running the Unreal builtin py27 … Could you kindly show me how to work with py36 that I installed from python.org?

Trying this trick with UE’s py27, I got more problems.



try:
    import unreal as ue
except ModuleNotFoundError:
    pass


This code helps prevent running the script outside of UE, but after inserting the reload call




try:
    import unreal as ue
    reload(ue)
except ModuleNotFoundError:
    pass



I got


LogPython: Error: NameError: name 'ModuleNotFoundError' is not defined

Moving the reload call after the try block, I got


LogPython: Error: ImportError: No module named unreal


What am I missing?

There is a way to swap out the unreal engine’s default python 2.7 to Python 3.x and higher …BUT you need to rebuild the engine from source code with the higher version of python swapped in. Under the following directory there is a bat file that will help swap out the python version. I believe you need to register your python 3.6 under system path for the bat file to recognize/find that python install and then swap it in. Once its copied all the python files (running that bat file), you will need to open up visual studio and build the engine/editor/everything. I tried this on ue4.21 and it worked perfectly. Haven’t done this on ue4.22 yet. Again the following file comes from the source code on github, not through the launcher.


[UnrealEngine](https://github.com/EpicGames/UnrealEngine)/[Engine](https://github.com/EpicGames/UnrealEngine/tree/release/Engine)/[Source](https://github.com/EpicGames/UnrealEngine/tree/release/Engine/Source)/[ThirdParty](https://github.com/EpicGames/UnrealEngine/tree/release/Engine/Source/ThirdParty)/**Python**/[CopyPythonInstall_Win64.bat](https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/ThirdParty/Python/CopyPythonInstall_Win64.bat)

Epic also mentions it here on this page.

https://docs.unrealengine.com/en-us/Editor/ScriptingAndAutomation/Python

For reloading, you want to reload your own modules/scripts inside of the ue editor, so you don’t need to relaunch the editor everytime you make a change (to the py file…using whatever IDE).
Also unreal modules do not work outside the editor itself or a separate python installation.

one way to reload, take the following py file ‘c:/temp/libraryUI.py’


import sys
sys.path.append('c:/temp')
import libraryUI as UI
reload(UI)

UI.do_something()#a function inside of libraryUI

Make changes to py file, run the above code again in UE without closing it.

@cgsquirrel2 Thanks a lot. I missed the Unreal doc section about rebuilding from source. My bad.

Just a note, but please don’t reload the unreal module itself as that’s defined and managed in C++, so can’t be reloaded from Python as there’s nothing for it to reload.

You’re fine to reload your own Python modules though.

If you have a source build and want to use a different version of Python, then you can also set UE_PYTHON_DIR rather than copying the SDK into our ThirdParty folder (as long as you don’t care about the portability of your build).