python - 从 cmd 导入 Cmd,(不适用于 MyPrompt 类)

标签 python eclipse python-3.x inner-classes

这是 python 中的辅助命令行,我当然希望为自己添加自定义内容,并且正在添加 IP 搜索器。出现此错误已有一段时间了,似乎无法弄清楚它对启动我的提示至关重要。任何提示表示赞赏,美好的一天。

from cmd import Cmd
import pyautogui as pag #Future#
import time #Debugging/Future#
import sys,os
clear = lambda : os.system('cls')
#############################################
from colorama import init
from ctypes.test.test_pickling import name
init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected
from termcolor import cprint 
from pyfiglet import figlet_format
##############################################

class MyPrompt(Cmd):

    @staticmethod
    def do_lineage(self):
        """Switch to Lineage 2 Helper"""
        print("Switching to Lineage 2 Helper....")
        os.system(r"python C:\Users\David\eclipse-workspace\CMD\src\L2.py")

    @staticmethod
    def do_ip(self):
        """IP"""
        print("Switching to IP stuff.... ")
        os.system(r"python C:\Users\David\eclipse-workspace\CMD\src\Play.py")    

    @staticmethod
    def do_quit(self):
        """Quits the program."""
        print("Quitting...")
        raise SystemExit

    @staticmethod
    def do_Movies(self,num):
        """1-3 different sites, or all for """
        if num == 1:
            print("https://genvideos.org")
            os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome --app=https://genvideos.org")
        if num == 2:
            print("https://www3.gomovies.sc")
            os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome --app=https://www3.gomovies.sc")
        if num == 3:
            print("https://vioozgo.org/")
            os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome --app=google.com")
        if num == "all":
            print("https://genvideos.org")
            print("https://www3.gomovies.sc")
            print("https://vioozgo.org/")
            os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome --app=google.com")


if __name__ == '__main__':
    clear()
    prompt = MyPrompt()
    prompt.prompt = '> '
    prompt.cmdloop(cprint(figlet_format('--------\nHelper\n--------', font='smslant'), "yellow"))

我的错误:

Traceback (most recent call last):
  File "C:\Users\David\eclipse-workspace\CMD\src\Cmdd.py", line 1, in <module>
    from cmd import Cmd
ImportError: cannot import name 'Cmd'

这以前是有效的,必须改变一些东西。我哪里错了?

最佳答案

当我遇到这样的问题时,我喜欢让自己进入命令行中的调试器并开始四处寻找。

为此,我添加了import pdb; pdb.set_trace() 在问题发生的地方附近,在本例中是在文件的顶部。进入 Debug模式后,我开始查看导致问题的对象。我可能会首先更改导入语句以导入完整的 cmd 模块,然后我会 dir 说模块。您可以打印 cmd.__file__ 以查看它来自哪里

import cmd
import pdb; pdb.set_trace()
# code stops here so you can start digging
# dir(cmd) will tell you what properties the module has
# cmd.__file__ will tell you the file path

from cmd import Cmd
import pyautogui as pag #Future#
import time #Debugging/Future#
import sys,os
clear = lambda : os.system('cls')
#############################################
from colorama import init
from ctypes.test.test_pickling import name
init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected
from termcolor import cprint 
from pyfiglet import figlet_format
##############################################

class MyPrompt(Cmd):

    @staticmethod
    def do_lineage(self):
        """Switch to Lineage 2 Helper"""
        print("Switching to Lineage 2 Helper....")
        os.system(r"python C:\Users\David\eclipse-workspace\CMD\src\L2.py")

    @staticmethod
    def do_ip(self):
        """IP"""
        print("Switching to IP stuff.... ")
        os.system(r"python C:\Users\David\eclipse-workspace\CMD\src\Play.py")    

    @staticmethod
    def do_quit(self):
        """Quits the program."""
        print("Quitting...")
        raise SystemExit

    @staticmethod
    def do_Movies(self,num):
        """1-3 different sites, or all for """
        if num == 1:
            print("https://genvideos.org")
            os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome --app=https://genvideos.org")
        if num == 2:
            print("https://www3.gomovies.sc")
            os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome --app=https://www3.gomovies.sc")
        if num == 3:
            print("https://vioozgo.org/")
            os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome --app=google.com")
        if num == "all":
            print("https://genvideos.org")
            print("https://www3.gomovies.sc")
            print("https://vioozgo.org/")
            os.system("C:\Program Files (x86)\Google\Chrome\Application\chrome --app=google.com")


if __name__ == '__main__':
    clear()
    prompt = MyPrompt()
    prompt.prompt = '> '
    prompt.cmdloop(cprint(figlet_format('--------\nHelper\n--------', font='smslant'), "yellow"))

关于python - 从 cmd 导入 Cmd,(不适用于 MyPrompt 类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51677378/

相关文章:

python - 如何对python字典的每个键的值进行排序?

python - OpenCV Python人脸识别仅一个人脸识别

带有 Gevent 池的 Python 脚本,消耗大量内存,锁定

java - 尝试在 eclipse 中使用 tomcat

java - 如何在类路径中找到隐藏的jar?

eclipse - 在eclipse中集成Redmine和Mylyn生成查询失败

python - 如何将二维列表写入txt文件?

python-3.x - 子类和父类的比较

python - ctypes - 函数仅返回空字符串

python - 为唯一的键值对创建 UUID,同时保持字典完整性