Embedding FreeCAD in a Python virtual environment

Having trouble installing or compiling FreeCAD? Get help here.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by adrianinsaval »

are the python versions matching?
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by adrianinsaval »

Ugh, windows and it's stupid backslash paths :roll: :roll:
try writing your path like that @mlaura.leonardi, the backslash is also a escape character and confuses python

Code: Select all

'C:\\Program Files\\FreeCAD 0.20\\bin'
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by heda »

one way is to always use raw strings for paths on windows (but much better to use pathlib nowadays)...

Code: Select all

r'C:\Program Files\FreeCAD 0.20\bin'
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by heron »

mlaura.leonardi wrote: Tue Sep 27, 2022 3:14 pm Unfortunately this is still giving me the same issue
Hello, are you trying this so you don't get errors when debugging in VSCode?
mlaura.leonardi
Posts: 12
Joined: Mon Sep 26, 2022 3:02 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by mlaura.leonardi »

:( :( :( :( :(
Attachments
Screenshot 2022-09-28 142129.png
Screenshot 2022-09-28 142129.png (27.43 KiB) Viewed 769 times
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Embedding FreeCAD in a Python virtual environment

Post by wmayer »

Thus, if I really need to use FC embedded (which is the case), is it better for me to compile it first, in order to ensure the use of the same version of Python?
It suffices that major and minor Python version match but the micro number can be different because Python guarantees ABI compatibility. So, this means if FreeCAD was built with Python 3.9.7 your Python version can be 3.9.12. Important only is that the major version is 3 and the minor number is 9.

If this isn't the case then you can either use the Python version that comes with a FreeCAD installation or recompile FreeCAD with your Python version. But this won't be straightforward.
Do you know where can I find the .dll file? Because in the tutorial they just mention the FreeCAD.dll file, whereas in my bin folder I found three FreeCAD. dll files: FreeCADApp.dll, FreeCADBase.dill and FreeCADGui.dll
A FreeCAD.dll doesn't exist. The file is called FreeCAD.pyd but technically it's indeed a real dll. Python uses the pyd file suffix to make it clear it's an extension module.
:( :( :( :( :(
There is a Windows-specific thing since Python 3.8. There it no longer suffices to extend the PATH environment variable in order to import an extension module that depends on some other dlls. Now you additionally have to write:

Code: Select all

import os
os.add_dll_directory(r"C:\Program Files\FreeCAD 0.20\bin")
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by adrianinsaval »

wmayer wrote: Wed Sep 28, 2022 2:37 pm It suffices that major and minor Python version match but the micro number can be different because Python guarantees ABI compatibility. So, this means if FreeCAD was built with Python 3.9.7 your Python version can be 3.9.12.
is this statement from the wiki wrong then? I guess on windows this wouldn't be much of an issue anyways as most version would be all compiled with MSVC
https://wiki.freecadweb.org/Embedding_FreeCAD#Caveats wrote:Since the FreeCAD Python module is compiled from C++ (rather than being a pure Python module), it can only be imported from a compatible Python interpreter. Generally this means that the Python interpreter must be compiled with the same C compiler as was used to build FreeCAD.
There is a Windows-specific thing since Python 3.8. There it no longer suffices to extend the PATH environment variable in order to import an extension module that depends on some other dlls. Now you additionally have to write:

Code: Select all

import os
os.add_dll_directory(r"C:\Program Files\FreeCAD 0.20\bin")
should this be added to https://github.com/FreeCAD/FreeCAD/blob ... y.template ? Not sure if this would actually ever be used on windows though.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Embedding FreeCAD in a Python virtual environment

Post by wmayer »

Since the FreeCAD Python module is compiled from C++ (rather than being a pure Python module), it can only be imported from a compatible Python interpreter.
It talks about a compatible interpreter but doesn't say what a compatible interpreter would be. Insofar it's correct but not very helpful.
Generally this means that the Python interpreter must be compiled with the same C compiler as was used to build FreeCAD. Information about the compiler used to build a Python interpreter (including the one built with FreeCAD) can be found as follows:
This is not right. First of all FreeCAD is not built with a C compiler but a C++ compiler. Then under Linux systems it's of course possible to build Python and FreeCAD with different C/C++ compiler versions. There you can even use different compilers, like gcc for Python and clang++ for FreeCAD.

Also under Windows it shouldn't be a problem to use different MSVC versions for Python and FreeCAD.

BTW, at the time when I did the first MinGW port of FreeCAD (around 10 years ago) there it wasn't possible to build the Python binaries. What worked is to use the dll built with MSVC and create a suitable import library from it. It worked quite well to mix the Python dll based on MSVC with the binaries created by gcc.
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Embedding FreeCAD in a Python virtual environment

Post by adrianinsaval »

Thanks, I'll edit the page accordingly later.
Post Reply