How to run the freecad-python scripts in the command line?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Tom28
Posts: 6
Joined: Sun Aug 13, 2017 10:56 am

How to run the freecad-python scripts in the command line?

Post by Tom28 »

Hi,

I have a pyhton script which works well in the FreeCad Python console.

But I want to run it through the command-line in order to get it automated.

I searched a lot and tried so far two things:
- opening the FreeCadCmd.exe but there seems no python scripts working
- opening the python.exe in the /bin folder of Freecad. This seems also not to work when I put there the python code ... or do I need to load some librarys first?

Does anyone have experience with that?

Many thanks

Tom
GSR
Posts: 8
Joined: Fri Aug 04, 2017 6:56 am

Re: How to run the freecad-python scripts in the command line?

Post by GSR »

Hi Tom28,

On my machine, all I need to do is add the path to the FreeCAD libraries to the system path and then import FreeCAD from there. I add the following code block at the top of my scripts with the rest of my module imports.

-GSR

Code: Select all

# --------------------------------------------------------------------------- #
# FreeCAD related modules
# For 0.16 stable
sys.path.append('/usr/lib/freecad/lib')
# For 0.17 daily developmental
# sys.path.append('/usr/lib/freecad-daily/lib')
import FreeCAD
import Part
import Sketcher
Leatherman
Posts: 155
Joined: Thu Feb 02, 2017 2:49 pm

Re: How to run the freecad-python scripts in the command line?

Post by Leatherman »

GSR wrote: Sun Aug 13, 2017 8:08 pm Hi Tom28,

On my machine, all I need to do is add the path to the FreeCAD libraries to the system path and then import FreeCAD from there. I add the following code block at the top of my scripts with the rest of my module imports.

-GSR

Code: Select all

# --------------------------------------------------------------------------- #
# FreeCAD related modules
# For 0.16 stable
sys.path.append('/usr/lib/freecad/lib')
# For 0.17 daily developmental
# sys.path.append('/usr/lib/freecad-daily/lib')
import FreeCAD
import Part
import Sketcher
That's a good one. Thank you. :D
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: How to run the freecad-python scripts in the command line?

Post by ickby »

Normally all you need to do is to pass the script as command line argument, like

Code: Select all

FreeCADcmd.exe myScript.py
(syntax is for windows, but the principle holds for all Operating Systems)
Tom28
Posts: 6
Joined: Sun Aug 13, 2017 10:56 am

Re: How to run the freecad-python scripts in the command line?

Post by Tom28 »

Hi,
thanks a lot I will try both and inform you if it is working

Many thanks

Tom
triplus
Veteran
Posts: 9471
Joined: Mon Dec 12, 2011 4:45 pm

Re: How to run the freecad-python scripts in the command line?

Post by triplus »

There is one more option available from the FreeCAD GUI:

https://forum.freecadweb.org/viewtopic. ... 76#p180338
Tom28
Posts: 6
Joined: Sun Aug 13, 2017 10:56 am

Re: How to run the freecad-python scripts in the command line?

Post by Tom28 »

ickby wrote: Mon Aug 14, 2017 5:43 am Normally all you need to do is to pass the script as command line argument, like

Code: Select all

FreeCADcmd.exe myScript.py
(syntax is for windows, but the principle holds for all Operating Systems)
This works for me and seems for me a very easy way to accomplish this

Many thanks to all

Tom
ajarivas72
Posts: 8
Joined: Sun Sep 11, 2016 10:35 pm

Re: How to run the freecad-python scripts in the command line?

Post by ajarivas72 »

Hello

In Linux use the following command:

freecadcmd myscript.py
luimuyi
Posts: 4
Joined: Sat Aug 01, 2020 1:56 pm

Re: How to run the freecad-python scripts in the command line?

Post by luimuyi »

ajarivas72 wrote: Sun Sep 23, 2018 5:14 am Hello

In Linux use the following command:

freecadcmd myscript.py
I tried this command on Linux, but it will report an error:'[Cannot load Gui module in console application.]'
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: How to run the freecad-python scripts in the command line?

Post by openBrain »

luimuyi wrote: Sun Aug 02, 2020 1:47 am
ajarivas72 wrote: Sun Sep 23, 2018 5:14 am Hello

In Linux use the following command:

freecadcmd myscript.py
I tried this command on Linux, but it will report an error:'[Cannot load Gui module in console application.]'
Which seems logical. 'freecadcmd' is the CLI command so it has no awareness of the GUI part.
If your script is UI based, you have to run 'freecad my script.py'.
Post Reply