27

When I try:

sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next

in Ubuntu, the terminal show this error message:

Traceback (most recent call last):
 File "/usr/bin/add-apt-repository", line 11, in <module>
    from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
  File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 27, in <module>
    import apt_pkg
ImportError: No module named 'apt_pkg'

I have two version of python, one is 2.7 the other is 3.5 .

How can I install this package on python3.5?

Philip
  • 323
  • 3
  • 13
ding
  • 271
  • 1
  • 3
  • 4
  • Why would you want to install the package for Python 3.5? Most likely it's an Ubuntu specific system package, which comes bundled with the default system Python (which is 2.7.x) – Eli Korvigo Feb 22 '17 at 08:50
  • Actually you are not installing anything, you only add a ppa to the system. To me this looks like a bug in `/usr/bin/add-apt-repository` (which is a Python3 script). – Jan Groth Feb 25 '18 at 01:48
  • Does this answer your question? [python-dev installation error: ImportError: No module named apt\_pkg](https://stackoverflow.com/questions/13708180/python-dev-installation-error-importerror-no-module-named-apt-pkg) – Jamie Lindsey Dec 26 '19 at 08:12

8 Answers8

45

I had something quite different than this. Mine failed with

No module named 'softwareproperties'

My solution is:

sudo vim /usr/bin/add-apt-repository
Change package header from `python3` to `python3.4` (or lower)

This may happen when you recently upgraded or added another python3 package.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Yonatan Kiron
  • 2,718
  • 1
  • 20
  • 25
  • 9
    I had to edit it to `python3.5` on Ubuntu 16.04 and it worked. Thanks . – Or Duan Aug 10 '17 at 12:48
  • Manually editing package headers of system scripts didn't work for me on 16.04 and seems error-prone in the first place. To me this looks like a bug in `/usr/bin/add-apt-repository`. I ended up manually adding the ppa (and importing the key). – Jan Groth Feb 25 '18 at 01:51
  • @jangroth I totally agree, add your solution as an answer :) – Yonatan Kiron Mar 12 '18 at 07:15
  • It's just point to the correct version of python. Hard is to find if you have many installed – absentia May 23 '19 at 22:14
  • tks, changing hashbang to python3.5, but changing to python3.4 works! – Dee Jul 11 '19 at 09:42
  • if you are brave enough, you can run `sudo rpl -w "python3" "python$(python3 --version | awk '{print $2}')" /usr/bin/add-apt-repository` to replace package header with current python version. – Mark Kahn Jul 21 '21 at 07:36
  • I decided to isolate the issue and try with different python versions `3.9` `3.8` `3.7` `3.6` The one that worked for me `#!/usr/bin/python3.6` – Andre Leon Rangel Aug 06 '21 at 03:13
15

ubuntu 18.04:

sudo vim /usr/bin/add-apt-repository

Change package header from python3 to python3.6

it's work for me

Logemann
  • 2,767
  • 33
  • 53
Serafim
  • 353
  • 3
  • 10
9

Just chiming in since the version numbers may have changed.

Summary of fix: pointing my python3 to python 3.5 instead of 3.6

cd /usr/bin
rm python3
ln -s python3.5 python3

Detail: I had this same problem and found that /usr/bin/add-apt-repository had a shebang hint to use /usr/bin/python3 which is pointing to 3.6. However I also have 3.5 on my system and have to toggle them often, so I toggled it back and add-apt-repository now works.

Yonatan Kiron
  • 2,718
  • 1
  • 20
  • 25
Philip
  • 323
  • 3
  • 13
  • Worked for me on WSL Ubunutu 16.04.4. I was having the same error as the OP. – TurboGus Jul 19 '18 at 19:10
  • Glad this helped. Given the supposed backwards compatibility of 3.6/3.5, it's weird to me that I used to have issues where Ubuntu wouldn't work properly with 3.6. Notably, my Terminal windows wouldn't start up until I toggled back to 3.5. In current Ubuntu I think 3.6 is now standard. – Philip Jan 30 '19 at 21:11
0

I had the same issue with Ubuntu 20.04 and python3 pointing to python3.6.

I don't like to edit system file or similar (eg. /usr/bin/add-apt-repository) and change symlinks of certain commands (python is quite "special"). I suggest to follow the Ubuntu way to manage alternative versions of same command.

I had python3 pointing to python3.6 but python3.8 installed too so I updated alternatives to make coexist 3.6 and 3.8.

My proposed solution is to make python3 pointing to last installed version you have using thee "Ubuntu way" managing alternative.

  1. Install last python3 version available. Mabe you have it or maybe you don't.
sudo apt update && sudo apt upgrade && sudo apt install python3
  1. Check now which is your version of python3
python3 --version
  1. If it pointing to last (eg. 3.8), you should solved your problem. If python3 is still pointing to a lower version (eg. 3.6), check where last is...
which python3.8
  1. Now managing python 3 alternatives. Check if alternatives about python3 exists.
sudo update-alternatives --config python3
  1. If some are listed, check if last version installed is in and if it is choose it and you should solve your problem. Otherwise remember the highest value of Priority. Ubuntu will use the alternative with highest value. Exit typing Enter button.

  2. Add last version installed as alternative. Here I use the dicimal part of version as priority. The command need python3

sudo update-alternatives /usr/bin/python3 python3 /usr/bin/python3.8 8
  1. If you need, add the others version of python3... (lower priority!!!) Now your python3 is pointing to last version available in "Ubuntu style". (and the problem should be solved).
Lorenzo Eccher
  • 301
  • 2
  • 4
0

IMO, there is a neater solution to bypass the modification of a system file: sudo apt-get install --reinstall python3-apt.

Kassi
  • 151
  • 2
  • 12
0

If you are using UBUNTU, change to python3.8 version or install it

sudo apt install python3.8-dev

and after run

sudo apt-get install software-properties-common
Ruan Nawe
  • 363
  • 6
  • 9
0

I have found that everything that contains

ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done

Is a Python issue. To resolve it, one must set the correct python version. It depends on many factors what "correct version" means

Check python versions in the system

sudo apt list --installed | grep python*

Find what is at /usr/bin

ls -lha /usr/bin/python*
  • Locate the Python file that is generating the error
Traceback (most recent call last):
  File "/usr/BAR/FOO # Is a python file and it varies
  # For the case in this question the file is at
  # /usr/bin/add-apt-repository
  • Open the file with sudo and add the "correct python version"
#!/usr/bin/python3.6

# Above should be the first line of the file
Andre Leon Rangel
  • 1,659
  • 1
  • 16
  • 28
0

This worked for me :

sudo apt-get install python3-apt --reinstall
cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so
Martin Valgur
  • 5,793
  • 1
  • 33
  • 45
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 29 '22 at 16:27