NX How to execute NXOpen Python code from any command prompt?

2023-09-25T14:19:42Z
NX Open

Summary


Details

How to execute NXOpen Python code from any command prompt?

Solution

There are at least two ways for executing a Python source code (.py) file with NXOpen APIs without a NX GUI (graphical user interface).

  1. Using the embedded Python libraries from a standard NX installation, open a NX Command Prompt and execute the 'run_journal.exe' command line utility to solve the paths for the 'import NXOpen' statement.
    1. Using an external Python distribution, open a standard Command Prompt, set PATH and PYTHONPATH environment variables and execute the 'python.exe' application.

      Example for second option (assuming NX 2306 and an external Python 3.10 distribution in D:\Python310):

      # Demo - report_nx_version.py import NXOpen if __name__ == '__main__': # obtain NX session theSession = NXOpen.Session.GetSession() # query the hardcoded NX variable nxver = theSession.GetEnvironmentVariableValue("NX_FULL_VERSION") # output in internal or external mode theSession.ListingWindow.Open() theSession.ListingWindow.WriteLine("NX Full Version string is {0}".format(nxver))

      .

      Command Prompt output:

      >set PATH=%UGII_BASE_DIR%\NXBIN;d:\python310;%PATH%
      >set PYTHONPATH=D:\Python310;D:\Python310\DLLs;D:\Python310\Lib;D:\Python310\Lib\site-packages;%UGII_BASE_DIR%\NXBIN\python
      >python report_nx_version.py
      NX Full Version string is v2306.0.0.4000

      Notes

      UGII_PYTHONPATH is only required and used by NX internal/external because the common PYTHONPATH was blocked by the JACK toolkit which used another Python version in earlier days.

      So if you don’t need Python debugging with Eclipse or similar, then this approach makes it easier and faster as you don’t even need a NX command prompt.

      See also:

      NX Open Programmer's Guide - Use external Python distribution
      https://docs.sw.siemens.com/en-US/doc/209349590/PL20221117716122093.nxopen_prog_guide/xid1124926

      NX Open Programmer's Guide - Executing Batch Applications - NX Open for Python
      https://docs.sw.siemens.com/en-US/doc/209349590/PL20221117716122093.nxopen_prog_guide/xid862706

      KB Article ID# PL8750551

      Contents

      SummaryDetails

      Associated Components

      NX Open Python