Failing to transfer Python script to new Ubuntu install

Need help, or want to share a macro? Post here!
Post Reply
0xCoto
Posts: 7
Joined: Wed Aug 10, 2022 3:56 pm

Failing to transfer Python script to new Ubuntu install

Post by 0xCoto »

I am trying to export an individual STL file for every object in an imported STEP file using an external Python script (NOT FreeCAD Python console):

Code: Select all

import sys
# Set FreeCAD Python PATH
sys.path = ['/usr/share/freecad/Mod/Web', '/usr/share/freecad/Mod/Raytracing', '/usr/share/freecad/Mod/Material', '/usr/share/freecad/Mod/Show', '/usr/share/freecad/Mod/Start', '/usr/share/freecad/Mod/Spreadsheet', '/usr/share/freecad/Mod/PartDesign', '/usr/share/freecad/Mod/Part', '/usr/share/freecad/Mod/Complete', '/usr/share/freecad/Mod/Path', '/usr/share/freecad/Mod/Image', '/usr/share/freecad/Mod/Import', '/usr/share/freecad/Mod/Robot', '/usr/share/freecad/Mod/Points', '/usr/share/freecad/Mod/Surface', '/usr/share/freecad/Mod/Test', '/usr/share/freecad/Mod/Tux', '/usr/share/freecad/Mod/Sketcher', '/usr/share/freecad/Mod/MeshPart', '/usr/share/freecad/Mod/Fem', '/usr/share/freecad/Mod/Measure', '/usr/share/freecad/Mod/Ship', '/usr/share/freecad/Mod/Drawing', '/usr/share/freecad/Mod/Plot', '/usr/share/freecad/Mod/Idf', '/usr/share/freecad/Mod/ReverseEngineering', '/usr/share/freecad/Mod/OpenSCAD', '/usr/share/freecad/Mod/Draft', '/usr/share/freecad/Mod/TechDraw', '/usr/share/freecad/Mod/Arch', '/usr/share/freecad/Mod/AddonManager', '/usr/share/freecad/Mod/Inspection', '/usr/share/freecad/Mod/Mesh', '/usr/share/freecad/Mod', '/usr/lib/freecad/lib64', '/usr/lib/freecad-python3/lib', '/usr/share/freecad/Ext', '/usr/lib/freecad/bin', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/dist-packages', '/usr/lib/python3/dist-packages', '', '/usr/lib/freecad/Macro']

# Initiate GUI
from PySide2 import QtCore, QtGui, QtWidgets
import FreeCAD, FreeCADGui

class MainWindow(QtWidgets.QMainWindow):
	def showEvent(self, event):
		FreeCADGui.showMainWindow()
		self.setCentralWidget(FreeCADGui.getMainWindow())

app=QtWidgets.QApplication(sys.argv)
mw=MainWindow()
mw.resize(1200,800)
mw.show()

# Update GUI
app.processEvents()
app.processEvents()
app.processEvents()

# Import step file
exec(open('/usr/share/freecad/Mod/Start/StartPage/LoadNew.py').read())
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")
Gui.ActiveDocument=Gui.getDocument("Unnamed")
import ImportGui
ImportGui.insert(u"/home/abc/Desktop/my_model.step","Unnamed")
Gui.SendMsgToActiveView("ViewFit")

# Export STL
obj_list = []
for obj in FreeCAD.ActiveDocument.Objects:
	if hasattr(obj,"Shape"):
		obj_list.append(obj.Name)

for object in obj_list:
	__objs__=[]
	__objs__.append(FreeCAD.getDocument("Unnamed").getObject(str(object)))

	import Mesh
	Mesh.export(__objs__,u"/home/abc/Desktop/"+str(object)+".stl")

	del __objs__
Although the code worked on Ubuntu (20.04) before, I tried to run it on a fresher Ubuntu (22.04) install, but I get this error:

Code: Select all

Traceback (most recent call last):
  File "/home/abc/Desktop/api/export_stl.py", line 6, in <module>
    from PySide2 import QtCore, QtGui, QtWidgets
  File "/usr/lib/python3/dist-packages/PySide2/__init__.py", line 1, in <module>
    from __future__ import print_function
ModuleNotFoundError: No module named '__future__'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 62, in apport_excepthook
    if not enabled():
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
    import re
ModuleNotFoundError: No module named 're'

Original exception was:
Traceback (most recent call last):
  File "/home/abc/Desktop/api/export_stl.py", line 6, in <module>
    from PySide2 import QtCore, QtGui, QtWidgets
  File "/usr/lib/python3/dist-packages/PySide2/__init__.py", line 1, in <module>
    from __future__ import print_function
ModuleNotFoundError: No module named '__future__'
What could be the cause, and how may I fix it? I'm using Python 3.10.4.
Last edited by 0xCoto on Wed Aug 10, 2022 8:55 pm, edited 1 time in total.
jbi
Posts: 88
Joined: Sun Apr 24, 2016 3:28 pm

Re: Failing to transfer Python script to new Ubuntu install

Post by jbi »

Not an expert on this but i think you need to install future module on the system python installation.
https://pypi.org/project/future/
0xCoto
Posts: 7
Joined: Wed Aug 10, 2022 3:56 pm

Re: Failing to transfer Python script to new Ubuntu install

Post by 0xCoto »

@jbi It seems it is already installed... :(

Code: Select all

abc@abc:~/Desktop/api$ pip -V
pip 22.2.2 from /home/abc/.local/lib/python3.10/site-packages/pip (python 3.10)
abc@abc:~/Desktop/api$ python3 -V
Python 3.10.4
abc@abc:~/Desktop/api$ python3 test_fc.py 
Traceback (most recent call last):
  File "/home/abc/Desktop/api/test_fc.py", line 51, in <module>
    from PySide2 import QtCore, QtGui, QtWidgets
  File "/usr/lib/python3/dist-packages/PySide2/__init__.py", line 1, in <module>
    from __future__ import print_function
ModuleNotFoundError: No module named '__future__'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 71, in apport_excepthook
    import re, traceback
ModuleNotFoundError: No module named 'traceback'

Original exception was:
Traceback (most recent call last):
  File "/home/abc/Desktop/api/test_fc.py", line 51, in <module>
    from PySide2 import QtCore, QtGui, QtWidgets
  File "/usr/lib/python3/dist-packages/PySide2/__init__.py", line 1, in <module>
    from __future__ import print_function
ModuleNotFoundError: No module named '__future__'
abc@abc:~/Desktop/api$ 
0xCoto
Posts: 7
Joined: Wed Aug 10, 2022 3:56 pm

Re: Failing to transfer Python script to new Ubuntu install

Post by 0xCoto »

Update: looks like I resolved it by changing the structure completely so it doesn't use GUI at all (but rather FreeCAD command-line instructions).
Post Reply