What is happening with my python and terminal on ubuntu? - python-3.x

I utilize Ubuntu 20.4 and python3.10, but when I run:
sudo apt-get update or sudo apt update
my terminal show me this code:
Traceback (most recent call last):
File "/usr/lib/cnf-update-db", line 8, in <module>
from CommandNotFou/?nd.db.creator import DbCreator
File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 12, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code
When I run: sudo apt-get upgrade no problems happen.
Detail my python3.8 and python3.10 run normally
I don't know what is this.

I ran into same problem today. I did some search via google. Per https://www.linuxquestions.org/questions/linux-software-2/errors-trying-to-run-apt-get-update-4175665017/#3, finally my problem is fixed by followings:
cd /usr/lib/python3/dist-packages
ls -la apt.pkg.*
if apt.pkg.so is no there, copy the one to there.
sudo cp apt_pkg.cpython-38-x86_64-linux-gnu.so apt_pkg.so

Related

Can't do additional updates using sudo apt update error : Problem executing scripts

I am using Ubuntu v20.04.5 LTS for Windows 11. While trying to run sudo apt update.
I get the following error
Hit:5 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Fetched 114 kB in 2s (58.3 kB/s)
Traceback (most recent call last):
File "/usr/lib/cnf-update-db", line 8, in <module>
from CommandNotFound.db.creator import DbCreator
File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 12, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code.
I've tried re-installing python 3
sudo apt-get install python3-apt --reinstall
Then created a symlink from apt_pkg 34m to apt_pkg 35m & apt_pkg35m to apt_pkg36m
cd /usr/lib/python3/dist-packages
sudo ln -s apt_pkg.cpython-{34m,35m}-x86_64-linux-gnu.so apt_pkg.so
sudo ln -s apt_pkg.cpython-{35m,36m}-x86_64-linux-gnu.so apt_pkg.so
I'm not quite sure executing the above commands helped out on anything. Or I am totally off the track in solving this one. I don't seem to understand why this is happening. Does anyone who might have a rough idea of where the problem is?

python poetry how to install for all users ubuntu 18.04?

