Skip to main content

Import Python Modules

IronPython is regular Python but with the option of using .NET classes or DLL files (e.g. the TCAdmin SDK).

Use .NET in IronPython

If you want to use any of the .NET classes, you will need to import them into the IronPython script like you would in Python. Before using any .NET classes, you will need to import the clr module, however. This is an example of how to use the .NET WebClient class, so we can use the DownloadFile() method:

from System.Net import WebClient
from System.IO import Path

#Download the TCAdmin logo to the service's root directory
try:
wc = WebClient()
wc.DownloadFile('https://clients.tcadmin.com/billing/images/logo.png', Path.Combine(ThisService.RootDirectory, 'tcadmin-logo.png'))
except Exception as e:
Script.WriteToConsole('Could not download the file. Error: {}'.format(e))

Use the TCAdmin SDK

Before you can use the TCAdmin SDK (or any DLL files for that matter), you need to reference them first. This is done by using the clr.AddReference() method.

import clr
clr.AddReference('TCAdmin.SDK')
from TCAdmin.SDK import Mail

#Print the control panel URL to the script window
company_info = Mail.CompanyInfo()
Script.WriteToConsole(company_info.ControlPanelUrl)

Use custom Python modules

In some cases, you might need to use a module that is not included in the default Python library.

import sys
sys.path.append('C:\\CustomPythonModules\\Lib')
from bs4 import BeautifulSoup
# do stuff with bs4