Installing a python package with pip from within a script

Installing a python package with pip from within a script

Anonymous
Not applicable
6,647 Views
3 Replies
Message 1 of 4

Installing a python package with pip from within a script

Anonymous
Not applicable

I want to install some third party packages into the Fusion python interpreter in a robust way. I understand one solution is to just include the dependency source code directly within my add-in. I don't want to do this. Instead, I'm trying to run pip from within the python interpreter to install directly to the Fusion python. I tried the following script:

 

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = None
    try:
        text = "Lorem ipsum dolor sit amet"
        def install_and_import(package):
            import importlib
            try:
                importlib.import_module(package)
            except ImportError:
                import pip
                pip.main(['install', package])
            finally:
                globals()[package] = importlib.import_module(package)
        app = adsk.core.Application.get()
        ui  = app.userInterface
        install_and_import('transliterate')
        msg = transliterate.translit(text, 'hy')
        ui.messageBox(msg)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

It should install transliterate package, but instead, when running the command pip.main(['install', package]) all I get is the following message:

 

Collecting transliterate
  Downloading transliterate-1.10-py2.py3-none-any.whl (49kB)
2

The console in Spyder never shows a message that the package was actually installed... it was just downloaded. I checked the same command on my own python interpreter (3.6), and it worked fine, giving a downloading, collecting and installing message. My question is: what did I do wrong? and how do I get pip to successfully install a package?

 

0 Likes
Accepted solutions (2)
6,648 Views
3 Replies
Replies (3)
Message 2 of 4

marshaltu
Autodesk
Autodesk
Accepted solution

Hello,

 

The root cause was that the package was installed under "python interpreter root path/lib/site-packages". For the case of Fusion 360 as Python interpreter, the path of package will be "%localappdata/Autodesk/webdeploy/production/xxxxxx/Lib/site-packages/" in Windows. However the path is not in system path so that the package cannot be imported because the path cannot be found. 

 

I modified the codes to add the path to system path. It should work well now. Please give a try.

 

Thanks,

Marshal

 

import adsk.core, adsk.fusion, adsk.cam, traceback
import sys, os
packagepath = os.path.join(os.path.dirname(sys.argv[0]), 'Lib/site-packages/')
if packagepath not in sys.path:
    sys.path.append(packagepath)

def run(context):
    ui = None
    try:
        text = "Lorem ipsum dolor sit amet"
        def install_and_import(package):
            import importlib
            try:
                importlib.import_module(package)
            except ImportError:
                import pip
                pip.main(['install', package])
            finally:
                globals()[package] = importlib.import_module(package)
        app = adsk.core.Application.get()
        ui  = app.userInterface
        install_and_import('transliterate')
        msg = transliterate.translit(text, 'hy')
        ui.messageBox(msg)

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


Marshal Tu
Fusion Developer
>
0 Likes
Message 3 of 4

Anonymous
Not applicable

Finally got around to trying this solution, and I can't seem to run pip itself. So do I need to then package pip with my addin, use my local pip copy to run this code and then I can use pip freely to download and install additional packages? Does this seem like a good approach?

Message 4 of 4

marshaltu
Autodesk
Autodesk
Accepted solution

Hello,

 

You may have to ship pip packaged with your addin together. Meanwhile you have to consider if there is permission issue if your users run pip to install packages in their machine.

 

In addition, the versions of packages installed in your addin may conflict with others when users install other addins in their machine and those addins also install some Python packages. I would like to recommend you ship all packages with your addin together for safety.

 

Thanks,

Marshal



Marshal Tu
Fusion Developer
>
0 Likes