Score:4

Running a Python2 program with Python3?

be flag

EDIT: Updates below, the scenery seems to have changed significantly.


I have Ubuntu 20.04, and have installed Python 3.10 manually. There's python 2 in the system already, and that's what I get if I simply execute python at a command line, rather than python3. However, I only seem to have a version of pip that is specific to python3.

I have a program that I want to run (chirp) which is written in python2. It complains of the lack of a serial library:

$ ./chirpw
Traceback (most recent call last):
  File "./chirpw", line 24, in <module>
    from chirp.drivers import *
  File "/home/simon/chirp-daily-20220103/chirp/drivers/idrp.py", line 16, in <module>
    import serial
ImportError: No module named serial

However, if I attempt to get this module with pip, I end up with pip3 running and get the python3 version of this module.

How should I go about adding a library to python2 specifically (and ensure that the library that's fetched is the python2 library)?

I suppose the "best" way might be to create a virtual environment specifically for this particular program, but I don't know how to do that in the case of a pre-existing program either.

Any pointers gratefully accepted.


EDIT. I found this page: setting up pipenv and virtualenvironments and I seem to have moved forward. I now seem to have an actual virtual environment built around /usr/bin/python (which is python2.7), and using it, pip -install serial worked. However, after doing this, it's now complaining that it can't find the module gtk, and the attempt to load that module fails saying there's no such module:

(venv) :~/chirp-daily-20220103$ python -m pip install gtk
DEPRECATION:[...]
ERROR: Could not find a version that satisfies the requirement gtk (from versions: none)
ERROR: No matching distribution found for gtk

Next I tried the flatpak version of this, but flatpak isn't working for me at all. I'll open a different question on that, since we're now well off my original question's topic.

Thanks to all who tried to help :)

Dave avatar
za flag
Check this answer to add the pip for python 2.7 packages not included in Ubuntu 20.04 https://stackoverflow.com/a/64297311/4576663
be flag
@Dave yes, I did the manual install from the source package, and the "instructions" simply say ./chirpw should work, but it doesn't.
Dave avatar
za flag
Yeah I read through them and then dug through the code and realized it was running the python2 itself. It's the missing pip for python2 giving you a hard time I think, see my above comment for a link for help installing it.
Dave avatar
za flag
"However, after doing this, it's now complaining that it can't find the module gtk," Yes, I've done a lot of pygobject (gtk) development in python and accessing gtk from within a venv comes with it's own nightmares. It's outside my wheel house to help ya get it going within that environment.
Score:1
zw flag

As we already know CHIRP is also provided as Flatpak and this installation method is easier.

But if you still want to proceed with installation from source tar-ball - use below commands:

sudo apt-get update
sudo apt-get install python2 python2-dev python-is-python2

cd ~/Downloads
# three below command will download old packages from 18.04 LTS repository
wget -c http://archive.ubuntu.com/ubuntu/pool/main/p/pyserial/python-serial_3.4-2_all.deb
wget -c http://archive.ubuntu.com/ubuntu/pool/universe/p/pygtk/python-gtk2_2.24.0-5.1ubuntu2_amd64.deb
sudo apt-get install ./python-serial_3.4-2_all.deb ./python-gtk2_2.24.0-5.1ubuntu2_amd64.deb

wget -c https://trac.chirp.danplanet.com/chirp_daily/LATEST/chirp-daily-20220103.tar.gz
tar -xf chirp-daily-20220103.tar.gz
cd chirp-daily-20220103/
sudo python2 setup.py install

and then launch CHIRP from Applications or Brisk menu.

be flag
This is surely great for anyone for whom flatpak works. Sadly flatpak itself fails at a most basic level for for me, and it's unclear why.
Score:0
cn flag

You can run pip via Python by doing python -m pip. That will run the Python 2 version. For example, if python went to Python 2, and you wanted to use the Python 2 version of pip to install the pyserial library, you could do this:

$ python -m pip install pyserial

To understand what that is doing, see the Python manpage

-m module-name
       Searches sys.path for the named module and runs
       the corresponding  .py  file  as  a script.
Dave avatar
za flag
This assumes the OP has pip for python2 installed, if he doesn't, this will not work.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.