更改 Inkscape 的 Python 解释器

Changing the Python interpreter for Inkscape

我在使用 Inkscape 时不断收到错误,这些错误似乎暗示着 python 2 vs 3 的期望没有得到满足,尽管我已经安装了它们。例如,当我尝试从模板生成新文档时,我得到

Traceback (most recent call last):
  File "empty_generic.py", line 82, in <module>
    c.affect()
  File "/usr/share/inkscape/extensions/inkex.py", line 285, in affect
    self.output()
  File "/usr/share/inkscape/extensions/inkex.py", line 272, in output
    self.document.write(sys.stdout)
  File "src/lxml/lxml.etree.pyx", line 2033, in lxml.etree._ElementTree.write (src/lxml/lxml.etree.c:63667)
  File "src/lxml/serializer.pxi", line 524, in lxml.etree._tofilelike (src/lxml/lxml.etree.c:134877)
  File "src/lxml/lxml.etree.pyx", line 324, in lxml.etree._ExceptionContext._raise_if_stored (src/lxml/lxml.etree.c:10737)
  File "src/lxml/serializer.pxi", line 441, in lxml.etree._FilelikeWriter.write (src/lxml/lxml.etree.c:133581)
TypeError: write() argument must be str, not bytes

最后一行似乎正是我所说的——通常这个错误是由 运行 python 2 代码和 python 3 解释器引起的,并且可以修复通过简单地将字符串对象 str 作为 str.decode() 或其他东西传递。但是,显然,编辑 inkscape 源代码并不是一个理想的解决方案。

此外,在尝试生成 Voronoi 图时,我得到

Traceback (most recent call last):
  File "voronoi2svg.py", line 36, in <module>
    import simplepath
  File "/usr/share/inkscape/extensions/simplepath.py", line 51
    raise Exception, 'Invalid path data!'
                   ^
SyntaxError: invalid syntax

这又是一个明显的 2 对 3 错误。

有没有办法更改 Inkscape 使用的 python 解释器?

我实际上是从 Inkscape 网站本身找到了问题的答案 here:

If your operating system (e.g. your Linux distro) uses a different default version of Python (or Perl, Ruby, etc.) than what is required by Inkscape extensions, please see Extension Interpreters for how to set the interpreter that Inkscape will use. The most common example of this is that the default Python version of the operating system is 3, but Inkscape requires Python2, resulting in all extensions giving an error.

其中 leads to this page,表示:

Selecting a specific interpreter version (via preferences file) In the preferences.xml file, a user can set a specific executable of the interpreter that Inkscape should use to execute extensions of a specific type.

This is especially useful, if the system's default version of the interpreter is incompatible with the one used by Inkscape's extension subsystem (e.g. Inkscape extensions that rely on inkex.py will only work with Python 2 (as of Inkscape 0.92.1), while on some recent Linux distributions, the default Python version used is Python 3, which results in errors during execution of extensions).

To change the executable that will be used to run script extensions to a different value than the default value in the above table, you need to do the following:

quit all running Inkscape processes Open your perferences.xml file with a text editor (find the exact location of the file by going to Edit -> Preferences -> System: User Preferences) search the group which holds settings for the extension system itself and options of various extensions:

<group
 id="extensions"
 …
 org.ekips.filter.gears.teeth="24"
 org.ekips.filter.gears.pitch="20"
 org.ekips.filter.gears.angle="20" />

Insert a key for the interpreter, for example 'python-interpreter' for setting the program that should be used to run python extensions, and set the string to the absolute path to the python binary which is compatible with Inkscape's current extension scripts (in the example below, the path is "/usr/bin/python2.7". It will look different on Windows systems.):

<group
 id="extensions"
 python-interpreter="/usr/bin/python2.7"
 …
 org.ekips.filter.gears.teeth="24"
 org.ekips.filter.gears.pitch="20"
 org.ekips.filter.gears.angle="20" />

Save the preferences file, and launch Inkscape to test the extensions.

你的回答对我有帮助。但我走了另一条路。使用 pyenv local 2.7.18.config/inkscape/extension 文件夹中设置本地 python。使用它安装任何 python 所需的依赖项并且它有效。

更改 python 解释器的设置对我不起作用。