Origin provides embedded python support, allowing users to execute python code within Origin. The embedded Python in Origin includes all the basic modules and the modules that are installed to your Python application. However, there are still many popular packages, e.g. PyQt4, numpy, scipy, are not included in Origin. In this blog, we will show you step by step how to load these packages with Origin’s embedded Python.
The basic idea is to add the path of the desired python package to the search path list of Origin’s embedded Python. Assume you already installed an external Python and the package that you want to use inside Origin, confirm the following two things before read further:
- Make sure the external Python to be compatible with the embedded Python version in Origin. For Origin 2017, the embedded Python is updated to the latest version — version 3.5 and version 2.7. If you have Origin 2016 or 2015, the Python version is 3.3 and 2.7 instead. And in this blog, we tested with built-in Python 3.5 in Origin 2017. So the external Python should also be Python 3.5 .
- Make sure the external Python corresponding to same CPU register size as the Origin being running – 32 bit or 64 bit. To know whether current Origin version is 32 bit or 64 bit, you can directly read it from Origin program window title bar or type command “system.bits=;” in Command Window or Script Window. Here we are running a 64-bit Origin. Make sure the external Python is 64-bit.
NOTE: If you haven’t installed an external Python, You need to install it first. We recommend you download Python from one of the Python distributions: Anaconda, Enthought Canopy, Python(x,y), WinPython, Pyzo. It saves you time because many useful Python packages are already included. If you prefer manually installing packages, Christoph Gohlke provides pre-built Windows installers for many Python packages, which work extremely well.
In this blog, we obtained the Python installer from Anaconda. Make sure to download 64-bit Python of version 3.5 in this particular case.
- In Origin’s workspace, select Tools:Python console… . Make sure the first line in the console starts with “Python 3.5.2”. In case the first line shows Python 2.7 instead, you need to go to Script Window and run @PYV = 3 to change Python version to 3.5.2 .
- Suppose we want to import NumPy package into Python. By default, the embedded Python doesn’t include NumPy package so that running import numpy in the console gives error message: (see below)
- We need to locate the folder of the external Python that contains NumPy package. For Anaconda, the package library is under folder: C:\Users\username\AppData\Local\Continuum\Anaconda3\Lib\site-packages ( “username” should be changed accordingly ). Go to this fold and make sure NumPy module is installed. Record the path name. Note: The package library location differs for different distributions of Python and you need to find it out yourself.
- Next, we need to add this package path to the search path list of Origin’s embedded Python. To do that, go back to the Python console we launched in step 1, run the following commands one by one to append the path string to the list. Again the PkPath string should be changed accordingly for your own case.
123import sysPkPath = "C:\\Users\\username\\AppData\\Local\\Continuum\\Anaconda3\\Lib\\site-packages"sys.path.append(PkPath)
- Run the following commands to test the installation of NumPy module:
123import numpyx = numpy.array([[1, 2, 3], [4, 5, 6]], numpy.int32)print(x)
NumPy module is now available for use.
- Notice that the the external package library of Anaconda contains many other modules and they are accessible by the embedded Python as well.
- Since Origin supports both Python 3.5 and Python 2.7. In case you are running Python 2.7, make sure the external Python is also of version 2.7. Then the rest procedures are the same.
One last issue is that each time you launch Origin, you have to repeat the whole process to call external packages. One solution is to let Origin automatically add the package path each time it is started. Here are the steps:
- Go to your user file folder by select Help:Open folder:User File Folder.
- Create an .ogs file with a text editor (e.g. Notepad) and enter the following information:
- Browse to your Origin.ini file in your UFF and open this file in your text editor.
- Locate the [Startup] section of the file. If no such section exists, create one. Add the name of the .OGS file you just created using the following form and save it: