ModuleNotFoundError: No module named 'PIL' in Fusion360 environment

447 Views Asked by At

I am working on fusion360 python API and face some library issue. Actually, I import PIL python library in my code and it give the error that ModuleNotFoundError: No module named 'PIL'. Can anyone knows how to add the external python libraries in Fusion360 environment that allows me to use the PIL python library for fusion360 API ?

enter image description here

1

There are 1 best solutions below

1
Darius Grigaliunas On

There is no easy way to do that to my knowledge. Fusion 360 uses a separate Python version which only has standard libraries. There are some workarounds mentioned here:

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/to-install-python-modules/td-p/5777176

But overall it is better to stick to standard libraries as making other libraries work will be tedious work. And most likely will stop working after Fusion 360 makes an update

EDIT

One workaround which worked for me quite well is to install the package if it fails to import it. Here is example with ezdxf library:

import platform
import os
from pathlib import Path
import sys
import subprocess

def install_package( package_name ):
    print( f"Installing {package_name}..." )
    if platform.system() == "Windows":
        python_path = str( Path(os.__file__).parents[1] / 'python.exe' )
    else:
        raise Exception( f'Unsupported platform {platform.system()}' )

    try:
        import pip
    except:
        get_pip_filepath = os.path.join(
            os.path.dirname( __file__ ), 'get-pip.py'
        )
        subprocess.check_call( [ python_path, get_pip_filepath ] )

    subprocess.check_call(
        [ python_path, '-m', 'pip', 'install', package_name ]
    )

try:
    import ezdxf
except ModuleNotFoundError:
    install_package( 'ezdxf==0.17' )
    import ezdxf

Notice that you need to to put get-pip.py into location of your script. And this solution can stop working after an update if F360 decides to change something in paths