20

I don't know what I've done but basic commands like "ls" and "sudo" no longer work and now throw me this error message.

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'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 24, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'

Original exception was:
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' 

Please help, I'm crying, I have a deadline tomorrow.

Should I just do a fresh install of Ubuntu but keep the files?

  • 2
    Welcome! After what command the error shows? – guillermo chamorro Feb 20 at 23:53
  • 4
    Let us start with the basics. does cd /bin work or give an error? If it works does cd /bin ; echo ls* give one or more words of output including ls, or does it only give ls* back? – icarus Feb 20 at 23:53
  • Lots of different commands all bring the same error (at least the same as far as I can tell with a quick skim-read). Commands like "sudo", "ls", "dpkg" but commands like "cd" and "pwd" work fine – Retsek Feb 20 at 23:55
  • 2
    Great. Does /bin/ls -l /bin/ls give you a long listing of the 'ls program or an error? If it works then run PATH=/bin:/usr/bin:$PATH and see if ls and sudo are now working. – icarus Feb 21 at 0:04
  • 2
    @Retsek Good luck with the thing you had a deadline for! – MechMK1 Feb 21 at 9:13
37

We did some debugging with the OP.

  1. cd /bin This worked,so we knew /bin had not been deleted
  2. cd /bin; echo ls* This produced "ls lsblk lsmod" rather than "ls*" so we knew there were at least 3 files in /bin
  3. /bin/ls -l /bin/ls This produced "-rwxr-xr-x 1 root root 133792 Jan 18 2018 /bin/ls" so it wasn't a matter of execute permissions being removed.
  4. PATH=/bin:/usr/bin:$PATH This creates a sane couple of places to look for commands, and now ls and sudo are working again.

Conclusion: The PATH variable had become corrupted, and hence bash couldn't find anything. Using step by step debugging starting with just builtin commands (cd and echo) this was diagnosed quickly. Hopefully the OP has dried their tears and is all smiles again.

The OP has just commented that they have "removed some very silly edits to my .bashrc and everything seems to be okay".

|improve this answer|||||
  • 3
    You are a Unix detective, genius and saint. Thank-you for blessing me with your troubleshooting <3 – Retsek Feb 21 at 0:22
  • 12
    You are not the first person to have mangled the PATH. It was my first thought, too. The difference here is that in an attempt at user-friendliness Ubuntu adds an extension to the Bourne Again shell that runs another program when a command is not found, instead of printing a short "command not found" message. When that program fails, as here, its error messages are vastly less user-friendly than the vanilla "command not found" message. What icarus has not covered is repairing the command-not-found system, to stop the utility program from failing. – JdeBP Feb 21 at 8:44
  • 1
    … as it still is at unix.stackexchange.com/q/568967/5132 . (-: – JdeBP Feb 21 at 19:45
  • I was once really surprised to find that some commands could run faster than a command that wasn't found. That drove me nuts for a few days. Thanks Ubuntu! – chicks Feb 25 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.