ModuleNotFoundError: No module named ‘distutils’

Product:
Planning Analytics 2.0.9.19
Microsoft Windows 2019 server
Python 3.12

Issue:

When run below code, i get a error like:

C:\Python312>python.exe c:\proj1\test.py
Traceback (most recent call last):
File “c:\proj1\test.py”, line 4, in <module>
from distutils.util import strtobool
ModuleNotFoundError: No module named ‘distutils’

 

import getpass
from distutils.util import strtobool

from TM1py.Services import TM1Service

# Parameters for connection
user = input("TM1 User (leave empty if SSO): ")
password = getpass.getpass("Password (leave empty if SSO): ")
namespace = input("CAM Namespace (leave empty if no CAM Security): ")
address = input("Address (leave empty if localhost): ") or "localhost"
gateway = input("ClientCAMURI (leave empty if no SSO): ")
port = input("HTTP Port (Default 5000): ") or "5000"
ssl = strtobool(input("SSL (Default T or F): ") or "T")

if len(namespace.strip()) == 0:
namespace = None

if len(gateway.strip()) == 0:
gateway = None

try:
with TM1Service(
address=address,
port=port,
user=user,
password=password,
namespace=namespace,
gateway=gateway,
ssl=ssl) as tm1:
server_name = tm1.server.get_server_name()
print("Connection to TM1 established!! your Servername is: {}".format(server_name))
except Exception as e:
print("\nERROR:")
print("\t" + str(e))

 

Solution:

Change to python version 3.10 as python version 3.12 does not support this commands.

Python 3.12 no longer provides any standard-library distutils version at all. There is no longer any “stdlib” distutils, so it cannot be selected. Because distutils was deprecated in 3.10 and removed in 3.12.

You can still use distutils on Python 3.12+ by installing setuptools.

Run the command: python.exe -m pip install setuptools

setuptools provides a module _distutils_hack, which when loaded replaces sys.modules[‘distutils’] to point to setuptools._distutils; however, it only does this when SETUPTOOLS_USE_DISTUTILS is either unset or has the value ‘local’.

You can also avoid using the distutils functions.

 

More information:

https://code.cubewise.com/blog/check-connectivity-with-tm1/

https://marketplace.visualstudio.com/items?itemName=ms-python.python

https://realpython.com/python-zipfile/#creating-zip-files-sequentially

https://github.com/cubewise-code/tm1py

https://github.com/cubewise-code/TM1py-samples