I am trying to install poetry for all users on ubuntu 18.04 and it doesn't seem to work.
I have tried the script (which installs to a local user's home directory)
$ curl -sSL https://install.python-poetry.org | python3 -
# this works but is only installed for the current user
and I've tried pip:
$ pip install poetry
$ poetry
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I also tried manually moving the poetry binary into /usr/local/bin but there are library dependencies that are not available.
this is how I solved it:
# copy poetry bin and lib to /usr/local/bin and /usr/local/lib
$ cd ~/.local/lib/python3.8/site-packages
$ sudo cp -R ./poetry-1.1.13.dist-info/ /usr/local/lib/python3.8/dist-packages/
$ sudo cp -R ./poetry_core-1.0.8.dist-info/ /usr/local/lib/python3.8/dist-packages/
$ sudo cp -a . /usr/local/lib/python3.8/dist-packages/
$ cd ~/.local/bin
$ sudo cp poetry /usr/local/bin/
$ sudo chmod -R 775 /usr/local/lib/python3.8/dist-packages/

Can't add repository to install from on Ubuntu 20.04 LTS when switching from Python 3.8 to 3.7?

I am trying to install an application and as mentioned in the manual, I am trying to add it's repository without luck:
sudo add-apt-repository ppa:linuxuprising/shutter
And here is the error I am getting:
Traceback (most recent call last):
File "/usr/bin/add-apt-repository", line 12, in <module>
from softwareproperties.SoftwareProperties import SoftwareProperties, shortcut_handler
File "/usr/lib/python3/dist-packages/softwareproperties/SoftwareProperties.py", line 28, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I have tried to reinstall using this solution:
sudo apt remove python3-apt
sudo apt autoremove
sudo apt autoclean
sudo apt install python3-apt
Still get the same error on my Ubuntu 20.04 LTS
Python version is: 3.7.9
I must say that it work when I switch to python 3.8 using update-alternatives.
Maybe this can help you to guide me what should I change.
You can add the repository manually (working around the add-apt-repository script problem):
Import the gpg key:
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com EA8CACC073C3DB2A
Add the ppa:
printf "%s\n" "deb http://ppa.launchpad.net/linuxuprising/shutter/ubuntu focal main" |\
sudo tee /etc/apt/sources.list.d/linuxuprising-shutter-focal.list
Then run:
sudo apt update
Technical details: Linux Uprising team (check the gpg key)
In order to install shutter in ubuntu-20 just do:
sudo snap install shutter
It installs all packages needed in box without interferences with other software.

ModuleNotFoundError: No module named '_sqlite3'

On Redhat 4.4.7-18 I am trying to run python3 code using sqlite, but I get the following import error:
Traceback (most recent call last):
File "database.py", line 7, in <module>
import sqlite3
File "/usr/local/lib/python3.6/sqlite3/__init__.py", line 23, in <module>
from sqlite3.dbapi2 import *
File "/usr/local/lib/python3.6/sqlite3/dbapi2.py", line 27, in <module>
from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
I tried to install it:
>sudo pip install sqlite3
Collecting sqlite3
Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ProtocolError('Connection aborted.', error(101, 'Network is unreachable'))': /simple/sqlite3/
(while the network is reachable...) and with the following command:
> sudo yum install sqlite-devel
Loaded plugins: post-transaction-actions, product-id, refresh-packagekit,
: rhnplugin, search-disabled-repos, security, subscription-manager
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Install Process
Package sqlite-devel-3.6.20-1.el6_7.2.x86_64 already installed and latest version
Nothing to do
So it is installed and not installed? Any suggestion how I can solve the original problem?
Not a direct answer but I ended up here with my search engine... So for my fellow web-surfers:
I had a similar issue, but on ubuntu 16.04 with a manually compile python3.6 version :
from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
I had to install libsqlite3-dev (sudo apt install libsqlite3-dev) and compile from the start python3.6 to make it work.
Yep.
sudo yum install sqlite-devel
Followed by rebuild of Python 3.8.3 from source did the trick. Thx!
I had this issue on linux mint 20 after sqlite3 successfully installed
from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
also, could not import sqlite3 into python interpreter
Fix:
sudo apt install libsqlite3-dev
cd your python installer directory
./configure
sudo make install
It happens when you install Python using the source code and then compile.
Usually people tend to install python and sqlite into separate folders. This causes sqlite binary inaccessible to python executables.
To solve this problem, use a common folder to install BOTH, the Python as well as sqlite. So that python binary and sqlite binary, reside in the same bin folder.
Below example script will solve:
INSTALL_BASE_PATH="/home/<user>/<tools>"
cd ~
mkdir build
cd build
[ -f Python-3.9.10.tgz ] || wget --no-check-certificate https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
tar -zxvf Python-3.9.10.tgz
[ -f sqlite-autoconf-3380000.tar.gz ] || wget --no-check-certificate https://www.sqlite.org/2022/sqlite-autoconf-3380000.tar.gz
tar -zxvf sqlite-autoconf-3380000.tar.gz
cd sqlite-autoconf-3380000
./configure --prefix=${INSTALL_BASE_PATH}
make
make install
cd ../Python-3.9.10
LD_RUN_PATH=${INSTALL_BASE_PATH}/lib configure
LDFLAGS="-L ${INSTALL_BASE_PATH}/lib"
CPPFLAGS="-I ${INSTALL_BASE_PATH}/include"
LD_RUN_PATH=${INSTALL_BASE_PATH}/lib make
./configure --prefix=${INSTALL_BASE_PATH}
make
make install
cd ~
LINE_TO_ADD="export PATH=${INSTALL_BASE_PATH}/bin:\$PATH"
if grep -q -v "${LINE_TO_ADD}" $HOME/.bashrc; then echo "${LINE_TO_ADD}" >> $HOME/.bashrc; fi
LINE_TO_ADD="export LD_LIBRARY_PATH=${INSTALL_BASE_PATH}/lib"
if grep -q -v "${LINE_TO_ADD}" $HOME/.bashrc; then echo "${LINE_TO_ADD}" >> $HOME/.bashrc; fi
source $HOME/.bashrc

dpkg: error processing archive

I got this error when trying to install anything on ubuntu 16.04.
SyntaxError: invalid syntax
dpkg: error processing archive /var/cache/apt/archives/python-
samba_2%3a4.3.11+dfsg-0ubuntu0.16.04.6_amd64.deb (--unpack):
subprocess new pre-removal script returned error exit status 1
Traceback (most recent call last):
File "/usr/bin/pycompile", line 35, in <module>
from debpython.version import SUPPORTED, debsorted, vrepr, \
File "/usr/share/python/debpython/version.py", line 24, in <module>
from ConfigParser import SafeConfigParser
ImportError: No module named 'ConfigParser'
dpkg: error while cleaning up:
subprocess installed post-installation script returned error exit status 1
Processing triggers for man-db (2.7.5-1) ...
Errors were encountered while processing:
/var/cache/apt/archives/python-libxml2_2.9.3+dfsg1-1ubuntu0.2_amd64.deb
/var/cache/apt/archives/python-samba_2%3a4.3.11+dfsg-0ubuntu0.16.04.6_amd64.deb
N: Ignoring file '50unattended-upgrades.ucf-dist' in directory '/etc/apt/apt.conf.d/' as it has an invalid filename extension
E: Sub-process /usr/bin/dpkg returned an error code (1)
I have tried apt-get -f install but it shows the same error . could anyone help ?
After a while in searching , i have fixed the problem with those lines in terminal:
$ sudo apt-get clean
$ sudo apt-get update
$ sudo apt-get install --reinstall python-minimal python-lockfile
$ sudo apt-get -f install
For a generic approach on how to deal with the issue stated in this question's header, also check answers of this question
the following fixed my issue
sudo dpkg -P <program>
To fix this problem, please check your Python versions.
Insert command:
update-alternatives --config python
Then enable version 2.7.
Now check.

Resources