Problem when importing FreeCAD modules

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
mpot95
Posts: 4
Joined: Tue Mar 02, 2021 5:45 am

Problem when importing FreeCAD modules

Post by mpot95 »

Hi everybody! I have a problem when trying to import FreeCAD libraries in a python script. In particular when trying to import the Draft module I get the following error:

import Draft
File "/usr/share/freecad/Mod/Draft/Draft.py", line 53, in <module>
from draftutils.utils import ARROW_TYPES as arrowtypes
File "/usr/share/freecad/Mod/Draft/draftutils/utils.py", line 39, in <module>
import PySide.QtCore as QtCore
ModuleNotFoundError: No module named 'PySide'

I am using FreeCad 0.19 and Python 3.8.5. Does anybody have any idea about how to solve this problem?
Thanks a lot in advance.
edi
Posts: 482
Joined: Fri Jan 17, 2020 1:32 pm

Re: Problem when importing FreeCAD modules

Post by edi »

I cannot confirm that, neither in the editor nor in the console.
Can you publish a short example script reproducing the issue ?
mpot95
Posts: 4
Joined: Tue Mar 02, 2021 5:45 am

Re: Problem when importing FreeCAD modules

Post by mpot95 »

I get the error even when only trying to import the Draft module into my .py script. For istance the following code:

Code: Select all

import sys
sys.path.append('/usr/share/freecad/Mod/Draft')
sys.path.append('/usr/lib/freecad/lib')
from FreeCAD import Vector
import Draft
returns the error I've described in the previous post.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Problem when importing FreeCAD modules

Post by heda »

firstly, we can see that you are on linux, but which one might actually matter

secondly
when you activate draft wb via gui, what happens then in the report view?

thirdly
mpot95 wrote: Fri Mar 19, 2021 6:37 pm trying to import the Draft module into my .py script
so, just to be clear.
.py script where? are you running this via freecad console, as a macro, or are you trying to import from for example system python?
mpot95
Posts: 4
Joined: Tue Mar 02, 2021 5:45 am

Re: Problem when importing FreeCAD modules

Post by mpot95 »

heda wrote: Fri Mar 19, 2021 7:20 pm so, just to be clear.
.py script where? are you running this via freecad console, as a macro, or are you trying to import from for example system python?
When importing Draft via FreeCAD console or from a macro everything is ok. I get this error only when importing Draf from the system Python console or from a .py script executed via the system Python console.
I also specify that I'm working under Kubuntu 20.04 LTS.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Problem when importing FreeCAD modules

Post by heda »

oki, importing fc into system python - as far as I understand this is not really a supported use case.

plenty of people attempting this, so just search the forum for using "fc with external python".

what do you want to achieve that you cannot do with a macro from within fc?
or is it just that you are expecting this to work out of the box?

have you looked at starting freecad with for example a script Start_up_and_Configuration.

you could also look at the conda installation of fc, that one might bring you closer to where your expectations are.
Oli772
Posts: 26
Joined: Wed Apr 27, 2022 2:35 pm

Re: Problem when importing FreeCAD modules

Post by Oli772 »

mpot95 wrote: Fri Mar 19, 2021 8:54 pm
heda wrote: Fri Mar 19, 2021 7:20 pm so, just to be clear.
.py script where? are you running this via freecad console, as a macro, or are you trying to import from for example system python?
When importing Draft via FreeCAD console or from a macro everything is ok. I get this error only when importing Draf from the system Python console or from a .py script executed via the system Python console.
I also specify that I'm working under Kubuntu 20.04 LTS.

Hello,
so how did you solve this issue ?
User avatar
onekk
Veteran
Posts: 6208
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Problem when importing FreeCAD modules

Post by onekk »

Oli772 wrote: Fri Jul 29, 2022 4:03 pm Hello,
so how did you solve this issue ?
It is not an issue.

Simply using "FreeCAD as a library" in other word importing FreeCAD with an external python interpreter is not something that is a "supported user case".

FreeCAD have an API that is made in C++ and in Python and has "his internal python interpreter" as some part of FreeCAD are made in Python.

so it is not a simple task to use "FreeCAD as a library", due to the fact that simply FreeCAD is not build to work in this manner at least actually, when the project was started it was slightly more easy to do from some old messages In have read on the forum.

But these messages are more old than 10 years.

There are limited possibilities to make something similar but you have to point your pythonpath to the place were FC (FreeCAD for friends) have placed his libraries, but as not every part ofvthe API is available in python you are on your own and your mileage may vary, at least python versions must match, so if you have a FC compiled with python 3.10 and your puthon OS version is 3.8.5 it is very difficult that things were smooth.


Same difficulties are present if you want to edit python script. with an external editor, using autocompletion and object introspection is rather difficult to obtain, some have found a way to make it partially work.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply