同时在 Raspbian 运行 多个 python 版本

On Raspbian run multiple python Versions simultaniously

python3.1 刚出来的时候,我有一些旧程序。 在程序中,我经常使用 Callable() 将函数及其参数传递给我的 TKinter 应用程序:

tvf.mi(datei_bu, text=datei_opt, command=Callable(exec_datei_opts, datei_opt))

现在我想再次使用我的程序,但是 callable- 对象不见了。在网络上,我发现此功能已在 python 3.2 中删除,none 的替代方案对我有用。

最后我决定重新安装 python 3.1。但是,我不知道是否可以同时安装多个 python 3 版本,或者当我想使用这个特殊版本时如何为这个版本 'create' 一个 shell 命令版本。

我的问题是:

来自 terminal、运行 与:

python3.1 your_program.py

如果 python 3.1 不可用,就像@JoeIddon 回答的评论中的情况一样,您可能必须更改您的代码库。在这种情况下,您可以:

Monkey 补丁可调用 functools.partial

具有避免多次代码修改的优点。

from functools import partial
Command = partial
tvf.mi(datei_bu, text=datei_opt, command=Command(exec_datei_opts, datei_opt))

或者:

用 lambda 替换 Callable() 可行,但会带来多次代码更改的负担。

tvf.mi(datei_bu, text=datei_opt, command=Callable(exec_datei_opts, datei_opt))

替换为:

tvf.mi(datei_bu, text=datei_opt, command=lambda x=datei_opt: exec_datei_opts(x))

在终端中,给出安装 python 3.1 的路径,如下所示:

/<python3.1 folder>/bin/python  filename.py

或者您可以尝试创建虚拟环境并激活它 那么你可以 运行 给定的脚本 这是助手 link:http://docs.python-guide.org/en/latest/dev/virtualenvs/

使用update-alternatives 命令。它可以帮助您灵活使用python。

这是示例代码。

$ sudo update-alternatives --list python3
update-alternatives: error: no alternatives for python

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.4 1
update-alternatives: using /usr/bin/python3.4 to provide /usr/bin/python3 (python3) in auto mode
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 2
update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python3 (python3) in auto mode

$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   2         auto mode
  1            /usr/bin/python3.4   1         manual mode
  2            /usr/bin/python3.5   2         manual mode

Press enter to keep the current choice[*], or type selection number:

Callable 看起来很像 functools.partial

这是部分工作。当我 运行:

from functools import partial
from operator import mul

def do_stuff(num, command):
    return num + command()

for y in range(5):
    print(do_stuff(5, partial(mul, y, 2)))

我得到:

5
7
9
11
13

你应该可以做到:

from functools import partial
tvf.mi(datei_bu, text=datei_opt, command=partial(exec_datei_opts, datei_opt))

似乎 Python 没有内置 Callable。您可能将它与 callable predicate 混淆了,后者确实是删除然后带回来:

New in version 3.2: This function was first removed in Python 3.0 and then brought back in Python 3.2.

网络上我能找到的所有 Callable 的引用都指向 swampy package (a by-product of Think Python book), which has swampy.Gui.Callable:

class Callable(object):
    """Wrap a function and its arguments in a callable object.
    Callables can can be passed as a callback parameter and invoked later.
    This code is adapted from the Python Cookbook 9.1, page 302,
    with one change: if call is invoked with args and kwds, they
    are added to the args and kwds stored in the Callable.
    """
    def __init__(self, func, *args, **kwds):
        self.func = func
        self.args = args
        self.kwds = kwds

    def __call__(self, *args, **kwds):
        d = dict(self.kwds)
        d.update(kwds)
        return self.func(*self.args+args, **d)

    def __str__(self):
        return self.func.__name__

您还可以查看 的答案,其中 OP 希望出于相同目的重新实现 Callable

正在安装 Python 3.1

如果你还想尝试安装旧版本Python3,你可以试试下面的方法。我假设 Raspbian 是基于 Debian 的发行版并且适用相同的命令。这是验证您可以在 Debian Jessie 兼容系统上执行此操作的 Dockerfile。您可以在 shell:

中尝试使用 RUN 命令
FROM debian:jessie

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install --no-install-recommends --yes software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    # The PPA is designed for Ubuntu, but the trick makes it work
    # because Debian Jessie is compatible with Ubuntu Trusty #
    # on package-level
    sed -i 's/jessie/trusty/g' \
        /etc/apt/sources.list.d/deadsnakes-ppa-jessie.list && \
    apt-get update && \
    apt-get install --no-install-recommends --yes python3.1

CMD python3.1

终于我得到了所有问题的答案。为了解决我所有的问题,我使用了这里答案中的很多东西。他们来了:

1) 是否有替代已消失的可调用对象?

基本没有。至少 none 在这种情况下有效。我将解释为什么这不会影响我的解决方案。

我犯了一个错误,只是从我的 TKinter 应用程序中删除了代码,而没有查看我的 include 依赖项。 saaj 的回答让我重新思考是否曾经使用过 swampy,事实上我确实做到了。我什至在我的书架上找到了对应的德语 ("Programmieren lernen mit python")。傻我! X}

我在不同的文件中定义了我的 GUI 组件,我只是将它们导入到我的主应用程序 main.py 中。

我在编程方面不是很先进,所以我不知道例如:

sub.py:(写在main.py之前)

import THISISTHEFORGOTTENMODULE

def subfoo(temp=0):
    ... #some Code in which Functions from THISISTHEFORGOTTENMODULE were used

main.py:

import sub

subfoo()
temp = SuperSpecialFunctionFromForgottenModule()
subfoo(temp)

这个星座导致我写main.py的时候不用说THISISTHEFORGOTTENMODULE.SomeSpeci...的行为。如果我写了这个,我会立即知道我必须在我的新程序中导入什么。当我最近看到这段代码时,我认为 Callable 来自标准库。不幸的是,之前 python 中存在一个功能仅在一个字符上不同(主要 C 而不是次要 c),并且被删除了。这误导我寻找替代品。像 functools.partial 这样的东西(归功于 e.s),在某些情况下会起作用,感谢您的知识,但它并没有起作用很多次。

当我最终在其中一个子模块中找到 from swampy import * 时,我对自己进入 tripwire 感到愤怒。(具有讽刺意味的是,我正是那个问题的解决者)。

这部分致谢:saaj,e.s,和我自己(用于研究)

2)+3) 如何同时安装多个python版本?

好吧,我查看了所有建议,对我来说,update-alternatives 效果最好。我根据以下建议成功安装了 python3.1:

  • sanket mokashi(虚拟环境建议)
  • saaj(dockerfile 的东西按预期运行)
  • Byeongguk Gong(这是最舒服的做法!)

我只是在这里提到这个,因为它是问题的一部分,但我不再需要这个了,我只是把答案留给路过并需要它的人。


在我的结果总结中写下所有这些内容的最后,我要感谢所有帮助我解决这个问题的人。