简体   繁体   English

我的程序在尝试运行我的 function 时抛出错误

[英]My program throws an error when trying to run my function

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.我尝试制作一个 function 检查我的 Ubuntu 服务器是否通过 apt 列表安装了特定的 package,当不满足条件时,理论上,该程序应该安装任何必要的依赖项以使其他软件工作。 Here's a function I wrote:这是我写的 function:

# 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:尝试运行此 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'"

Try changing your if expression to check that the docker-ce key is in the cache map before trying to access it.在尝试访问它之前,尝试更改您的if表达式以检查 docker docker-ce密钥是否在cache map 中。 If there isn't a key with that name, it makes sense to assume that the package isn't installed.如果没有具有该名称的密钥,则可以假设未安装 package。 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.条件表达式的第一个子句确保第二个子句不会被评估,如果它会抛出您看到的错误(好吧,Duh,?正确的。)...,但如果您不完全理解为什么会这样。 it's a good thing to clearly understand: Maybe see: https://pythoninformer.com/python-language/intermediate-python/short-circuit-evaluation/看清楚了是件好事:或许可以看看: https://pythoninformer.com/python-language/intermediate-python/short-circuit-evaluation/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的PyQt应用在空闲状态下运行良好,但尝试从cmd运行时抛出错误 - My PyQt app runs fine inside Idle but throws an error when trying to run from cmd 尝试运行我的 tensorflow 代码时出错 - error when trying to run my tensorflow code 尝试使用 kivy 在 vi​​rtualbox ubuntu 中运行我的 python 程序时出现语法错误 - Syntax error when trying to run my python program in virtualbox ubuntu with kivy 尝试运行我的简单程序时未定义全局名称“ transact” - Global name 'transact' not defined when trying to run my simple program 使用 Tkinter 创建 UI。 尝试调整按钮文本的字体并运行程序时,我收到此错误 - Creating a UI with Tkinter. When trying to adjust the font of my button text and I run the program, I get this error back 尝试运行我的 Jupyter Notebook 时出现错误 13 - Error 13 When trying to run my Jupyter Notebook 运行我的第一个Spark Python程序错误 - Run my first spark python program error 试图找出我的功能的运行时间 - Trying to figure out the run time of my function 对于我的 tkinter 注册程序,Function 没有在我的 class 中运行 - Function doesnt run in my class for my tkinter registration program 为什么我的程序中的 if 语句不是 function 当它放入一个要运行的 function 时? - Why does the if statements in my program not function when it's put in a function that's meant to run?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM