为什么REPL无法识别我的功能

时间:2020-07-14 02:41:59

标签: python python-3.x read-eval-print-loop

由于某种原因,当我在REPL中运行程序时,无法识别我的模块。我从导入单词(fetch_words,print_words)中输入 ,并收到错误 fetch_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() 

1 个答案:

答案 0 :(得分:1)

导入函数时语法不正确。

由于您已将文件命名为import {spawn} from "child_process"; // haystack to search within const haystack = "I am \n such a big string, do you\n see me?"; const readable = new Readable(); readable.push(haystack); readable.push(null); // the list of needles that would normally go in `--file=needles.txt` const needles = ["find", "me", "or", "me"]; // spawn `fgrep` // Q: How do I pass in `needles` as a string? const fgrep = spawn(`fgrep`, [needles]) // pipe my haystack to fgrep readable.pipe(fgrep.stdin); ,因此导入其中定义的函数的正确语法为:

practice.py

或者如果您需要导入多个功能:

from practice import fetch_words

请记住,要从中导入的模块的名称应与文件名称相同,但不带from practice import fetch_words, print_words 扩展名。在这种情况下,模块是.py,而不是practice

相关问题