无法从我的目录导入python中的函数

康纳

如标题所示,我在将函数导入到main.py文件时遇到了麻烦。我将我的项目(有史以来第一个个人项目)保存到名为的文件夹中BillApp
在该文件夹中,我有以下文件。
main.py commands.py billapp.txt

我正在尝试导入commands.py我的main.py代码,以便可以使用中的功能main.py

# This is the main code which will access the commands from commands.py
import commands

print 'Hello, Welcome to your friendly text-based bill reminder.'
print '\nI will be tracking all of your bills so you will never miss a payment again.'
print '\nLet\'s get started, please type on of the following commands.'

def userSelection():
    userSelection = raw_input('New \nSaved \nRemove \nAlerts')
    userSel = userSelection.lower()
    if userSel == 'new':
        commands.getDesc()
    elif userSel == 'saved':
        commands.savedBills()
    elif userSel == 'remove':
        commands.removeBills()
    elif userSel == 'alerts':
        commands.setAlerts()
    else:
        print 'Please enter a valid command'
        userSelection()

这是我的commands.py文件,以防万一你想看看。

 def userSelection():
    userSelection = raw_input('New \nSaved \nRemove \nAlerts')
    userSel = userSelection.lower()
    if userSel == 'new':
        getDesc()
    elif userSel == 'saved':
        savedBills()
    elif userSel == 'remove':
        removeBills()
    elif userSel == 'alerts':
        setAlerts()
    else:
        print 'Please enter a valid command'
        userSelection()

def getDesc():
    desc = raw_input('\nHow do you want to describe this bill?\n')

def getAmnt():
    try:
        billAmnt = raw_input('How much is this bill?')
        amnt = float(billAmnt.strip('$'))
        return amnt
    except ValueError:
        print 'Please enter an amount that is at least 0'
        getAmnt()

def getDate():
    try:
        strDate = raw_input('What date of the month is this bill due?\nExample: 3rd\n')
        date = strDate.strip('abcdefghijklmnopqrstuvqxyz')
        numDate = int(date)
        print numDate
    except ValueError:
        print 'Please enter the day of the month, for example. 3rd or 17th.'

def getFreqy():
    strFreqy = raw_input('How often do you have to pay this bill? \nMonthly, Weekly, or Once\n')
    freqy = strFreqy.lower()
    if freqy == 'monthly':
        print 'monthly'
    elif freqy == 'weekly':
        print 'weekly'
    elif freqy == 'once':
        print 'once'
    else:
        print 'Please enter either monthly, weekly, or once.'
        getFreqy()

def savedBills():
    print 'Here is a list of all of your bills.'
    # retrieve bills from database

def removeBills():
    print 'You wish to remove a bill from your list, very well, let\'s get started.',
    print 'Here\'s a list of all of your bills.'
    # retrieve a numbered list of all bills
    billNum = raw_input('What number bill would you like to remove?')
    if int.billNum:
        print 'We will remove that bill from your list.'
        waiting()
    else:
        print 'Please type in a corresponding number.'

def setAlerts():
    # pulls the current bill alert from database
    print 'Your bill alerts are currently set to %s.' % currentAlert
    change = raw_input("Would you like to change this?")
        if change == yes:
            # change bill alert in database
        else change == no:
            print 'Your bill alerts will continue as scheduled'
        elif:
            print 'Please enter either yes or no.'

main.py从外壳运行,我得到以下信息。

MacBook-Pro:BillApp cmaher92$ sudo python main.py
Hello, Welcome to your friendly text-based bill reminder.

I will be tracking all of your bills so you will never miss a payment again.

Let's get started, please type on of the following commands.
New 
Saved 
Remove 
Alerts
>new
Traceback (most recent call last):
  File "main.py", line 22, in <module>
    userSelection()
  File "main.py", line 11, in userSelection
    commands.getDesc()
AttributeError: 'module' object has no attribute 'getDesc'
Connor-Mahers-MacBook-Pro:BillApp cmaher92$
阿德里安努斯

一些基本的调试后(你混了elseelif一些缩进和其他错误等中change == yes),这似乎为我工作。commands.py这里导入不是问题。

至少我到达了它要求的部分

您想如何描述这笔帐单?

main.py

# This is the main code which will access the commands from commands.py
import commands

