简体   繁体   中英

python add-apt-repository: ImportError: No module named 'apt_pkg'

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?

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.

ubuntu 18.04:

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

Change package header from python3 to python3.6

it's work for me

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.

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

IMO,有一个更简洁的解决方案来绕过系统文件的修改: sudo apt-get install --reinstall python3-apt

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM