从python中的不同模块调用函数

问题描述 投票:-2回答:2

我正在写一个用西班牙语结合动词的基本程序。我目前有两个文件:main.py和test.py.我正在使用test.py来测试该函数。 目前main.py有:

import test as present

print("Welcome to Spanish Verb Conjugator")
verb = raw_input("Enter the verb: ")
length = len(verb)

#print(length)

v1 = length - 2
r1 = length - 1
v = verb[v1]
r = verb[r1]
end = str(v+r)
stem = verb[0:v1]


tense = raw_input("Choose your tense: ")
if tense == "present":
    test.testt(end)

最后我试着调用test.py上的testt函数test.py有:

import main 

def testt(ending):
    if ending == "ar":
        form = raw_input("Form: ")
        if form == "yo":
            return form + " " + stem + "o"

我的错误是:

Traceback (most recent call last):
  File "/home/ubuntu/workspace/main.py", line 1, in <module>
    import test
  File "/home/ubuntu/workspace/test.py", line 1, in <module>
    import main 
  File "/home/ubuntu/workspace/main.py", line 19, in <module>
    test.testt(end)
AttributeError: 'module' object has no attribute 'testt'

我正在使用python 2。

python function import module
2个回答
1
投票

将main.py中的代码更改为:

import test 

print("Welcome to Spanish Verb Conjugator")
verb = raw_input("Enter the verb: ")
length = len(verb)

#print(length)

v1 = length - 2
r1 = length - 1
v = verb[v1]
r = verb[r1]
end = str(v+r)
print end
stem = verb[0:v1]


tense = raw_input("Choose your tense: ")
if tense == "present":
    test.testt(end)

并将test.py更改为:

def testt(ending):
    if ending == "ar":
        form = raw_input("Form: ")
        if form == "yo":
            return form + " " + stem + "o"

此外,

stem不会在test.py中工作,因为它在main.py中定义


0
投票

您正在导入test作为present。而不是使用test.testt()使用present.testt()。此外,您的代码遭受circular import问题。 Circular Import Problem

© www.soinside.com 2019 - 2024. All rights reserved.