Drone4four
September 16th, 2017, 11:52 PM
I'm trying to run some Python code from a Python "Bookazine" . Initially the material begins with some Python code for rudimentary rock-paper-scissors and hangman games which would run from the command line. It’s been fun to play with. But now the book ports the game scripts from the command line into Tkinter. I’ve encountered a few issues.
Here (https://github.com/Angeles4four/PyMag-games/blob/master/tkinter/Main_Interface.py) is the parent (Main_Interface.py) script that I am working with. Pastebin alternate (https://pastebin.com/3caj1WJB).
Here (https://github.com/Angeles4four/PyMag-games/blob/master/tkinter/rockscissorspaper.py) is the rock-scissors-paper game module I am troubleshooting. Pastebin alternate (https://pastebin.com/C1MEcXR5).
Here (https://github.com/Angeles4four/PyMag-games) is the main directory for the whole project on GitHub.
When I run the Main_Interface.py script, the window pops up and looks pretty good. But when I click on the option button to launch the subsequent script, rock-paper-scissors game, nothing happens.
Someone recommended that I begin to use an IDE and debugger which would help me troubleshoot. So I've settled with Spyder2. Spyder has identified all sorts of issues with my code. I counted 32 triangles with exclamation marks in my rock-scissors paper script. Here are some near the top of the script in question:
Line 3: 'from Tkinter import *'; unable to detect undefined names
I kinda get this one based on a quantifiedcode doc called, "using wildcard imports (from … import *) (https://docs.quantifiedcode.com/python-anti-patterns/maintainability/from_module_import_all_used.html#using-wildcard-imports-from-import)"
which details how it’s better practice to specify which function within the imported module, rather than importing the entire module. From the article:
When an import statement in the pattern of from MODULE import * is used it may become difficult for a Python validator to detect undefined names in the program that imported the module. Furthermore, as a general best practice, import statements should be as specific as possible and should only import what they need.
So this might mean that the authors of the Python Bookazine are teaching less than best practices. I am reluctant to make the leap to add every single function referred to in the game script because there are so many which leaves alotta room for error. My question here: Even though it might be better practice to not import everything, still from Tkinter I should be able to import all the functions, right?
Line 15: local variable ‘names’ is assigned to but never used
Line 16: local variable ‘rules’ is assigned to but never used
I don’t understand the issues identified at lines 15 +16 because the variables are indeed defined but they are in fact used shortly there after in the script. My question: Why is Spyder saying these variables are never used when they actually are? Is Spyder expecting to have variables defined after they are called in a function, rather than the revers?
Line 20: 'game' may be undefined or defined from star imports: Tkinter
Line 28: ‘names’ may be undefined or defined from star imports: Tkinter
Line 37: ‘rules’ may be undefined or defined from star imports: Tkinter
The issues identified here at lines 20, 28 and 37 look similar to the issues referred to above. The functions and variables are indeed defined and they are not imported with Tkinter. What is Spyder trying to say here?
I’m using Spyder v3.1.4 and underneath I’m running Python v2.7 on Ubuntu 17.10.
Thanks for your attention.
Here (https://github.com/Angeles4four/PyMag-games/blob/master/tkinter/Main_Interface.py) is the parent (Main_Interface.py) script that I am working with. Pastebin alternate (https://pastebin.com/3caj1WJB).
Here (https://github.com/Angeles4four/PyMag-games/blob/master/tkinter/rockscissorspaper.py) is the rock-scissors-paper game module I am troubleshooting. Pastebin alternate (https://pastebin.com/C1MEcXR5).
Here (https://github.com/Angeles4four/PyMag-games) is the main directory for the whole project on GitHub.
When I run the Main_Interface.py script, the window pops up and looks pretty good. But when I click on the option button to launch the subsequent script, rock-paper-scissors game, nothing happens.
Someone recommended that I begin to use an IDE and debugger which would help me troubleshoot. So I've settled with Spyder2. Spyder has identified all sorts of issues with my code. I counted 32 triangles with exclamation marks in my rock-scissors paper script. Here are some near the top of the script in question:
Line 3: 'from Tkinter import *'; unable to detect undefined names
I kinda get this one based on a quantifiedcode doc called, "using wildcard imports (from … import *) (https://docs.quantifiedcode.com/python-anti-patterns/maintainability/from_module_import_all_used.html#using-wildcard-imports-from-import)"
which details how it’s better practice to specify which function within the imported module, rather than importing the entire module. From the article:
When an import statement in the pattern of from MODULE import * is used it may become difficult for a Python validator to detect undefined names in the program that imported the module. Furthermore, as a general best practice, import statements should be as specific as possible and should only import what they need.
So this might mean that the authors of the Python Bookazine are teaching less than best practices. I am reluctant to make the leap to add every single function referred to in the game script because there are so many which leaves alotta room for error. My question here: Even though it might be better practice to not import everything, still from Tkinter I should be able to import all the functions, right?
Line 15: local variable ‘names’ is assigned to but never used
Line 16: local variable ‘rules’ is assigned to but never used
I don’t understand the issues identified at lines 15 +16 because the variables are indeed defined but they are in fact used shortly there after in the script. My question: Why is Spyder saying these variables are never used when they actually are? Is Spyder expecting to have variables defined after they are called in a function, rather than the revers?
Line 20: 'game' may be undefined or defined from star imports: Tkinter
Line 28: ‘names’ may be undefined or defined from star imports: Tkinter
Line 37: ‘rules’ may be undefined or defined from star imports: Tkinter
The issues identified here at lines 20, 28 and 37 look similar to the issues referred to above. The functions and variables are indeed defined and they are not imported with Tkinter. What is Spyder trying to say here?
I’m using Spyder v3.1.4 and underneath I’m running Python v2.7 on Ubuntu 17.10.
Thanks for your attention.