How to import custom Python library without copying file to Python Lib folder?

How to import custom Python library without copying file to Python Lib folder?

book

Article ID: KB0076500

calendar_today

Updated On:

Products Versions
Spotfire Statistica 13.3.1 and higher

Description

When using the Python node in a workspace, instead of copying Python modules to the Python Lib folder, the end user may import the module as a custom library.

 

Issue/Introduction

Pythton with statistica

Environment

Windows operating systems. Python versions 2.7 and above.

Resolution

For example, Atest.py, attached to this knowledge base article below, cannot be used unless either copied to the Python Lib folder or by importing the path to the Python module. The Atest.py is a simple function that multiplies the input by 2.  Copying to the Python Lib folder is not necessary, instead import the Python module path so that it is accessible. 

To import the path so the Python module can be accessed, insert the following two lines:

import sys
sys.path.insert(0, 'C:/<location of the Python module>/')  #make sure to use the forward slash here and not the backward slash

In the example, the follow lines are in the Python node:

import sys
sys.path.insert(0, 'C:/tmp/') #make sure to use the forward slash here and not the backward slash
import Atest
print (Atest.testFunc(1))
 

Python Node with import path statement

Note:  Without importing the module, the Atest.py module would need to be copied to the Lib file.  For Python 3.6, typical location is C:\Program Files\Python36\Lib.  

Run of Workspace with Python node with copied module to Lib folder

 

Attachments

How to import custom Python library without copying file to Python Lib folder? get_app