[Bf-python] PYTHONPATH

www.stani.be s_t_a_n_i at yahoo.com
Thu Sep 11 21:17:38 CEST 2003


Hi,

jms <jmsoler at free.fr> wrote:
> All you need :
> http://www.elysiun.com/forum/viewtopic.php?t=7723

I read that thread already before, but for me it is
one more reason to solve this problem inside Blender
itself. So that users don't have to worry about
setting the pythonpath themselves for standard
installations, like Willian said.

--- Willian Padovani Germano <wgermano at ig.com.br>
wrote:
> We can include, probably (and you can, too,
> appending to sys.path in your
> program before trying to reach libs there), but we
> should know the different
> default places based on platform.
The default places are known, see explanation below. 
The problem is that I can't include it in my
application, because it should be started with typing
the following in the textwindow of any blender
drawing:
  import spe
  spe.main()
Of course this code could be extended with a platform
check and appending to a systempath and so on...  But
than it gets to complicated for the user to use spe
from within Blender if he has to learn by heart all
this code.  Also he could open a python file each
time, but that's less user friendly and it's just
moving around the problem, because than where has this
file to be?!


For Python programs the standard for distributing
modules/programs is based on the setup function from
the distutils.core package.  So the paths of these
standard installations should be present in the
internal Blender PYTHONPATH.  What I propose is that
the PYTHONPATH is set inside Blender to default
values, but that in the Blender preferences this can
be altered.  I see that in the Preferences>File Paths
that there is already a Python entry. Is this an entry
for a single path or a list of paths seperated by a
comma or ; ?  Because if this would act as a
Pythonpath variable it should be able to contain
multiple directories.

> This may also cause problems for users that didn't
> install Python in the
> default places, so we have to be careful.

Well, I presume that people who don't install Python
in the default places are more experienced and know
what they are doing. So than they can adjust in the
preferences of Blender the Pythonpath as well.  But
even in Python is not installed in the default places,
it is possible to find out the path.  I dove a bit in
the distutils module to find out how the
distutils.core.setup function can now where Python is
installed.  I found the answer in sysconfig.py file of
the distutils package. The function is:

    PREFIX = os.path.normpath(sys.prefix)
    EXEC_PREFIX = os.path.normpath(sys.exec_prefix)

    def get_python_lib(plat_specific=0,
standard_lib=0, prefix=None):
        """Return the directory containing the Python
library (standard or
        site additions).

        If 'plat_specific' is true, return the
directory containing
        platform-specific modules, i.e. any module
from a non-pure-Python
        module distribution; otherwise, return the
platform-shared library
        directory.  If 'standard_lib' is true, return
the directory
        containing standard Python library modules;
otherwise, return the
        directory for site-specific modules.

        If 'prefix' is supplied, use it instead of
sys.prefix or
        sys.exec_prefix -- i.e., ignore
'plat_specific'.
        """
        if prefix is None:
            prefix = plat_specific and EXEC_PREFIX or
PREFIX

        if os.name == "posix":
            libpython = os.path.join(prefix,
                                     "lib", "python" +
sys.version[:3])
            if standard_lib:
                return libpython
            else:
                return os.path.join(libpython,
"site-packages")

        elif os.name == "nt":
            if standard_lib:
                return os.path.join(prefix, "Lib")
            else:
                if sys.version < "2.2":
                    return prefix
                else:
                    return os.path.join(PREFIX, "Lib",
"site-packages")

        elif os.name == "mac":
            if plat_specific:
                if standard_lib:
                    return os.path.join(prefix, "Lib",
"lib-dynload")
                else:
                    return os.path.join(prefix, "Lib",
"site-packages")
            else:
                if standard_lib:
                    return os.path.join(prefix, "Lib")
                else:
                    return os.path.join(prefix, "Lib",
"site-packages")
        else:
            raise DistutilsPlatformError(
                "I don't know where Python installs
its library "
                "on platform '%s'" % os.name)

So this is how it works.  Is it not possible to
incorparate this in the Blender code.  This SHOULD
give the right paths, independent of where somebody
installed Python.

Of course this is only relevant for people who
installed the full Python installation (most Linux
versions come with it anyway).  But with the function
it could be possible to get the name of the path. 
Blender could check if the path exists and if it does
append it to the Pythonpath.  If the path doesn't
exist the user might have no full Python installation,
in which case it is fine, or a very 'weird' one :-),
so than he can take of it himself by filling it in the
preferences dialog box.

An important remark: the problem of the PYTHONPATH
doesn't occur on Windows XP or Windows 2000. So it
reduces the problem to Linux or maybe other unix based
systems. In Windows inside Blender:
  import sys
  print sys.path
gives:
['C:\\Python22\\Lib\\site-packages\\Pythonwin',
'C:\\Python22\\Lib\\site-packages\\win32',
'C:\\Python22\\Lib\\site-packages\\win32\\lib',
'C:\\Python22\\Lib\\site-packages',
'C:\\Python22\\Lib', 'C:\\Python22\\DLLs',
'C:\\Python22\\Lib\\lib-tk', '', 'C:\\Program
Files\\Blender Foundation\\Blender-2.28a',
'C:\\PROGRA~1\\BLENDE~1\\BLENDE~1.28A']

Hope we are getting further on this,
Stani

http://spe.pycs.net

PS I would regret if Blender doens't integrate well
with the full python installation, because that's why
Blender scripting excels it commercial rivals
(maxscript or mel are just a laugh in comparison to
Python). It's a pitty that Python has to be launched
inside Blender and that Blender can not be launched
from Python.  Will that one time be possible? 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com



More information about the Bf-python mailing list