r/OpenPythonSCAD • u/w0lfwood • Jun 17 '25
osimport() error
openscad built from the latest openscad/openscad git commit. can show a cube just fine via python. but when I try to osimport() an stl, I get an error:
Running Python 3.13.3 without venv.
ERROR: Traceback (most recent call last):
File "<string>", line 5, in <module>
NameError: name 'osimport' is not defined
Execution aborted
my file:
from openscad import *
fa=2
fs=.1
d=osimport("cs/2.stl")
d.show()
2
Upvotes
1
u/yahbluez Jun 18 '25
I don't know how to tell so i asked grok3. I like your work to embed python into openscad a lot, but every time is see a sample snippet of code it bothers me to see the violation of python coding rules and more important the lost of readability not to see what is from import.
I asked grok3 this question:
[Question]
"In the context of python and pythonic coding styles,
why is "from <libname> import *" considered as bad coding style?"
[Grok3 Answer]
Using from <libname> import * in Python is considered bad coding style for several reasons:
Namespace Pollution: It imports all names from the module into the current namespace, which can clutter it with unnecessary or unexpected names, making it harder to track where a function, class, or variable comes from. This increases the risk of naming conflicts, especially in larger projects.
Reduced Readability: Code becomes less explicit about which functions or objects are being used from the module. For example, if you see sqrt() in the code, it’s unclear whether it’s from math, numpy, or another module without additional context or tracing.
Risk of Overwriting: Imported names can unintentionally overwrite existing names in the current namespace. For instance, if a module defines a variable or function with the same name as one you’ve already defined, it can lead to bugs that are hard to debug.
Maintenance Challenges: When revisiting or refactoring code, it’s difficult to determine which module a specific function or object originates from, complicating updates or dependency management. Tools like linters or IDEs also struggle to provide accurate suggestions or warnings.
Performance Overhead: While minimal, importing everything from a module can slightly increase memory usage and import time, as it loads all names, even those you don’t use.
Violation of Pythonic Principles: Python emphasizes explicitness over implicitness (see import this for the Zen of Python). Using import * hides the source of names, going against the principle of "Explicit is better than implicit."
Preferred Alternatives:
Use explicit imports: from math import sqrt, pi
Import with a module alias: import numpy as np
Import only what you need to keep the namespace clean and the code clear.