1

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

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
Usman
  • 1,983
  • 15
  • 28

1 Answers1

-1

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

  • 1
    I guess the answer was down voted because it uses a link to a page that might go away. The short of it is that Fusion 360 will re-install the private and temporary Python it uses without warning. Even if you game the private Python install you can lose your installs at any time. Imports and sys.path in Fusion 360 make import and importlib very hard to use especially if you want to do advanced things such as plug-ins. The path issues also broke the Pycharm integration. – tpc1095 Apr 17 '23 at 19:28