Using third party Python packages with Apex
In common usage Apex macros and scripts are executed using a Python interpreter that is delivered and integrated with Apex. The Python interpreter shipped with Apex 2021.4 is Python 3.8.2. Using Macro/Play or invoking a custom tool causes the Python script associated with the Macro or custom tool to be interpreted by the Python interpreter that is integrated with Apex. This architecture introduces some constraints on the usage of third party Python packages with Apex scripts,
- You will not see “python.exe” in the Windows task manager when Apex is executing a Python script – this is mostly a cosmetic issue
- Depending on your company’s IT policies you may or may not have write access to the Apex installation folder where the Apex Python interpreter is installed. This may make it difficult, or impossible, to install third party packages into the standard “Lib/site-packages” folder of the Apex Python installation
- Because the Python interpreter and the Apex executable are both executing in the same process space, Windows requires that any compiled code that may be used to support third party Python packages must use the same Microsoft VC++ runtime as Apex. Note that Apex delivers and installs not only a Python interpreter but also a reasonable suite of scientific and engineering packages including numpy, scipy, pandas, h5py, tables, matplotlib, vtk and many others. Windows requires that all code running in a single Windows process must use the same VC++ runtime so Python packages that are implemented using compiled code (such as numpy, pandas etc) must use the same Microsoft VC++ runtimes as Apex, in addition to being compatible with the Python version that is delivered with Apex. For the Apex 2021.4 release this meas that all Python packages used in a Apex scripts must be,
- compiled and linked with the VC++ 14.X runtime (Visual Studio 2015 and later support this version)
- must be compatible with Python 3.8.2.
- In order to provide a controlled environment for the Apex executable, Apex modifies its runtime environment prior to starting. Specifically, Apex modifies the Windows PATH and PYTHONPATH environment variables. Note that Apex only modifies these environment variables for the process in which it is running – no other processes are impacted. Since third party Python packages typically rely on both of these environment variables, and the Apex Python interpreter inherits the modified environment, configuration of these environment variables that may have been made to ensure correct execution of the third party packages outside of Apex will NOT be available when these packages are invoked by the Apex integrated Python interpreter.
If you need to use Python packages that are not included with the Apex installation, please make sure they are compatible with the above Python and VC++ versions. If you have write access to the location where Apex is installed, the most convenient way of achieving this is to use pip to install the target package as shown below,
~my_apex_installation\python3\python3.8.exe -m pip install <package_name>
If you do not have write access to your Apex installation folder, you can access Python packages from a separate compatible Python installation. The following example uses the Anaconda distribution as an example but can be adapted to other Python distributions,
- Download Anaconda 2021.05 for 64 bit Windows (Anaconda3-2021.05-Windows-x86_64.exe) from https://repo.anaconda.com/archive/
- Add the following lines to your script:
import sys import os
# Modify the following locations to point to the appropriate site-packages folder where you have installed 3rd party Python packages
os.environ['PATH'] = r'E:\MSC\Software\Anaconda3_2021.05\Library\bin' + ';' + os.environ['PATH'] sys.path.insert(0, r'E:\MSC\Software\Anaconda3_2021.05\Lib\site-packages')
#Add the following line if you are running Python packages that need QT
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'E:\MSC\Software\Anaconda3_2021.05\Library\plugins\platforms‘
# All of the preceding lines must appear BEFORE the ‘import apex’ statement in your script
- Log in to post comments
