# Setup Your Own Python Template

With Dynamo 2.0 we have the ability to specify a default template `(.py extension)` to use when opening the python window for the first time. This has been a long-desired request as this expedites the usage of Python within Dynamo. Having the ability to use a template allows us to have default imports ready to go when we want to develop a custom Python script.

The location for this template is in the `APPDATA` location for your Dynamo install.

This is typically as follows `( %appdata%\Dynamo\Dynamo Core\{version}\ )`.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-0714e70f6b1c13dbe7bfb63adb5be14c4f7d4b44%2Fpython%20templates%20-%20appdata%20folder%20location.jpg?alt=media\&token=e68a2aae-cb4e-4254-a111-dc7a763a3052)

### Setting Up The Template

In order to utilize this functionality we need to add the following line in our `DynamoSettings.xml` file. *(Edit in notepad)*

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-082766a2f8ccfc1cf64a54a6c8bb47c5fe5a1b24%2Fpython%20templates%20-dynamo%20settings%20xml%20file.png?alt=media\&token=13541ff7-5f82-4164-8d20-17202c4666f5)

Where we see `<PythonTemplateFilePath />`, we can simply replace this with the following:

```
<PythonTemplateFilePath>
<string>C:\Users\CURRENTUSER\AppData\Roaming\Dynamo\Dynamo Core\2.0\PythonTemplate.py</string>
</PythonTemplateFilePath>
```

{% hint style="warning" %}
*Note: replace CURRENTUSER with your username*
{% endhint %}

Next we need to build a template with the functionality that we want to use built-in. In our case lets embed the Revit related imports and some of the other typical items when working with Revit.

You can start a blank notepad document and paste the following code inside:

```py
import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

#Preparing input from dynamo to revit
element = UnwrapElement(IN[0])

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

TransactionManager.Instance.TransactionTaskDone()

OUT = element
```

Once that is done, save this file as `PythonTemplate.py` in the `APPDATA` location.

### Python Script Behavior After

After the python template is defined, Dynamo will look for this each time a Python node is placed. If it is not found it will look like the default Python window.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-aca54f9c4e0881f8ee8535fc2cb618edd339dad5%2Fpython%20templates%20-%20before%20setup%20template.jpg?alt=media)

If the Python template is found (like our Revit one for example) you will see all of the default items you built in.

![](https://1734247194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FY5ZuHF3yuXFWp1C46ZSo%2Fuploads%2Fgit-blob-2bce8274a46e009bfb77c1171010cf532bca2f35%2Fpython%20templates%20-%20after%20setup%20template.jpg?alt=media)

Additional information regarding this great addition (by Radu Gidei) can be found here. <https://github.com/DynamoDS/Dynamo/pull/8122>
