[Bf-python] PYTHONPATH
Willian Padovani Germano
wgermano at ig.com.br
Sat Sep 13 06:54:55 CEST 2003
From: "Stephen Swaney" <sswaney at swbell.net>
> I was hacking around with this problem a bit, trying to figure out
> why when I run python from the command line, I get the site-package
> stuff, but not in blender.
Fine : ), this is probably enough for accessing those external modules, like
Spe needs, on a system with a default installation.
(...)
> A more permanent fix would be to enhance the init_syspath()
> func in BPY_interface.c down in ../blender/source/blender/python/
> There may be a better way to do this, but I have included
> a first attempt at a patch just for fun.
Looks fine, but it's probably necessary to incref the strings before
appending them to sys.path, look below:
> Index: BPY_interface.c
> ===================================================================
> RCS file:
/cvsroot/bf-blender/blender/source/blender/python/BPY_interface.c,v
> retrieving revision 1.22
> diff -u -r1.22 BPY_interface.c
> --- BPY_interface.c 3 Sep 2003 04:13:08 -0000 1.22
> +++ BPY_interface.c 12 Sep 2003 20:50:13 -0000
> @@ -108,7 +108,7 @@
>
> /* TODO: Shouldn't "blender" be replaced by PACKAGE ?? (config.h) */
> Py_SetProgramName("blender");
> -
> +
> Py_Initialize ();
>
> init_ourImport ();
> @@ -198,6 +198,28 @@
> p = Py_BuildValue("s", U.pythondir);
> syspath_append(p); /* append to module search path */
> }
> +
> + /* sds */
> + /* bring in the site module so we can add site-package dirs to sys.path
*/
> + mod = PyImport_ImportModule("site"); /* new ref */
> + if( mod)
> + {
> + PyObject* item;
> + int size, index ;
> +
> + /* get the value of 'sitedirs' from the module */
> + d = PyModule_GetDict(mod); /* borrowed ref */
> + p = PyDict_GetItemString( d, "sitedirs"); /* borrowed ref */
> +
> + /* append each item in sitedirs list to path */
> + size = PyList_Size( p );
> + for( index = 0; index < size; index++)
> + {
> + item = PyList_GetItem( p, index ); /* borrowed ref */
here: Py_INCREF(item);
(or use PySequence_GetItem, instead of PyList_GetItem)
> + syspath_append( item );
> + }
> + Py_DECREF(mod);
> + }
>
> /* set sys.executable to the Blender exe */
> mod = PyImport_ImportModule("sys"); /* new ref */
>
Thanks, Stephen, this is a welcome addition for 2.29 : ). I'll clean up a
little the mess I'm making with the sources locally and test/apply this
early next week. Of course Michel or Jacques can also apply this, if they
get to try it first.
--
Willian, wgermano at ig.com.br
More information about the Bf-python
mailing list