簡體   English   中英

導入另一個程序作為模塊

[英]import another program as module

我在堆棧上搜索了我的問題,但沒有找到解決方案,所以我來這里問你關於.im學習的問題,這本書是用書“ OReilly.Introducing.Python”以及模塊部分的第5章。作者說,你可以保存一個當兩個程序保存在一個目錄中時,在另一個程序中作為模塊使用。 這是第一個用作模塊的程序。 report.py

def get_description(): # see the docstring below?
"""Return random weather, just like the pros"""
    from random import choice
    possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows']
    return choice(possibilities)

而主程序是這樣的:

import report
description = report.get_description()
print("Today's weather:", description)

它是一個簡單的程序,我知道何時要導入此錯誤,它會終止:

追溯(最近一次通話最近):導入報告中的文件“ H:\\ python \\ Lib \\ weather.py”,第1行,文件“ H:\\ python \\ Lib \\ report.py”,行2“””返回隨機天氣,就像專業人士一樣“”“” ^ IndentationError:預期為縮進塊

我試圖更改目錄並將其復制到lib文件夾或腳本,這是我的sys.path:H:\\ python \\ Lib C:\\ Windows \\ System32 H:\\ python \\ Lib \\ idlelib H:\\ python \\ python35.zip H :\\ python \\ DLLs H:\\ python \\ lib H:\\ python H:\\ python \\ lib \\ site-packages

正如錯誤所言,您必須標識文檔字符串:

def get_description(): # see the docstring below?
    """Return random weather, just like the pros"""
    from random import choice
    possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows']
    return choice(possibilities)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM