15
$\begingroup$

When Blender is installed, it creates its own copy of Python 3.x. However, I prefer to code in Python 2.x. This makes it difficult for me to work with newer versions of Blender. I would like to know what to do so that Blender will instead use an external installation of Python 2.x, which I already have installed on my computer.

How do I make Blender recognize the other version of Python?

$\endgroup$
  • $\begingroup$ Maybe you would like to clarify what bit of python 2 is more comfortable than python 3. I know plenty of people who took a long time to step over to py3, but eventually concluded that their original resistance could only be justified in a select set of scenarios (like some modules not being available for py3 - yet) Is it the print() statement? the try-except stuff? or the xrange vs range?. Progress to py3 is inevitable, for many use cases it is faster and has more conveniences. $\endgroup$ – zeffii May 25 '13 at 7:27
  • $\begingroup$ Out of curiosity, can you say why you want to use Python2.x ? $\endgroup$ – ideasman42 May 31 '13 at 15:57
  • $\begingroup$ I use Python 2.x for my work doing desktop application design, and I haven't yet switched over all of my code/packages/IDE to Python 3.x. I'll have to switch over sometime, but until then I would like to use the same version of Python for all of my coding projects. $\endgroup$ – Gwen May 31 '13 at 16:02
  • 7
    $\begingroup$ Python3.x was being release about the same time blender was undergoing a complete api rewrite (which meant we had to rewrite all our scripts anyway), we had had trouble supporting multiple python versions already (2.4x -> 2.6x), so we made the move to a single, bundled Python version - and using Python3.x was a good choice to avoid yet-another rewrite/update later on. $\endgroup$ – ideasman42 May 31 '13 at 16:12
  • $\begingroup$ @Gwenn might want to look into VirtualEnv, so you can have several python installations going on. $\endgroup$ – zeffii Jun 1 '13 at 21:07
29
$\begingroup$

In summary - you can't.

Since Blender 2.5 we've moved on to Python 3.x This means it is not possible to use Python 2.x for scripting in Blender.

Now it's impossible to build against Python 2.x, and even if you did manage by editing the source-code, you'd probably run into problems running Blender, since Blender's own scripts are coded in Python 3.

Setting PYTHONPATH to a non Python 3.3 installation like Python 2.7 will cause Blender to crash and die horribly. (So always make sure you don't have PYTHONPATH set to a non-compatible Python version. But you can use it to point to an external Python 3.3 installation that has extra modules installed that don't come bundled with Blender).

Note: with current Blender pointing to Python 3.1 or Python 3.2 will also fail. So make always sure you point your PYTHONPATH to same version as used in Blender, should you need the external one.

In the long run you'll be better off getting used to Python 3. Also here is the proposal for the switch.

$\endgroup$
  • 4
    $\begingroup$ You could possibly edit your comment - 3.x --> 3.3, Important to note that if you have blender built against 3.3 and point PYTHONPATH to 3.2, 3.1... etc, it will fail. $\endgroup$ – ideasman42 May 24 '13 at 23:15
1
$\begingroup$

You can also use the Python lib2to3 package to automatically convert code directly from Python 2.x to Python 3.x. That way, it is possible to write your code in 2.x, then convert it to a format Blender understands (3.x).

This package comes installed with newer forms of Python under the Tools/scripts directory. You can use it to convert a single file by typing $ 2to3 example.py at the command prompt.

There are a variety of different options built in with this package to make this a quite powerful package. It is described in full, glorious detail at http://docs.python.org/2/library/2to3.html, for those who are interested in learning more.

$\endgroup$
  • 1
    $\begingroup$ it won't help converting blender 2.49- python code to 2.5+ code (just in case anyone thought that). $\endgroup$ – zeffii May 26 '13 at 16:22
  • $\begingroup$ But it will help the OP's purpose of being able to write code in the version they are most comfortable with. $\endgroup$ – Gwen May 26 '13 at 16:28
  • 1
    $\begingroup$ seems like unnecessary stubbornness of the OP to have to resort to 2to3 tools for all python he/she intends to write for Blender 2.5+. You didn't respond to my request to give reasons why you can't write directly in py3. $\endgroup$ – zeffii May 26 '13 at 17:13
0
$\begingroup$

Technically Blender has one originally math-python modules now ported to C++ therefore built-in subroutines, so you can't make the imports even if you replace the working python version as i did next. so you wouldn't import numpy for example in the first attempts.

so i tried really hard and i did. used PYTHONPATH and also the following then blender did run the alternative python but would not load BPY

import sys
addons=r"C:\Program Files\Blender Foundation\Blender\2.67\scripts\addons"
modules=r"C:\Program Files\Blender Foundation\Blender\2.67\scripts\modules"
startup=r"C:\Program Files\Blender Foundation\Blender\2.67\scripts\startup"
lib=r"C:\Program Files\Blender Foundation\Blender\2.67\python\lib"<br/>
DLLs=r"c:\\Program Files\\Blender Foundation\\Blender\\2.67\\python\\DLLs"
packages=r"C:\\Program Files\\Blender Foundation\\Blender\\2.67\\python\\lib\\site-packages"
python=r"C:\\Program Files\\Blender Foundation\\Blender\\2.67\\python"

sys.path.append(addons)
sys.path.append(modules)
sys.path.append(startup)
sys.path.append(lib)
sys.path.append(DLLs)
sys.path.append(packages)
sys.path.append(python)

so i copied BPY.so like this

Linux:

cp ./bin/bpy.so /usr/lib/python3.3/site-packages/
cp --recursive ./bin/2.65 /usr/lib/python3.3/site-packages/

Windows:

copy bin\bpy.pyd C:\Python33\Lib\site-packages\
copy bin\*.dll C:\Python33\Lib\site-packages\
del C:\Python32\Lib\site-packages\python33.dll
xcopy /E bin\2.65 C:\Python33\

and still nothing for import bpy

$\endgroup$
0
$\begingroup$

You can't use python 2 in blender anymore but you can make it use you system python but compiling blender from source. This is especially useful on OSX if you need to use the Framework version of python to prevent it from crashing when do anything with the native Mac GUI.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.