Running a 'system' Python script from a virtual environment

Hi All,

First off, this is my first post here. I’m no Python pro but I’ve written quite some Python and Micro Python code the past few years.

I started this post explaining the whole situation but that got way to complicated so I tried to narrow it down to the actual problem, if you need more info, just ask.

What I’m trying to do:

  • I have a script (A) that runs in the virtual environment of Fusion 360
  • I would like to call/run a script (B) on my ‘system Python’ from the Fusion script, see code below

The error I’m getting is:
PermissionError: [WinError 5] Access is denied

So my question is, how do I run a script on my main Python installation from within the Fusion 360 environment?

Thanks for your feedback.

My Fusion 360 script:

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

def run(context):
	
	app = adsk.core.Application.get()
	ui  = app.userInterface
	   
	try:
		
		# Run the script and wait for it to complete
		python_path = "C:/Users/...../AppData/Local/Programs/Python/Python312"
		script_path = "D:/Python_testscript/remote_script.py"
		result = subprocess.run([python_path, script_path], capture_output=True, text=True)

		# Check if the script ran successfully
		if result.returncode == 0:
			print("Script ran successfully")
			print("Output:", result.stdout)
		else:
			print("Script encountered an error")
			print("Error:", result.stderr)

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

Is this what you’re looking for?

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-9701BBA7-EC0E-4016-A9C8-964AA4838954

You should be able to run a script with the systm Python by invoking the
system Python.

You should check that these paths are correct:

 python_path = "C:/Users/...../AppData/Local/Programs/Python/Python312"
 script_path = "D:/Python_testscript/remote_script.py"

Can you invoke that command directly eg from the command prompt?

 C:/Users/...../AppData/Local/Programs/Python/Python312 D:/Python_testscript/remote_script.py

FInally, a try/except should (a) surround only the code you’re trying to
catch, and not catch everything, and report what it catches.

1 Like

Ok, bear with me, this is the first time trying to run scripts between environments.

Below are my scripts, the first one (capture_canvas.py) runs in Fusion.
The second (capture.py) should run on my systems Python.

I added ‘/python.exe’ to ‘python_path’ in ‘capture_canvas.py’, the script that runs in Fusion.
I tested ‘capture.py’ by running it from the command line, it works and saves a picture.

However, when I run capture.py from Fusion it does return result.stdout correctly but it does not save a picture.

Sorry if this is confusing but this is new to me.

Thanks for your feedback.

The capture_canvas.py script that runs in Fusion:

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

def run(context):
	
	app = adsk.core.Application.get()
	ui  = app.userInterface
	   
	# Run the script and wait for it to complete
	python_path = "C:/Users/....../AppData/Local/Programs/Python/Python312/python.exe"
	script_path = "D:/Python_testscript/capture.py"
	result = subprocess.run([python_path, script_path], capture_output=True, text=True)

	if result.returncode == 0:
		ui.messageBox(f'returncode: {result.returncode} \r\nstdout: {result.stdout}')
	else:
		ui.messageBox(f'returncode: {result.returncode}  {result.stderr}')

The capture.py script that runs on my systems python:

import cv2
from datetime import datetime

if __name__ == '__main__':

	now = datetime.now()
	filename = f'{now.year}.{now.month:02d}.{now.day:02d}.{now.hour:02d}.{now.minute:02d}.{now.second:02d}'

	print(filename)

	cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)			# Capture from USB webcam
	cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)             # Set image size
	cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 960)
	cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25)           # Disable auto-exposure
	cap.set(cv2.CAP_PROP_EXPOSURE, -4)                  # Set exposure

	ret, frame = cap.read()

	if ret: cv2.imwrite(f'{filename}.jpg', frame)

	cap.release()

Also, how can I post code so it scrolls horizontally?
The line wrapping makes the code look messed up at the moment.

I got it working now.