Home > Enterprise >  My program throws an error when trying to run my function
My program throws an error when trying to run my function

Time:03-05

I tried to make a function that checks if my Ubuntu Server has a specific package installed via apt list, when the condition isn't met it, in theory, the program should install any necessary dependencies for the other piece of software to work. Here's a function I wrote:

# Docker Configuration Tool
def DCT():
    cache = apt.Cache()
    if cache['docker-ce'].is_installed:
        print("Docker and Docker-compose are installed on this system...")
        print("If you don't have MySQL Server installed on your system use Docker to prepare and configure your Server")
        __run_file = [ BASH COMMANDS ]
        OS_MCE(__run_file)
    else:
        print("Docker and Docker-compose are not installed on this system!")
        print("Preparing Environment for the Installation...\n")

        __install_docker = [ BASH COMMANDS ]
        OS_MCE(__install_docker)

An error when trying to run this function:

Traceback (most recent call last):
  File "MIM.py", line 304, in <module>
    NSWIT(True)
  File "MIM.py", line 297, in NSWIT
    Menu()
  File "MIM.py", line 257, in Menu
    DCT()
  File "MIM.py", line 117, in DCT
    if cache['docker-ce'].is_installed:
  File "/usr/lib/python3/dist-packages/apt/cache.py", line 305, in __getitem__
    raise KeyError('The cache has no package named %r' % key)
KeyError: "The cache has no package named 'docker-ce'"

CodePudding user response:

Try changing your if expression to check that the docker-ce key is in the cache map before trying to access it. If there isn't a key with that name, it makes sense to assume that the package isn't installed. So like this:

# Docker Configuration Tool
def DCT():
    cache = apt.Cache()
    if 'docker-ce' in cache and cache['docker-ce'].is_installed:
        print("Docker and Docker-compose are installed on this system...")
        print("If you don't have MySQL Server installed on your system use Docker to prepare and configure your Server")
        __run_file = [ BASH COMMANDS ]
        OS_MCE(__run_file)
    else:
        print("Docker and Docker-compose are not installed on this system!")
        print("Preparing Environment for the Installation...\n")

        __install_docker = [ BASH COMMANDS ]
        OS_MCE(__install_docker)

Error messages are your friend. Read them carefully. In this case, the error was telling you precisely what was wrong, and with that, it is obvious how to fix it once you've been doing this for a while.

This is a good opportunity to learn about and understand short-circuited evaluation of logical expressions. The first clause of your conditional expression insures that the second clause isn't evaluated if it will throw the error you were seeing (well, Duh!, right?)...but if you don't fully understand why that is, it's a good thing to clearly understand. Maybe see: https://pythoninformer.com/python-language/intermediate-python/short-circuit-evaluation/

  • Related