Recently, I encountered a problem I haven’t had to deal with in quite a while. I wanted to use the name platform.py for my game, but apparently there is already a module called that, so I had to use a different name. It took me quite a while to figure out why it wasn’t working (perhaps purely due to incompetence) because the error message simply stated that the function I was trying to use didn’t exist. But I honestly had no idea what was happening for about 5 minutes.
My first proposal is to have “user-level” scripts, so the developer can use any name for their scripting purposes. A way to have 2 python modules – one system-level Blender and one user-level – with the same name, but prioritize whichever one the user has created (toggle-able, of course, between the two for each module with the same name).
My second proposal, is to also have a warning in the UI stating that there are conflicting module names. Maybe a little yellow triangle next to the text editor and logic controller.
Not really of great importance, but I really like being able to specify exactly what my modules are named for my benefit of understanding what they do.
There might be a better way of doing this, or already a method I am unaware of? Anyone else have any suggestions?
Sent from my Galaxy Nexus using Tapatalk
The problem is all modules are loaded into the space in Python (sys.modules I think). In order to keep two levels, we’d have to write our own module dictionaries and hook Python import statement. This is doable, probably not worth it. Besides, you’d then shadow the module from the standard library, which is bad practice.
Sometimes I feel the use of the word “scripting” is a misnomer, since scripting naively implies limited functionality, however Python is a general-purpose programming language. Probably many people think of programming : compiled and scripting : interpreted, but the distinction is quickly fading these days.
Creating a user-level scope is an unecessary abstraction since the language itself can already deal with it (by throwing an error…). IMHO, with names conflicting with reserved names, you generally don’t want to use reserved keywords as variable names as it makes the code harder to read (e.g. in C# you can do private string @long). An alternative is to prefix it or use a synonym.
In terms of understanding what you are programming, well documented code and appropriate comments are your best friends
This is a common situation, not limited towards the BGE.
If you really need to use the same names you can place your modules in a package.
Using packages is the common way to deal with such naming conflicts.
Btw. If it take you just 5 minutes to find out about the naming conflict, then you are really good. It took my half a day to find out why my test.py was reported to miss the function it obviously contained :). No i call my test modules demo.py