[Bf-python] PYTHONPATH
Stephen Swaney
sswaney at swbell.net
Fri Sep 12 23:13:03 CEST 2003
Willian Padovani Germano wrote:
>
> Thanks for finding / sharing with us the info about how to set the python
> path, we'll look at how to improve the situation, maybe not for 2.29, but
> we'll surely work on this.
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. There are some comments in the site.py
file about how the site module is automagically imported in python
versions > 1.5. I guess we don't get it in blender because we
do all the python init stuff ourselves.
One quick hack-around is to insert something like the following
at the beginning of a script:
import sys
try:
import site
sys.path += site.sitedirs
except:
pass
# rest of init stuff here
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.
--
Stephen Swaney
sswaney at swbell.net
-------------- next part --------------
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 */
+ syspath_append( item );
+ }
+ Py_DECREF(mod);
+ }
/* set sys.executable to the Blender exe */
mod = PyImport_ImportModule("sys"); /* new ref */
More information about the Bf-python
mailing list