【问题标题】:Could someone explain to me what I'm doing wrong in this Python Code and help me figure it out?有人可以向我解释我在这个 Python 代码中做错了什么并帮助我弄清楚吗?
【发布时间】:2014-02-20 13:38:06
【问题描述】:

这是错误:

    Traceback (most recent call last):
      File "/home/endergreen/PyOS/SHOE/C.pyx", line 24, in <module>
        x.execute()
    AttributeError: 'str' object has no attribute 'execute'

这是一组代码:

    #!/usr/bin/env python
    import os as os
    import sys as sys
    import time as time
    import DefaultX as x

    def setup():
        execfile ("Setup.pyx")
        print ("Running...")
    def start():
        execfile ("/HDD/BOOT.pyrx")
        print ("Running...")
    def end():
        print ("Stopping...")
        time.sleep (3)

    x.machine_start
    x = raw_input (">>>")
    if x == 'setup':
        x.setup()
    if x == 'start':
        x.start()
    if x == 'execute':
        x.execute()
    if x == 'chkfile':
        x.chkfile()
    if x == 'chkinstall':
        x.chkdisk()
    if x == 'commands':
        x.cmds()
    else:
        print ("Unsupported Command")
        raw_input ("Press [Enter] to continue...")
        execfile ("C.pyx")

错误似乎是在 DefaultX.py 中没有定义“执行”函数,但我在另一个项目中使用了相同的精确代码(文字复制粘贴情况)并且它工作 100%!我不知道我做错了什么,任何帮助将不胜感激!

【问题讨论】:

  • 您希望在x.execute() 行发生什么? xstr
  • 你正在用 x = raw_input (">>>") 覆盖 x
  • 我很确定代码从来没有工作过,不管大写字母是否有把握......
  • DSM,什么代码不起作用?你的意思是DefaultX? DefaultX 是我制作的自定义模块。
  • @user3230819:我的意思是x的冲突不是突然出现的。

标签: python python-import function


【解决方案1】:

您对x 的定义有冲突:

import DefaultX as x

x = raw_input (">>>")

换一个。

【讨论】:

  • 谢谢你,马克,你的回答。我很困惑我怎么会犯这样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 2022-12-28
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多