> For the complete documentation index, see [llms.txt](https://bkortbus.gitbook.io/unreal-python-recipe-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bkortbus.gitbook.io/unreal-python-recipe-book/startup-and-shutdown-scripts/treat-init_unreal-like-__init__.md).

# Treat init\_unreal like \_\_init\_\_

how I use the init\_unreal.py files

## How It Works

On Editor Startup, Unreal will look for and run all files named `init_unreal.py` in the Python Path, there are multiple files with that name and only the first one loaded will be available as an actual import:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FcwtiqRh8pkfizWSBMhOn%2Fimage.png?alt=media&amp;token=0390dbb7-7b47-43b1-ac82-5e858d5bb0bf" alt=""><figcaption></figcaption></figure>

declaring functions and classes in this file can muddy their file location as they will belong to the `__main__` namespace:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FiUI54QVkFE6mhn7fxdGx%2Fimage.png?alt=media&amp;token=c628c261-ea1b-40b3-b4da-4e18b37514be" alt="" width="508"><figcaption></figcaption></figure>

{% hint style="info" %} <mark style="color:yellow;">Note</mark>: For Developers & Technical Staff this can be useful to pre-load Python modules and functions into the \_\_main\_\_ namespace, it can be useful when using the Python CLI in the Output Log window
{% endhint %}

***

## Further Examining init\_unreal

Because Unreal executes every init\_unreal.py file it finds in sys.path, there could be any number of competing python modules of the same name

To demonstrate this, here is a code snippet that lists the available init\_unreal files and then shows which one is currently importable:

```python
import sys
from pathlib import Path

print("init_unreal modules available:")
for folder_path in sys.path:
    for init_file in Path(folder_path).glob("init_unreal.py"):
        print(f"\t{init_file.resolve()}")

print("Location of imported init_unreal:")
import init_unreal
print(f"\t{init_unreal.__file__}")
```

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FMc7abO7MYL7NH1fopGpC%2Fimage.png?alt=media&amp;token=45206c38-46e0-4882-a3be-0ad9cc0f3324" alt=""><figcaption></figcaption></figure>

This can make it challenging to debug or inspect a specific startup script after Unreal opens.

***

## Example Setup

Because of how init\_unreal works, I treat those files files like an \_\_init\_\_.py file:

* Avoid declaring functions or classes in it directly
* Import modules as needed, such as a dedicated startup module
* Call startup functions from imported modules

In my `Content/Python` folder I usually have a sub-folder for my actual Python code alongside the init\_unreal.py file, I mostly do this for organization purposes is all:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FRB9K80Qo8ac5ZsvZzsCN%2Fimage.png?alt=media&amp;token=3adcb6c6-2caa-429d-9d7e-667304f802b6" alt=""><figcaption></figcaption></figure>

My init\_unreal.py files usually look something like this:

```python
print("Running the standard init_unreal.py script!")

from devcode import function_library, startup

startup.run()
```

* **function\_library** has a **BlueprintFunctionLibrary** class in it, this import makes the class available to Blueprints
* the **startup.run()** function runs my desired startup logic, this helps to reduce confusion if any errors occur and makes it easier to debug / edit if needed
