Python import own module - name not defined

前端 未结 1 1339
一向
一向 2020-12-11 22:38

I want to update my basic jumble game. I already did that the script gets words from a text file, now I want to separate them into modules as I have different text files wit

相关标签:
1条回答
  • 2020-12-11 23:27

    The problem you've stated is a standard namespace/scope problem. You've created a variable within the scope of amazement.py, but not in the jumble_game.py namespace. As a result, you can't access the top level variable in amazement.py without telling your program from where to get that variable.

    You can do a few things. I'll list two:

    1.

    from amazement import filename

    This will allow you to use the term "filename" as you've described.

    or 2.

    Replace any reference to filename with amazement.filename.

    You can read more about scopes and namespace here: http://sebastianraschka.com/Articles/2014_python_scope_and_namespaces.html

    0 讨论(0)
提交回复
热议问题