Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Raspbian run multiple python Versions simultaniously

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?
like image 534
monamona Avatar asked Nov 14 '17 21:11

monamona


5 Answers

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))
like image 149
e.s. Avatar answered Oct 20 '22 00:10

e.s.


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
like image 31
saaj Avatar answered Oct 19 '22 22:10

saaj


From the terminal, run with:

python3.1 your_program.py
like image 39
Joe Iddon Avatar answered Oct 19 '22 22:10

Joe Iddon


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/

like image 34
sanket mokashi Avatar answered Oct 20 '22 00:10

sanket mokashi


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:
like image 34
Byeongguk Gong Avatar answered Oct 19 '22 23:10

Byeongguk Gong