简体   繁体   中英

sys.path.append doesn't work with Python 3.x

I have Blender 2.66a which is an application that offers Python 3.3 APIs, On my system I have an installation of Python 3.2 with several modules that I wish to use inside Blender, I tried both

sys.path.append(r"/usr/lib/python3.2/")
sys.path.append("/usr/lib/python3.2/")

and this commands gives no errors, infact even the autocomplete feature works and new modules are indexed, so i tried

import tkinter

but this generates the following error

Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
  File "/usr/lib/python3.2/tkinter/__init__.py", line 42, in <module>
    raise ImportError(str(msg) + ', please install the python-tk package')
ImportError: No module named '_tkinter', please install the python-tk package

and I don't get the point of this error because it fails to load a module that it's there asking me to install the same module because that module is not installed ( ? ).

What can cause this obscure problem ?


EDIT

the tkinter module works from the gnome-terminal

If I got you right, you're using Python 3.3 from Blender but try to include the 3.2 standard library. This is bound to give you a flurry of issues, you should not do that. Find another way. It's likely that Blender offers a way to use the 3.3 standard library (and that's 99% compatible with 3.2). Pure-Python third party library can, of course, be included by fiddling with sys.path .

The specific issue you're seeing now is likely caused by the version difference. As people have pointed out in the comments, Python 3.3 doesn't find the _tkinter extension module. Although it is present (as it works from Python 3.2), it is most likely in a .so file with an ABI tag that is incompatible with Blender's Python 3.3, hence it won't even look at it (much like a module.txt is not considered for import module ). This is a good thing. Extension modules are highly version-specific, slight ABI mismatches (such as between 3.2 and 3.3, or two 3.3 compiled with different options) can cause pretty much any kind of error, from crashes to memory leaks to silent data corruption or even something completely different.

You can verify whether this is the case via import _tkinter; print(_tkinter.__file__) import _tkinter; print(_tkinter.__file__) in the 3.2 shell. Alternatively, _tkinter may live in a different directory entirely. Adding that directory won't actually fix the real issue outlined above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM