简体   繁体   English

sys.path.append不适用于Python 3.x

[英]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 我有Blender 2.66a,这是一个提供Python 3.3 API的应用程序,在我的系统上,我安装了Python 3.2,并提供了几个我希望在Blender中使用的模块,我都尝试了

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 tkinter模块从gnome-terminal运行

If I got you right, you're using Python 3.3 from Blender but try to include the 3.2 standard library. 如果我理解正确,则说明您使用的是Blender中的Python 3.3,但尝试包含3.2标准库。 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). Blender可能提供了使用3.3标准库的方式(与3.2兼容99%)。 Pure-Python third party library can, of course, be included by fiddling with sys.path . 当然,可以通过摆弄sys.path来包含纯Python第三方库。

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. 正如人们在评论中指出的那样,Python 3.3找不到_tkinter扩展模块。 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 ). 尽管它存在(因为它可以在Python 3.2中运行),但是它很可能在带有ABI标签的.so文件中,该标签与Blender的Python 3.3不兼容,因此它甚至都不会看它(很像module.txt不用于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. 扩展模块是高度特定于版本的,轻微的ABI不匹配(例如在3.2和3.3之间,或者两个3.3使用不同的选项编译)可能导致几乎任何类型的错误,从崩溃到内存泄漏到静默数据损坏甚至完全不同。

You can verify whether this is the case via import _tkinter; print(_tkinter.__file__) 您可以通过import _tkinter; print(_tkinter.__file__)验证是否是这种情况import _tkinter; print(_tkinter.__file__) import _tkinter; print(_tkinter.__file__) in the 3.2 shell. 在3.2 Shell中import _tkinter; print(_tkinter.__file__) Alternatively, _tkinter may live in a different directory entirely. 另外, _tkinter可能完全位于另一个目录中。 Adding that directory won't actually fix the real issue outlined above. 添加该目录实际上无法解决上面概述的实际问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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