On Raspbian run multiple python Versions simultaniously
Asked Answered
R

7

5

I have some old programs from back in the times when python 3.1 came out. In the program I often used the Callable() to pass a function and it's parameters like this to my TKinter application:

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

Now I wanted to use my programs again but the callable- Object was gone. In the web I found that this functionality was removed in python 3.2, and none of the alternatives worked for me.

Finally I decided to reinstall python 3.1. However, I have no idea if it is possible to have multiple python 3 versions installed at the same time or how to 'create' a shell command for this version when I want to use this special version.

My questions are:

  • Is there a replacement for the Callable- Object that was removed?
  • How can I use multiple python versions at the same time?
  • How can I create a matching shell command?
Reimpression answered 14/11, 2017 at 21:5 Comment(1)
Btw: I'm on Raspbian with a RPi 3Reimpression
E
1

It does not seem that Python ever had Callable built in. You may have confused it with callable predicate which indeed was removed and then brought back:

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

All references of Callable on the web that I could find point to 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__

You can also look at answers to this question, where OP wanted to reimplement Callable for the same purpose.

Installing Python 3.1

If you still want to try to install the old version of Python 3, you can give the following a try. I assume that Raspbian is Debian-based distro and the same commands apply. Here's the Dockerfile that verifies that you can do it on Debian Jessie-compatibe system. You can try the RUN commands from it in your shell:

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
Eurhythmy answered 30/11, 2017 at 10:34 Comment(0)
R
2

Callable looks suspiciously like functools.partial.

Here's partial at work. when i run:

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)))

I get:

5
7
9
11
13

You should be able to do:

from functools import partial
tvf.mi(datei_bu, text=datei_opt, command=partial(exec_datei_opts, datei_opt))
Renaissance answered 30/11, 2017 at 7:41 Comment(0)
E
1

From the terminal, run with:

python3.1 your_program.py
Equilibrium answered 14/11, 2017 at 21:9 Comment(2)
/usr/bin/python /usr/bin/python3.4 /usr/bin/python3-config /usr/bin/python2 /usr/bin/python3.4-config /usr/bin/python3m /usr/bin/python2.7 /usr/bin/python3.4m /usr/bin/python3m-config /usr/bin/python3 /usr/bin/python3.4m-configReimpression
WOW! I was told that the package python3.1 is retarded and no longer available, I should use python 3.5 X(Reimpression
S
1

If python 3.1 is unavailable, as seems to be the case from the comments on @JoeIddon answer, you may have to alter your code base. In this case, you can:

Monkey patching Callable with functools.partial

has the advantage of avoiding multiple code modifications.

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

Alternatively:

replacing Callable() with lambda works, but carries the burden of multiple code alterations.

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

replace with:

tvf.mi(datei_bu, text=datei_opt, command=lambda x=datei_opt: exec_datei_opts(x))
Snarl answered 30/11, 2017 at 6:47 Comment(0)
G
1

In terminal , give the path where python 3.1 is installed like this:

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

Alternatively you can try to create virtual environment and activate it then you can run the given script here is the helper link: http://docs.python-guide.org/en/latest/dev/virtualenvs/

Grendel answered 30/11, 2017 at 7:11 Comment(0)
D
1

Using update-alternatives command. It can help you use python flexibly.

Here's the sample code.

$ 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:
Doukhobor answered 30/11, 2017 at 7:18 Comment(0)
E
1

It does not seem that Python ever had Callable built in. You may have confused it with callable predicate which indeed was removed and then brought back:

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

All references of Callable on the web that I could find point to 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__

You can also look at answers to this question, where OP wanted to reimplement Callable for the same purpose.

Installing Python 3.1

If you still want to try to install the old version of Python 3, you can give the following a try. I assume that Raspbian is Debian-based distro and the same commands apply. Here's the Dockerfile that verifies that you can do it on Debian Jessie-compatibe system. You can try the RUN commands from it in your shell:

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
Eurhythmy answered 30/11, 2017 at 10:34 Comment(0)
R
0

Finally I got a answer to all of my questions. I used a lot of stuff from the answers here in order to solve all my problems. Here they come:

1) Is there a replacement for the callable object that is gone?

Basically no. At least none that is working in this context. I will explain why this does not affect my solution.

I made the mistake to just rip out the code out of my TKinter-application without watching my include-dependencies. The anwer of saaj made me rethink if had ever used swampy, and indeed I did. I even found the german equivalent ("Programmieren lernen mit python") in my bookshelf. Silly me! X}

I had my GUI components defined in different files, and I just imported them in the main.py, my main application.

I was not very advanced in programming so I didn't know that e.g.:

sub.py: (was written long before 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)

This constellation lead to the behaviour that when I wrote main.py, I didn't have to say THISISTHEFORGOTTENMODULE.SomeSpeci.... If I would have written this, I would have known instantly what I would have to import in my new program. When I recently saw this code I thought Callable was from the standard library. Unfortunately, there existed a function only different in one character (major C instead of minor c) in python before and was removed. This mislead me to seach for alternatives. Stuff like functools.partial (credits to e.s.), would have worked in some situations, thanks for the knowledge, but it didn't work quite a few times.

When I finally found a from swampy import * in one of the submodules, I was angry at myself for stepping into this tripwire.(Ironically I was the one that resolved exactly that issue).

Credits in this part to: saaj, e.s., and myself(for the research stuff)

2)+3) How to install multiple python versions at the same time?

Well, I had a look at all suggestions and for me, the update-alternatives worked out best. I managed to install python3.1 with the suggestions of:

  • sanket mokashi (The virtual environement suggestion)
  • saaj (The dockerfile stuff worked out as it should)
  • Byeongguk Gong (That was the most comfortable way of doing this!)

I just mention this here because it is part of the question, but I didn't need this anymore, I'll just leave the answer here for anyone that passed by and needs it.


At the end of writing all this down in a summary of my results, I want to thank all guys who helped me out with this issue.

Reimpression answered 1/12, 2017 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.