def userSelection():
    userSelection = raw_input('New \nSaved \nRemove \nAlerts')
    userSel = userSelection.lower()
    if userSel == 'new':
        commands.getDesc()
    elif userSel == 'saved':
        commands.savedBills()
    elif userSel == 'remove':
        commands.removeBills()
    elif userSel == 'alerts':
        commands.setAlerts()
    else:
        print 'Please enter a valid command'
        userSelection()

print 'Hello, Welcome to your friendly text-based bill reminder.'
print '\nI will be tracking all of your bills so you will never miss a payment again.'
print '\nLet\'s get started, please type on of the following commands.'
userSelection()

commands.py

def userSelection():
    userSelection = raw_input('New \nSaved \nRemove \nAlerts')
    userSel = userSelection.lower()
    if userSel == 'new':
        getDesc()
    elif userSel == 'saved':
        savedBills()
    elif userSel == 'remove':
        removeBills()
    elif userSel == 'alerts':
        setAlerts()
    else:
        print 'Please enter a valid command'
        userSelection()

def getDesc():
    desc = raw_input('\nHow do you want to describe this bill?\n')

def getAmnt():
    try:
        billAmnt = raw_input('How much is this bill?')
        amnt = float(billAmnt.strip('$'))
        return amnt
    except ValueError:
        print 'Please enter an amount that is at least 0'
        getAmnt()

def getDate():
    try:
        strDate = raw_input('What date of the month is this bill due?\nExample: 3rd\n')
        date = strDate.strip('abcdefghijklmnopqrstuvqxyz')
        numDate = int(date)
        print numDate
    except ValueError:
        print 'Please enter the day of the month, for example. 3rd or 17th.'

def getFreqy():
    strFreqy = raw_input('How often do you have to pay this bill? \nMonthly, Weekly, or Once\n')
    freqy = strFreqy.lower()
    if freqy == 'monthly':
        print 'monthly'
    elif freqy == 'weekly':
        print 'weekly'
    elif freqy == 'once':
        print 'once'
    else:
        print 'Please enter either monthly, weekly, or once.'
        getFreqy()

def savedBills():
    print 'Here is a list of all of your bills.'
    # retrieve bills from database

def removeBills():
    print 'You wish to remove a bill from your list, very well, let\'s get started.',
    print 'Here\'s a list of all of your bills.'
    # retrieve a numbered list of all bills
    billNum = raw_input('What number bill would you like to remove?')
    if int.billNum:
        print 'We will remove that bill from your list.'
        waiting()
    else:
        print 'Please type in a corresponding number.'

def setAlerts():
    # pulls the current bill alert from database
    print 'Your bill alerts are currently set to %s.' % currentAlert
    change = raw_input("Would you like to change this?")
    if change == 'yes':
        # change bill alert in database
        pass
    elif change == 'no':
        print 'Your bill alerts will continue as scheduled'
    else:
        print 'Please enter either yes or no.'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我无法从当前目录中的另一个 py 文件导入函数?

从父目录python导入文件中的函数

GO:无法从同一目录中的文件导入函数

无法从同一目录中的文件导入函数

目录中的Python导入

如何从父目录中的文件导入函数

Python无法导入我的模块

由于 Python 中的破折号,我无法导入库

无法从Python的子目录导入*

无法从其他目录导入python模块

Python 无法从父目录导入类?

无法从pycharm中的源目录导入模块

运行python单元测试无法在要导入的正确目录中找到我的文件

即使与我正在使用的文件位于同一目录中,也无法导入模块

无法从导入的函数中调用函数

使用Python 3从Jupyter Notebook中相对导入的另一个目录中的模块导入本地函数

无法从 python 中的另一个目录导入 python 类

Python中的AWS Lambda:在Lambda函数处理程序中导入父包/目录

无法从当前工作目录之外导入函数

Python:将文件从day_one.py导入到main.py,然后在day_one中,我从main.py导入一个函数。错误无法导入

从python中的其他目录导入文件?

从Python中的其他目录导入类

在python中从本地目录导入训练数据

从目录中以python导入用户定义的模块

如何从python中的不同目录导入模块

从 Python 3.6.9 中的父目录导入错误

为什么我无法使用ipython在Django的shell中访问导入的函数?

无法在Jupyter中导入Python函数

Python。无法从文件中导入多个函数