Reading YAML from a Python Script

Reading YAML from a Python Script

bloudraak
Enthusiast Enthusiast
883 Views
4 Replies
Message 1 of 5

Reading YAML from a Python Script

bloudraak
Enthusiast
Enthusiast

I have a YAML file that I'd like to use in a Python script to drive design in Fusion 360.  I rean

 

pip3 install pyyaml

 

But when running the script, you'll get an error yaml isn't found. 

 

I understand that Fusion 360 uses its own version of python, and as such there are some challenges. But in all honesty, the beauty of using Python is the ability to use tens of thousands of packages out there that can add value to Fusion 360.  It would be rather disappointing if I'm unable to reference external packages, or if Fusion 360 discards changes to the environment after an upgrade. 

Software Engineer
https://wernerstrydom.com
0 Likes
Accepted solutions (1)
884 Views
4 Replies
Replies (4)
Message 2 of 5

JeromeBriot
Mentor
Mentor

Hello,

 

You have to execute pip3 in the Python folder of Fusion 360. To find its path, run the following script (taken from the addin Open folders) :

 

import os
directory = os.path.join(os.getenv('LOCALAPPDATA'), 'Autodesk', 'webdeploy', 'production')
fusion360Install = max([os.path.join(directory,d) for d in os.listdir(directory)], key=os.path.getctime)
print(os.path.join(fusion360Install, 'Python'))

 

Note that you have to reinstall the module each time Fusion 360 is updated.

 

Some other methods have been discussed recently: After Fusion 360 background update, I need to reinstall numpy . . . . . . .

 

 

 

0 Likes
Message 3 of 5

bloudraak
Enthusiast
Enthusiast
Thanks. It's a bit disappointing. Is C++ support any better in that I can link with 3rd party libraries and not have to worry about updates breaking scripts or add-ins? I've only used Python scripts.
Software Engineer
https://wernerstrydom.com
0 Likes
Message 4 of 5

JeromeBriot
Mentor
Mentor
Accepted solution

Try this:

  1. Install pyyaml using pip3 as described previously
  2. Go to the "Python\Lib\site-packages" subfolder
  3. Copy the folder "yaml" into a subfolder "modules" in your add-in folder
  4. Uninstall pyyaml using pip3
  5. Call pyyaml by using the following inclusion:

 

from .modules import yaml

 

Now the module is independent from the Fusion 360 installation.

 

 

0 Likes
Message 5 of 5

pludikar
Collaborator
Collaborator

@bloudraak 

 

If you want to permanently attach a python library or extension to a script or Addin you are writing, then the only real way is to put the library/extension as a subfolder to your script folder.  It's briefly described here in the Fusion API user manual, but you'll have to scroll down to the appropriate section, as the page anchor is broken 

 

If the library is more complex than a single folder, you may have to insert the appropriate folder path into syspath.

 

sys.path.insert(0, appPath+"\\py_packages_path")

 

I had to do that to include the pydantic extension, because there were inter package dependencies that couldn't be resolved using relative references. 

 

If you just want to have the extension available just for development purposes then @JeromeBriot 's answer is the way to go.

 

Regards

Peter   

 

 

I'm not an expert, but I know enough to be very, very dangerous.

Life long R&D Engineer (retired after 30+ years in Military Communications, Aerospace Robotics and Transport Automation).
0 Likes