[Bf-python] sitedirs and Python 2.4
Ken Hughes
khughes at pacific.edu
Sun Oct 30 07:24:06 CET 2005
Jonathan Merritt wrote:
> Hi Ken,
>
> I should have read your email more carefully. If I do the following
> within Blender:
> >>> import site
> >>> site.main()
> >>> import sys
> >>> print sys.path
> then the site-packages directories *do* appear. Without calling
> site.main(), however, they don't appear:
> >>> import site
> >>> import sys
> >>> print sys.path
> So, your patch will work at my end.
>
> I still don't understand it though.
>
> The source for site.py looks like it calls main() from the top level of
> the file when it's imported. Why should main() need to be called
> separately? Is this a known difference in behaviour in the embedded
> interpreter compared with the normal command-line version?
>
> Jonathan.
>
> Jonathan Merritt wrote:
>
>> The wierd thing is that when I do an:
>> >>> import sys
>> >>> print sys.path
>> from the native Python command-line interpreter, the site-packages
>> directories are all present. However, when I do the same thing from
>> within Blender it doesn't include the site-packages! They still
>> aren't present even if I first manually "import site" within Blender,
>> although "import site" raises no exceptions.
Here's what I can tell you from what I understand, but for any history
of why things are done this way we'll have to ask Stephen and Willian.
The internal interpreter relies on Py_Initialize() to load the sys and
site modules. The site module calculates the site-packages; how this is
done changed between Python 2.3 and 2.4. Python 2.3 built a list named
"sitedirs" which the Blender interpreter accessed and appended to
sys.path. Python 2.4 now instead calculates what site-package
directories to add to sys.path using a function addsitepackages().
I've played around some more this evening and first found that I could
also add SOME of the site-packages to sys.path by getting a PyObject *
for site.addsitepackages() and calling with PyObject_CallFunction().
But after I read the documentation on Py_Initialize() in the Python/C
API Reference Manual:
"The basic initialization function is Py_Initialize(). This
initializes the table of loaded modules, and creates the
fundamental modules __builtin__, __main__, sys, and exceptions.
It also initializes the module search path (sys.path)."
From this I tried *not* setting the system path after calling
Py_Initialize() (there's a call to PySys_SetPath() in init_syspath). I
ended up with exactly the same site-packages in my sys.path as the
python2.4 interpreter gave me (of course, the complete sys.path was
different since Blender includes paths to its script directories).
Bottom line; maybe we're doing too much in our initialization code?
Ken
More information about the Bf-python
mailing list