How to embed python in an Objective-C OS X application for plugins? -



How to embed python in an Objective-C OS X application for plugins? -

i'm trying utilize python in new os x application plugin scripting. i'm looking offload of programme logic python easy, on-the-fly modification. had started looking @ big nerd ranch tutorial. seemed work, suggested older method of linking python. appears since xcode 5, supposed install python using this mac developer library tech note. process, however, seems create linked python instance, not embedded one. i've tried next guidelines in answer question seems break down.

so question this: current best practices building python utilize plugin runtime in objective c mac os x app? how 1 go making sure bundled application, , possible install additional libraries 1 might want before final objective c app built?

i've come method seems work well.

first, download source of python version want utilize official website. extract archive somewhere. i'm using python 3.4.2. adjust commands on scheme specific version you're using.

create build directory utilize development python version. entire directory should have no spaces in create sure bash interprets she-bang (#!) lines correctly. used /users/myaccount/development/python/devbuild/python3.4.2.

go extracted python directory , run next commands:

./configure --prefix="/users/myaccount/development/python/devbuild/python3.4.2" create make install

this install python in development build directory. setup python path utilize right directory:

export pythonpath="/users/myaccount/development/python/devbuild/python3.4.2/lib/python3.4/site-packages/"

go python bin directory (/users/myaccount/development/python/devbuild/python3.4.2/bin) , utilize pip3 there install modules need. $pythonpath setting ensure modules installed right site-packages directory.

find handy home pyobjc repository , clone there. checkout latest version tag , install it, making sure $pythonpath still correct:

hg clone https://bitbucket.org/ronaldoussoren/pyobjc cd pyobjc hg tags hg checkout [the id of latest version tag list] /users/myaccount/development/python/devbuild/python3.4.2/bin/python3 ./install.py

whenever need update python modules, create sure utilize right python bin , $pythonpath.

now add together python xcode project.

drag /users/myaccount/development/python/devbuild/python3.4.2 directory xcode project, setting not re-create items necessary, , create folder reference.

add /users/myaccount/development/python/devbuild/python3.4.2/include/python3.4m header search paths setting in xcode project's build settings. not sure if there's way generalized step search folder referenced directory had added.

drag `/users/myaccount/development/python/devbuild/python3.4.2/lib/libpython3.4m.a library xcode project, setting added reference without copying well.

the code big nerd ranch scripting tutorial repository can used few modifications.

the plugin manager code need nsstring extension work wchar_t strings python api seems much:

@interface nsstring (wcharencodedstring) - (wchar_t*) getwidestring; @end @implementation nsstring (wcharencodedstring) - (wchar_t*) getwidestring { const char* tmp = [self cstringusingencoding:nsutf8stringencoding]; unsigned long buflen = strlen(tmp) + 1; wchar_t* buffer = malloc(buflen * sizeof(wchar_t)); mbstowcs(buffer, tmp, buflen); homecoming buffer; } @end

the python header should included follows:

#include "python.h"

the next code needs run before py_initialize() called in order set right python executable, pythonpath, , pythonhome suggested zorg on other question.

nsstring* executablepath = [[nsbundle mainbundle] pathforresource:@"python3.4" oftype:nil indirectory:@"python3.4.2/bin"]; py_setprogramname([executablepath getwidestring]); nsstring* pythondirectory = [[nsbundle mainbundle] pathforresource:@"python3.4" oftype:nil indirectory:@"python3.4.2/lib"]; py_setpath([pythondirectory getwidestring]); py_setpythonhome([pythondirectory getwidestring]);

finally, python path needs expanded in pluginexecutor.py file include various subdirectories of high level lib path. add together next code top of plugin executor file:

import sys os import walk path = sys.path.copy() p in path: root,dirs,files in walk(p): if p not root: sys.path.append(root)

i'll post updates if things start break down, seems working solution now.

python objective-c xcode osx cocoa

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -