embedding FreeCAD in VSC

Having trouble installing or compiling FreeCAD? Get help here.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
LorenzH
Posts: 3
Joined: Mon Dec 21, 2020 4:00 pm

embedding FreeCAD in VSC

Post by LorenzH »

Hello community,

maybe an easy to answer question, but i haven't figured it out yet with the available sources. I successfully installed FreeCAD v0.18. As i want to use FreeCAD with an external python script, i need to "import" FreeCAD in my Visual Studio Code, i tried two approaches:

1)
using the wiki post: https://wiki.freecadweb.org/Embedding_FreeCAD

Code: Select all

sys and sys.path.append(G:/Program Files/FreeCAD/lib) 
import FreeCAD as App 
(that didn't work)

2)
created a environment file .env and defined (as described in post https://forum.freecadweb.org/viewtopic.php?t=45919) the paths there as follows:

Code: Select all

FREECAD_LIB = G:/Program Files/FreeCAD/lib
FREECAD_MOD = G:/Program Files/FreeCAD/Mod
PYTHONPATH = ${FREECAD_LIB}:${FREECAD_MOD}:${PYTHONPATH}
reopened VSC but didn't work.

With both approaches, VSC does not find the FreeCAD to work with.

Thanks in advance
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: embedding FreeCAD in VSC

Post by Kunda1 »

Install v0.19_pre instead of 0.18
Follow the instructions at:
See https://forum.freecadweb.org/viewtopic.php?f=8&t=40673
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: embedding FreeCAD in VSC

Post by wmayer »

sys.path.append(G:/Program Files/FreeCAD/lib)
You must use the path that contains the file FreeCAD.pyd.
LorenzH
Posts: 3
Joined: Mon Dec 21, 2020 4:00 pm

Re: embedding FreeCAD in VSC

Post by LorenzH »

Kunda1 wrote: Mon Dec 21, 2020 7:50 pm Install v0.19_pre instead of 0.18
Follow the instructions at:
See https://forum.freecadweb.org/viewtopic.php?f=8&t=40673
Hi Kunda,
well i cannot tell you yet what the problem is, but it not working with this approach on my windows os. (as VSC indicates you have to change the : to ; in the path, however still it doesn't recognise the path.

When using the sys.path.append() command, i can at least import it without errors, but as soon as I try to create the simple GUI as in the tutorial by Cyril, it fails. And with the append command i don't get the "auto complete". It finds the modules for "FreeCAD" and "Part", but doesn't find anything for FreeCADGui. And if i try to import FreeCADGui.dll or .pyd it says that both aren't a package.

Any ideas to that?
LorenzH
Posts: 3
Joined: Mon Dec 21, 2020 4:00 pm

Re: embedding FreeCAD in VSC

Post by LorenzH »

wmayer wrote: Tue Dec 22, 2020 11:28 am
sys.path.append(G:/Program Files/FreeCAD/lib)
You must use the path that contains the file FreeCAD.pyd.
i did so, and it does find the FreeCAD.pyd but not the FreeCADGui.pyd file (or at least it says, it isn't a module)
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: embedding FreeCAD in VSC

Post by Kunda1 »

LorenzH wrote: Tue Dec 22, 2020 2:47 pm i did so, and it does find the FreeCAD.pyd but not the FreeCADGui.pyd file (or at least it says, it isn't a module)
@LorenzH did you figure this out eventually ?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
jox81
Posts: 1
Joined: Mon Feb 15, 2021 9:18 pm

Re: embedding FreeCAD in VSC

Post by jox81 »

Hi Lorenz,

The base exemple from where your code comes from is written for linux.

As your are on Windows (> you're using "Program Files"), the environnement variable path have to use ';' instead of ':' like this :

Code: Select all

PYTHONPATH = ${FREECAD_LIB};${FREECAD_MOD};${PYTHONPATH}
Also in windows be sure to have an environnement variable defined for PYTHONPATH pointing to python installation folder
Post Reply