我問過幾個人,並且找不到任何可行的問題。我的代碼看起來完全相同,但我試圖爲遊戲導入標題。雖然該函數在源文件中工作,但在導入到另一個文件後,找不到。以下是文件,在cmd中所產生的誤差:導入的函數帶來的錯誤:名稱未定義
Game.py:
from Events import *
from Character import *
Opening()
Character.py:
from Events import *
from Game import *
Events.py:
from Game import *
from Character import *
def Opening():
print " _____ _ _____ _____ _ _ "
print "/ ___| | | /___| /__ \ | | (_) "
print "\ `--. _ _| |__ ______\ `--. _ __ __ _ ___ ___ |/\/ __ _ ___ ___ _ __ | |__ ___ _ __ _ __ _ "
print " `--. \ | | | '_ \______|`--. \ '_ \/_` |/ __/ _ \ | | /_` |/ __/ _ \| '_ \| '_ \/_ \| '__| |/ _` |"
print "/\__//|_| | |_) | /\__//|_) | (_| | (_| __/ | \__/\ (_| | (_| (_) | |_) | | | | (_) | | | | (_| |"
print "\____/ \__,_|_.__/ \____/| .__/ \__,_|\___\___| \____/\__,_|\___\___/| .__/|_| |_|\___/|_| |_|\__,_|"
print " | | | | "
print " |_| |_| "
但經過在cmd中運行Game.py文件,它會帶來錯誤:
Traceback (most recent call last): File "Game.py", line 2, in <module> from Events import * File "/tmp/so/Events.py", line 2, in <module> from Game import * File "/tmp/so/Game.py", line 8, in <module> Opening() NameError: name 'Opening' is not defined
爲什麼'Events'導入'Game'?另外我建議閱讀[PEP-8](https://www.python.org/dev/peps/pep-0008/)。 – jonrsharpe
試試'import events'' Events.Opening()' –