Embedding FreeCAD in a Python virtual environment
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
- adrianinsaval
- Veteran
- Posts: 5551
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Embedding FreeCAD in a Python virtual environment
are the python versions matching?
- ebrahim raeyat
- Posts: 621
- Joined: Sun Sep 09, 2018 7:00 pm
- Location: Iran
- Contact:
- adrianinsaval
- Veteran
- Posts: 5551
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Embedding FreeCAD in a Python virtual environment
Ugh, windows and it's stupid backslash paths
try writing your path like that @mlaura.leonardi, the backslash is also a escape character and confuses python
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'
Re: Embedding FreeCAD in a Python virtual environment
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'
Re: Embedding FreeCAD in a Python virtual environment
Hello, are you trying this so you don't get errors when debugging in VSCode?mlaura.leonardi wrote: ↑Tue Sep 27, 2022 3:14 pm Unfortunately this is still giving me the same issue
-
- Posts: 12
- Joined: Mon Sep 26, 2022 3:02 pm
Re: Embedding FreeCAD in a Python virtual environment
- Attachments
-
- Screenshot 2022-09-28 142129.png (27.43 KiB) Viewed 805 times
Re: Embedding FreeCAD in a Python virtual environment
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.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?
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.
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.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
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")
- adrianinsaval
- Veteran
- Posts: 5551
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Embedding FreeCAD in a Python virtual environment
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.
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.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")
Re: Embedding FreeCAD in a Python virtual environment
It talks about a compatible interpreter but doesn't say what a compatible interpreter would be. Insofar it's correct but not very helpful.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.
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.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:
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.
- adrianinsaval
- Veteran
- Posts: 5551
- Joined: Thu Apr 05, 2018 5:15 pm
Re: Embedding FreeCAD in a Python virtual environment
Thanks, I'll edit the page accordingly later.