Support Forum

Scripting LayoutEditor in Python with PyCharm on a Windows 10 machine

Paul

Sunday 18th August 2019
I'm trying to use PyCharm to script LayoutEditor in Python on a Windows 10 machine. In PyCharm, I set the interpreter to "C:\Program Files (x86)\LayoutEditor\python\python37\python.exe". In my Python script, the line "import LayoutEditor" fails because the Windows path in "C:\Program Files (x86)\LayoutEditor\python\python37\Lib\site-packages\LayoutEditor\_init.py" is not referenced to the root directory ("dir="Program Files (x86)/LayoutEditor/python" instead of "dir="C:/Program Files (x86)/LayoutEditor/python"). If I manually edit init.py to correct this, it them fails with the error message "ops, there was a problem load LayoutScript for python". The files "LayoutScript.pyd", "LayoutScript.pyw", and "oaxLayoutScript.py" are in "C:\Program Files (x86)\LayoutEditor\python\". Please help, thanks, Paul.
Jürgen
LayoutEditorFull
Monday 19th August 2019
Hi Paul, I don't know PyCharm, but in general some enviorrnments had to be set: * for Windows: * PATH must include the*LayoutEditor/bin/ *folder to be able to find the required dll * PYTHONPATH must include the *LayoutEditor/python* folder to find the LayoutEditor Python module * to use OpenAccess file format PATH must also contain the *LayoutEditor/oa/bin/ ....* folder with the dll file (differ a bit for 32bit and 64 bit systems) * for Linux: * LD_LIBRARY_PATH must include the *LayoutEditor/lib* path and for OpenAccess the *LayoutEditor/oa/lib/* path * PYTHONPATH must include the *LayoutEditor/python* folder to find the LayoutEditor Python module The integrated TextEditor will set these variables automaticly when calling a Python script
Paul

Monday 19th August 2019
Hi Jürgen, I followed your instructions, it works, problem solved! Thanks so much for the quick response, Paul.
Sanket

Friday 10th January 2020
Hi Jürgen, I have been trying to use python scripting for LayoutEditor on MacOSX (MacOS 10.15, Catalina). I downloaded the LayoutEditor free version to try it out, specially the python scripting. However it isn't clear what the PATH, PYTHONPATH and LD_LIBRARY_PATH should be. I tried the settings ``` export LD_LIBRARY_PATH=/Applications/layout.app/Contents/MacOS/lib:$LD_LIBRARY_PATH export PYTHONPATH=/Applications/layout.app/Contents/python:$PYTHONPATH ``` however when I run " import LayoutScript " in my python environment, I get the following error: ``` ----> 1 import LayoutScript /Applications/layout.app/Contents/python/LayoutScript.py in <module> 26 fp.close() 27 return _mod ---> 28 _LayoutScript = swig_import_helper() 29 del swig_import_helper 30 else: /Applications/layout.app/Contents/python/LayoutScript.py in swig_import_helper() 22 if fp is not None: 23 try: ---> 24 _mod = imp.load_module('_LayoutScript', fp, pathname, description) 25 finally: 26 fp.close() ~/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/imp.py in load_module(name, file, filename, details) 241 return load_dynamic(name, filename, opened_file) 242 else: --> 243 return load_dynamic(name, filename, file) 244 elif type_ == PKG_DIRECTORY: 245 return load_package(name, filename) ~/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/imp.py in load_dynamic(name, path, file) 341 spec = importlib.machinery.ModuleSpec( 342 name=name, loader=loader, origin=path) --> 343 return _load(spec) 344 345 else: ImportError: dynamic module does not define module export function (PyInit__LayoutScript) ``` Could you please help me with the right settings to get the scripting utility to work. Thanks, Sanket
Jürgen
LayoutEditorFull
Friday 10th January 2020
Hi Sanket, a Python script like below running with the build in TextEditor will print the correct settings for your system: ```python import os print(os.environ) ``` For a typical Mac system just setting PYTHONPATH would be sufficient ``` export PYTHONPATH=/Applications/layout.app/Contents/python:$PYTHONPATH ``` The Mac version of the LayoutEditor is compiled for Python2.7, that is the Python version included with Mac Os X. It will not run with Python 3.x from a separate installation.