简体   繁体   English

为什么我的 function 在 REPL 中没有被识别

[英]Why isn't my function recognized in the REPL

For some reason when I run my program in the REPL my module isn't recognized.由于某种原因,当我在 REPL 中运行我的程序时,我的模块无法识别。 I type in from import words (fetch_words, print_words) and receive the error fetch_words is not defined .我输入from import words (fetch_words, print_words)并收到错误fetch_words is not defined This happens as as well when I type import words当我输入导入词时也会发生这种情况

from urllib.request import urlopen

def fetch_words():
    story = urlopen('https://sixty-north.com/c/t.txt')
    story_words = []
    for line in story:
        line_words = line.decode('utf-8').split()
        for word in line_words:
            story_words.append(word)
    story.close()
    return story_words



def print_words(story_words):
    for word in story_words:
        print(word)


def main():
    words = fetch_words
    print_words(words)


if __name__ == '__main__':
    main() 

You have incorrect syntax while importing the function.导入 function 时语法不正确。

Since you have named your file as practice.py , the correct syntax of importing the function defined within it will be:由于您已将文件命名为practice.py ,因此导入其中定义的 function 的正确语法将是:

from practice import fetch_words

Or in case you need to import multiple functions:或者如果您需要导入多个功能:

from practice import fetch_words, print_words

Remember, the name of the module to import from should be the same as the name of the file, without .py extension.请记住,要导入的模块的名称应该与文件的名称相同,没有.py扩展名。 In this case, the module is practice , and not words .在这种情况下,模块是practice ,而不是words

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM