Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
ryn
Posts: 12
Joined: Tue Jun 14, 2022 7:50 pm

Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by ryn »

BUG: When attempting to import FreeCAD in python, I get the error, "ImportError: DLL load failed while importing FreeCAD: The parameter is incorrect."

Steps to reproduce:

create a virtual environment with python 3.8:
https://www.python.org/downloads/release/python-380/

Code: Select all

py -m virtualenv -p="<installed python 3.8 exe location>" .virtenv
import FreeCAD with the following script:

Code: Select all

FREECADPATH = '/program files/FreeCAD 0.20/bin' # path to your FreeCAD.so or FreeCAD.dll file
import sys
import os
sys.path.append(FREECADPATH)

#print(sys.version)

import FreeCAD

print("free cad is " + str(FreeCAD))
Run the above script with the python 3.8 of the virtual environment set to your interpreter.
The above script will result in the bug for at least versions 0.19 and 0.20 of FreeCAD(I've only tested those versions)

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: English/United States (en_US)
Last edited by ryn on Fri Jul 29, 2022 8:22 pm, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by onekk »

FreeCAD as a library is not the FreeCAD executable, there is some infos around on the power user section on the wiki.

Sorry but being on mobile I have no link to supply.

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/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by heda »

what are you trying to get done? (apart from the obvious...)

just asking, since the approach of importing fc into a standalone std-python is more cumbersome than the other way around,
i.e. install whatever you want in the bundled fc-python... (which knows fc by heart from the getgo)

or use conda?
ryn
Posts: 12
Joined: Tue Jun 14, 2022 7:50 pm

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by ryn »

heda wrote: Thu Jun 16, 2022 10:06 pm what are you trying to get done? (apart from the obvious...)

just asking, since the approach of importing fc into a standalone std-python is more cumbersome than the other way around,
i.e. install whatever you want in the bundled fc-python... (which knows fc by heart from the getgo)

or use conda?
I'd ideally like to be able to do parameter inspection of the objects inside of the FreeCAD module so I can know what I can call to do what without copying and pasting from other macros, but in the mean time, I want to figure out how to fix this bug so I can import the FreeCAD library in the first place. I'm not sure if its how I setup my environment in my steps, a bug with FreeCAD or something else with why I get the bug in the title.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by onekk »

To show parameters and similar Python console will suffice.

or even:

Code: Select all

print(dir(something))

or

help(something)

in Python Console or even simply the Help functions contain an item about API or similar that will show some information (maybe too detailed)

An in Python Console if you write as example:

Code: Select all

App.


as doon as you hit the . (point) you will have a popup eith some possible autocompletions and if you hover on them with mouse you have popups with further infos.

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/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by heda »

so, not on a quest to hack new interop ways for fc...
but introspection of what is there.

your choice if you want to travel a muddy road during rain season or the smooth paved path.
recommendation is to go the paved path...

hard to tell if you understand how these things work or not (i'm leaning towards not).
anyhow, as mentioned you are going over the bridge to fetch water...

a) in your venv install thonny (editor), set the py-interpreter to the fc python (probably works, at least i have made it work, but that does not say that it will for you...)

b) forget your venv and python.org install, just fire up the fc-bundled python and everything is already there...
it is located in <your install dir>/bin, there you also find freecadcmd.exe, does not matter which one you use... (here you can pip install anything you want, assuming there is a compatible pip-package to fc-py)

c) use the gui console coming with fc (like most others do...)

d) try scanObjects macro, link on Macros_recipes (code & scripting), dunno if it works...

e) Debugging

fc simply is not a pip-installable package - it is a c application which happens to have a python bundled.
this is not a bug, it is by design, and your issue imho is a matter of you setting up the system at your computer...
python is and has always been cranky on path definition/discovery, as example, setting path in a session does absolutely nothing for a process call, since that is governed by environment (and for that your venv will do nothing, unless you tell it to, originating from the (hard) way you are approaching things)

a) as such is simple (if it works), but why ever do that? (and it really is b...)
e) is the most involved way, you might be lucky that it works, and then again you might not be... and if not you are on your own in sorting it out on your computer
c) you are up and running as soon as you have started the gui, and everything is available without you lifting a finger...

choice is yours what you want to spend time on...
ryn
Posts: 12
Joined: Tue Jun 14, 2022 7:50 pm

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by ryn »

heda wrote: Fri Jun 17, 2022 9:41 pm so, not on a quest to hack new interop ways for fc...
but introspection of what is there.

your choice if you want to travel a muddy road during rain season or the smooth paved path.
recommendation is to go the paved path...

hard to tell if you understand how these things work or not (i'm leaning towards not).
anyhow, as mentioned you are going over the bridge to fetch water...

a) in your venv install thonny (editor), set the py-interpreter to the fc python (probably works, at least i have made it work, but that does not say that it will for you...)

b) forget your venv and python.org install, just fire up the fc-bundled python and everything is already there...
it is located in <your install dir>/bin, there you also find freecadcmd.exe, does not matter which one you use... (here you can pip install anything you want, assuming there is a compatible pip-package to fc-py)

c) use the gui console coming with fc (like most others do...)

d) try scanObjects macro, link on Macros_recipes (code & scripting), dunno if it works...

e) Debugging

fc simply is not a pip-installable package - it is a c application which happens to have a python bundled.
this is not a bug, it is by design, and your issue imho is a matter of you setting up the system at your computer...
python is and has always been cranky on path definition/discovery, as example, setting path in a session does absolutely nothing for a process call, since that is governed by environment (and for that your venv will do nothing, unless you tell it to, originating from the (hard) way you are approaching things)

a) as such is simple (if it works), but why ever do that? (and it really is b...)
e) is the most involved way, you might be lucky that it works, and then again you might not be... and if not you are on your own in sorting it out on your computer
c) you are up and running as soon as you have started the gui, and everything is available without you lifting a finger...

choice is yours what you want to spend time on...
I spent some time looking at what you suggested and here are the conclusions I came to:

a/b) Im using VS-CODE but selecting the interpreter to be FreeCAD's python works inside the interpreter. However, it doesn't detect objects inside FreeCAD, I.E: if there is an active document, FreeCAD.listDocuments() wont see it when running FreeCAD's python via VSCode. I also couldn't manage to get pip to work with FreeCAD as a whole either. FreeCAD doesn't come installed with pip, and I ran into error after error trying to get FreeCAD's python to have it.

c) I'm trying to make the workflow better >.>

d) This macro looks promising. There isn't object inspection with the IDE approach but this looks like the next best thing.

e) There are no intellisense features when you try to remotely attach to FreeCAD. I only managed to make it functionally identical to using the GUI trying this.


Considering all the options, I think the best option to get macro creation to have a better workflow/more features is to go with a two pronged approach:

1. Use the ScanObjects macro/something similar with object inspection to see whats inside modules more quickly

2. Whenever I need access to pip packages, I could have the FreeCAD python instance feed data from and to an external python file in a virtual environment with that package.

FreeCAD not having pip is a bummer, and true parameter hinting/other intellisesnse features isn't possible with how FreeCAD handles python,
but this is better then needing to pull out the wiki for every object/function
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by onekk »

ryn wrote: Mon Jun 20, 2022 8:14 pm FreeCAD not having pip is a bummer, and true parameter hinting/other intellisesnse features isn't possible with how FreeCAD handles python,
but this is better then needing to pull out the wiki for every object/function

FreeCAD is not a Python package.

FreeCAD has his "internal python interpreter", that have enough introspection to be useful when coding.

VSCode is a good editor, but it is MicroSoft so you can't expect too much flexiblity, good thing it is that sources are somewhat open and come free versions could be used, but not everything in VSCode is usable as example on Linux.

Some chances could be obtained with editors, and there are some experiments that will point the "methods recognition" to the sources usually in a conda install where sources are exposed, (I think that also on Windows something is exposed, but usually the nesting of Windows filesystem is very involuted)

I'm coding almost daily with FC and having a copy of FC open in different "screen page" it is so easy to write in "python console":

Code: Select all

help(Part.BezierCurve)

# or maybe

dir(Part.BezierCurve)

and obtain useful infos.

Not counting that you have the source code ready to be inspected on GitHub.

Hope it help.

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/
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by wmayer »

ryn wrote: Thu Jun 16, 2022 5:46 pm BUG: When attempting to import FreeCAD in python, I get the error, "ImportError: DLL load failed while importing FreeCAD: The parameter is incorrect."
Do you encounter this issue only when creating a virtual environment or does it also happen otherwise?
ryn
Posts: 12
Joined: Tue Jun 14, 2022 7:50 pm

Re: BUG: Can't import FreeCAD in python. DLL load failed while importing FreeCAD: The parameter is incorrect.

Post by ryn »

wmayer wrote: Fri Jun 24, 2022 9:19 am
ryn wrote: Thu Jun 16, 2022 5:46 pm BUG: When attempting to import FreeCAD in python, I get the error, "ImportError: DLL load failed while importing FreeCAD: The parameter is incorrect."
Do you encounter this issue only when creating a virtual environment or does it also happen otherwise?
I think I only got that error when I tried to import the FreeCAD from the .pyc file. The only way I managed to not get that error was by setting my interpreter to FreeCAD's python.exe
Post Reply