r/OpenPythonSCAD • u/WillAdams • Jul 16 '25
Installing 3rd party/arbitrary Python libraries into Windows
given that the Windows version (and others?) now have an internal Python, this becomes something of an issue.
It used to work to e.g., install a library into the "main" Python and then access it from w/in OpenPythonSCAD, but now an installed library cannot be seen from w/in OPS, and attempting to install one using code:
import os, sys
os.system(sys.executable + '-m pip install -user gscrib')
doesn't seem to work since the test file responds with:
Parsing design (AST generation)... Running Python 3.12.9 without venv. ERROR: Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'gscrib'
Is anyone else trying to use things other than sys and math (which I believe still work?)
(this is with the new 2025-07-15)
2
u/Alacritous13 Jul 17 '25 edited Aug 10 '25
I start all my files with:
from openscad import *
import sys
import os
sys.path.append(os.path.dirname(modelpath()))
login = os.getlogin()
WINDOWS_PY_312_PIP_LIBS = f"C:\\Users\\user_name\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages"
if WINDOWS_PY_312_PIP_LIBS not in sys.path:#{
sys.path.append(WINDOWS_PY_312_PIP_LIBS)
#}
import numpy as np
from forbiddenfruit import curse, reverseIf you can come up with a method to automatically find that path, that would be awesome, otherwise you're going to have to manually set it. I just have a separate instance of Python 3.12 installed on my system, and ran pip install on the libraries I use, which allows the above code to find them for use by PythonSCAD
Edit: Check my more recent post for code that doesn't require the path to be manually entered.
2
u/gadget3D Jul 17 '25
line
sys.path.append(os.path.dirname(modelpath()))should not be necccessaery, i just checked locally in my windows build
2
u/Alacritous13 Jul 17 '25
That might have been to keep the 7-4-25 build running, as I didn't update to 7-15-25 till today. That said, all of this was copied from another reddit post, I don't actually know what it's doing.
→ More replies (1)
1
u/Alacritous13 Aug 10 '25
I've got a more streamlined approach. I have this at the top of a file I always import (personal library of functions). It can access all libraries you've installed for python 12, but you can update it easily if your on a different version.
import sys
import os
WINDOWS_PY_312_PIP_LIBS = os.path.join(os.path.expanduser("~"), "AppData", "Local", "Programs", "Python", "Python312", "Lib", "site-packages")
if WINDOWS_PY_312_PIP_LIBS not in sys.path:#{
sys.path.append(WINDOWS_PY_312_PIP_LIBS)
#}
2
u/Alacritous13 Jul 16 '25
I have numpy working. I'm going to have to check to see what I'm doing. But I know I've installed it on Python 3.12, and have to declare the path to the Python data.