FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by Syres »

Because it's not in your path??? see below:

Code: Select all

>>> import Part
>>> import PartDesign
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'PartDesign'
>>> path = os.path.join(path_base, "Mod")
>>> sys.path.append(path)
>>> import PartDesign
>>>
User avatar
amrit3701
Posts: 343
Joined: Mon Jun 13, 2016 5:37 pm

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by amrit3701 »

Aleks wrote: Wed Sep 15, 2021 1:17 pm I did not compile FreeCAD. I just downloaded it prebuild. I am using the system installation of python to access FreeCAD through the python interface.
If you want to use FreeCAD (without GUI elements), then you can try the FreeCAD docker image (compiled without GUI elements).
https://wiki.freecadweb.org/FreeCAD_Docker_CLI_mode
Source code of docker image: https://github.com/amrit3701/docker-freecad-cli

For curiosity, I tested in the docker image and was able to import the PartDesign workbench.


root@f2fb577d7f67:/app# ipython
Python 3.8.5 (default, Jul 20 2020, 19:48:14)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.28.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import FreeCAD

In [2]: import Part

In [3]: import PartDesign

In [4]: import Path

In [5]: import Arch
Amritpal Singh
Github, Like my work, sponsor me!
m.cavallerin
Posts: 115
Joined: Wed May 30, 2018 6:59 pm

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by m.cavallerin »

Syres wrote: Fri Sep 24, 2021 3:35 pm Because it's not in your path??? see below:

Code: Select all

>>> import Part
>>> import PartDesign
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'PartDesign'
>>> path = os.path.join(path_base, "Mod")
>>> sys.path.append(path)
>>> import PartDesign
>>>
Where do you import PartDesign Module From? What is your path_base?
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by Aleks »

I did solve this by importing _PartDesign instead of PartDesign. Can someone explain what _PartDesign is?
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by onekk »

See:

https://github.com/FreeCAD/FreeCAD/blob ... signPy.cpp

Code: Select all

namespace PartDesign {
class Module : public Py::ExtensionModule<Module>
{
public:
    Module() : Py::ExtensionModule<Module>("_PartDesign")
    {
        add_varargs_method("makeFilletArc",&Module::makeFilletArc,
            "makeFilletArc(...) -- Fillet arc."
        );
        initialize("This module is the PartDesign module."); // register with Python
    }

    virtual ~Module() {}
I suppose that is the name of the C++ part of the Module, declared in:

Code: Select all

Module() : Py::ExtensionModule<Module>("_PartDesign")
But I'm not an expert of C++ and Python interactions.

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/
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by Aleks »

I thought I had this working, but coming back to it today it looks like it does not work.

When loading a document containing PartDesign objects (body, Pad, Pocket,...) from an external python interpreter (same version as FreeCAD Python interpreter) all the PartDesign objects are not recognized. Operating system is Ubuntu 20.04 without GUI.

This is the result that I get when opening the file through freecadcmd

Code: Select all

<body object>
<App::Origin object>
<GeoFeature object>
<GeoFeature object>
<GeoFeature object>
<GeoFeature object>
<GeoFeature object>
<GeoFeature object>
<Sketcher::SketchObject>
<PartDesign::Pad>
<Sketcher::SketchObject>
<PartDesign::Pocket>
<Sheet object>
<DrawPage object>
<DrawSVGTemplate object>
<DrawProjGroup object>
<DrawProjGroupItem object>
<DrawProjGroupItem object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<Path::FeaturePython object>
<Path::FeatureCompoundPython object>
<App::FeaturePython object>
<group object>
<Part::PartFeature>
<Path::FeaturePython object>
<Part::PartFeature>
<Path::FeaturePython object>
<Path::FeaturePython object>
<group object>
This is what I get when loading through an external interpreter with 'usr/lib/freecad-python3/lib/' added to path. All the PartDesign objects do not get recognized:

Code: Select all

<App::Origin object>
<GeoFeature object>
<GeoFeature object>
<GeoFeature object>
<GeoFeature object>
<GeoFeature object>
<GeoFeature object>
<Sketcher::SketchObject>
<Sketcher::SketchObject>
<Sheet object>
<DrawPage object>
<DrawSVGTemplate object>
<DrawProjGroup object>
<DrawProjGroupItem object>
<DrawProjGroupItem object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<DrawViewDimension object>
<Path::FeaturePython object>
<Path::FeatureCompoundPython object>
<App::FeaturePython object>
<group object>
<Part::PartFeature>
<Path::FeaturePython object>
<Part::PartFeature>
<Path::FeaturePython object>
<Path::FeaturePython object>
I tried

Code: Select all

import PartDesign

Returns

Code: Select all

ModuleNotFoundError: No module named 'PartDesign'
and

Code: Select all

import _PartDesign
Does not seen to help

This might be related to viewtopic.php?style=3&t=68354 but I dont fully understand what they were discussing. @drmacro and @onekk are you able to help with this?
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by onekk »

Aleks wrote: Wed Apr 19, 2023 2:19 pm ...
@onekk are you able to help with this?
Maybe, the use of an external python interpreter IMHO is a overcomplicated thing, but I could only note one thing:

- PartDesign is a Python module, so it reside in /Mod directory

probably adding to path 'usr/lib/freecad-python3/lib/' is not enough.

I will try to add even explicitly the /mod directory of "FreeCAD sources" in my case '/usr/lib/freecad/Mod/' to the python path but your mileage may vary or I probably I've guessed wrong.

Hope it helps

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/
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by Aleks »

Adding 'usr/lib/freecad/Mod' to path did the trick. I also vaguely understand why it needed to be added. it works now.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by onekk »

Aleks wrote: Thu Apr 20, 2023 3:22 pm Adding 'usr/lib/freecad/Mod' to path did the trick. I also vaguely understand why it needed to be added. it works now.
Happy to hear I've guessed right.

Python path is populated correctly if you use freecadcmd, as it initialize FreeCAD and probably one action is setting path correctly pointing to Mod directory and even user/Mod directory too.

If you use it from a foreign interpreter (asa library) probably the init part is not executed when you issue

Code: Select all

import FreeCAD
so you have to add all the directory manually or maybe check if they are already added.

I use this code with multifile scripts to avoid multiple path imports when developing, as at first start script directory is not in the path, but appending it multiple times is not very clever.

Code: Select all

if PATH in sys.path:
    pass
else:
    sys.path.append(PATH)
Hoping to have remembered code right (I'm far from a computer).

A little note, I use AppImage so FreeCAD python interpreter version is usually not the same as system version and it is "sort of" isolated from the system, I'm using a distribution supplied version (except for adrianinsaval Arch Linux version I use for FreeCAD-dev).

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/
User avatar
Aleks
Posts: 309
Joined: Sun Mar 08, 2020 5:27 pm
Location: Bonn, Germany
Contact:

Re: FreeCAD external Python interpreter will not return body object on Ubuntu 20.04

Post by Aleks »

onekk wrote: Fri Apr 21, 2023 5:48 am Happy to hear I've guessed right.
Thank you for pointing me in the right direction. Also thank you to everyone else for your input.

Thats probably also why it is called _PartDesign so when you have both directories '.../lib' and '.../Mod' there is just one PartDesign that you can import and the interpreter is not confused. But then I ask myself what the purpose of _PartDesign is at all.

I have added a list of Tips to the initial post on the first page of this topic.
FreeCAD als Maschinenbauer in die Konstruktion und Fertigung integrieren. Schulung buchen: www.alsado.de/freecad-schulungen
Post Reply