How to import a Python module?

How to import a Python module?

vkr.mobile
Participant Participant
3,557 Views
5 Replies
Message 1 of 6

How to import a Python module?

vkr.mobile
Participant
Participant

Have not been successful in importing the 'fs' module on my Mac. I've installed the module locally with:

 

pip install fs -t ./modules/

 

The module was installed successfully in the mentioned location, but no matter what I try, the Python API is unable to find the module. I've found some potentially good answers here (adding the path in different ways for instance, which didn't help).

 

Just some of the things I've tried (and commented out):

 

import adsk.core, adsk.fusion, adsk.cam, traceback
import sys, os, subprocess

# subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'fs'])

# sys.path.append('./modules')

import fs
# from modules.fs import *

# pip.main()
0 Likes
Accepted solutions (1)
3,558 Views
5 Replies
Replies (5)
Message 2 of 6

sharas_s
Contributor
Contributor

Hey vkr.mobile,

 

the problem here that you need to install the module into Python used by F360. As they use separate installation of Python, you need to specify that. You are on the right track, just `sys.executable` gives wrong path to python executable in this case and fails to install the module.
Depending on OS and version of F360, path to Python executable might be different. For Mac, this should give you right path:

python_path = sys.executable.split( '/' )
python_path = python_path[ :python_path.index( 'Contents' ) + 1 ]
python_path = '/'.join( python_path ) + '/' + 'Frameworks/Python.framework/Versions/Current/bin/python'

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 ] )
Message 3 of 6

vkr.mobile
Participant
Participant

Thank you very much for your reply. I will try it soon and let you know

0 Likes
Message 4 of 6

vkr.mobile
Participant
Participant
It still doesn't work for me. I tried the following code:

 

import adsk.core, adsk.fusion, adsk.cam, traceback
import sys, os, subprocess

def run(context):
    python_path = sys.executable.split( '/' )
    python_path = python_path[ :python_path.index( 'Contents' ) + 1 ]
    python_path = '/'.join( python_path ) + '/' + 'Frameworks/Python.framework/Versions/Current/bin/python'
    app = adsk.core.Application.get()

    try:
        import pip
        app.log("Made it to here")
    except:
        get_pip_filepath = os.path.join( os.path.dirname( __file__ ), 'get-pip.py')
        subprocess.check_call( [ python_path, get_pip_filepath ] )

 

And I get the following errors:

 

SCRIPT ERROR 

Traceback (most recent call last):
  File "/Users/parsec/Library/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/NewScript1/NewScript1.py", line 23, in run
    import pip
ModuleNotFoundError: No module named 'pip'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/parsec/Library/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/NewScript1/NewScript1.py", line 27, in run
    subprocess.check_call( [ python_path, get_pip_filepath ] )
  File "/Users/parsec/Library/Application Support/Autodesk/webdeploy/production/ea18aff57f0846f55d8fcb840ecd92f4bf0182fd/Autodesk Fusion 360.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python3.9/subprocess.py", line 373, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/Users/parsec/Library/Application Support/Autodesk/webdeploy/production/ea18aff57f0846f55d8fcb840ecd92f4bf0182fd/Autodesk Fusion 360.app/Contents/Frameworks/Python.framework/Versions/Current/bin/python', '/Users/parsec/Library/Application Support/Autodesk/Autodesk Fusion 360/API/Scripts/NewScript1/get-pip.py']' returned non-zero exit status 1.

 

0 Likes
Message 5 of 6

Jorge_Jaramillo
Collaborator
Collaborator
Accepted solution

Hi @vkr.mobile ,

 

I post a similar answer here, which you can use load any other module.

 

You need to know that Fusion360 comes with an own python's version, which may differ from the version you already have on your PC/MAC.  That is why modules installed with PIP are not being loaded on Fusion if the versions don't match.  You will find the step-by-step to setup a virtual environment with Fusion360 python's version, to install modules for that version, and how to tell Fusion360 python's module to look for them.

 

Hope this help. Let me know if it works for you.

 

Regards,

Jorge

 

 

0 Likes
Message 6 of 6

vkr.mobile
Participant
Participant

This in the end worked for me. Took me a bit to figure things out.

 

Thanks for the help @Jorge_Jaramillo @sharas_s.

 

 

0 Likes