Running Python Scripts Outside simCNC (e.g., in VS Code)

Created by Andrzej Rogozynski, Modified on Mon, 10 Aug at 12:58 PM by Andrzej Rogozynski

When a Python script is run directly from simCNC - whether as a pyAction, from the built-in editor, or by another internal method - simCNC silently executes a few initialization lines in the background. This keeps your scripts cleaner and eliminates the need to include the initialization code manually.


However, if you open and run the script using an external editor or debugger, such as VS Code, the script will fail because this initialization code is missing.


To solve this, simply add the following header at the top of your script. With this header included, the script will run correctly regardless of whether it is executed from simCNC or from an external editor or debugger.


#############################################
# CS-Lab s.c.
#
# simCNC universal script header
#
# Paste this block at the very top of a simCNC Python macro / M-code /
# python-action script (m3.py, probing.py, a custom python action, ...).
# It makes the file runnable, unchanged, in two contexts:
#
#   1) Started by simCNC itself. simCNC's own ___INIT.py has ALREADY
#      created d, msg, gui, t and exit() before this file's code runs,
#      so the block below detects that and does nothing - zero overhead,
#      zero behavior change for scripts running normally inside simCNC.
#
#   2) Started directly by an external interpreter (VS Code, PyCharm,
#      a plain "python thisFile.py", ...) for development/debugging with
#      a real editor/debugger. The block below then recreates the same
#      d, msg, gui, and exit() objects simCNC would have provided, by
#      opening the same kind of connection simCNC's own tools use to
#      talk to a running instance. Connections from localhost are always
#      accepted, so nothing needs to be configured for the common case of
#      running the external interpreter on the same machine as simCNC.
#
# Requirements for case 2:
#   - simCNC must already be running, with a profile open.
#   - This file must run from its normal location, a profile's "scripts"
#     folder (<profile>/scripts) - that's where ___DEVICE.py, ___MSG.py, 
#     ___GUI.py, ... live, and they're imported here by plain module name.
#   - Running the external interpreter on a different machine than
#     simCNC needs "external connections" enabled and a matching
#     password in simCNC's settings; same-machine needs neither.
#
# Safe to leave in place permanently - simCNC-run scripts skip it.
#############################################
if 'd' not in globals():
    import sys as simCncSys
    import os as simCncOs
    simCncSys.path.insert(0, simCncOs.path.dirname(simCncOs.path.abspath(__file__)))

    from ___DEVICE import *
    from ___MSG import Msg

    d = Device(ip='localhost', password='')  
    msg = Msg()

    def exit(exitCode=0):
        d.getComm().sendScriptFinished()
        simCncSys.exit(exitCode)

    try:
        from ___GUI import Gui
        gui = Gui(d.getComm())
    except:
        pass
#############################################
# END simCNC universal script header
#############################################

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article