View Issue Details

IDProjectCategoryView StatusLast Update
0002906FreeCADBugpublic2017-06-29 05:37
ReporterRubinous Assigned Toian.rees  
PrioritynormalSeveritymajorReproducibilityalways
Status closedResolutionfixed 
PlatformMacOSmacOSOS Version10.12
Product Version0.16 
Target VersionFixed in Version0.17 
Summary0002906: Python API error: No module named PartDesign
DescriptionI have a pure python program where I try to call `doc.addObject("PartDesign::Pad","Pad")` where `doc` is a document object. This call causes the following error: `Base.FreeCADError: FreeCAD exception thrown (No module named PartDesign)`.
Steps To ReproduceRun the following code snippet in a python console in macOS that has FreeCAD 0.16 installed in the Applications folder:
```
import sys

sys.path.append(u"/Applications/FreeCAD.app/Contents/lib")

import FreeCAD

doc = FreeCAD.newDocument("test")
doc.addObject("PartDesign::Pad","Pad")
```
TagsMacOSX, python
FreeCAD Information

Relationships

related to 0002927 assignedian.rees Mac application bundler doesn't track library versions 

Activities

ian.rees

2017-02-18 02:10

developer   ~0008342

Last edited: 2017-02-18 02:11

View 2 revisions

Hi Rubinous,

I'm not sure that what you're trying is meant to work - are you basing this on some existing documentation?

My understanding is that the Python interpreter needs to be built with the same C compiler, as the one used to build any compiled libraries (like the ones you're trying to use in FreeCAD) used with it. So, if the build system that made your FreeCAD is not using the same compiler that (presumably Apple?) used to make the python interpreter you're using, then I wouldn't expect it to work. Could be wrong though!

So, I'd suggest having a look through the "Python scripting and macros" section of the forum - https://forum.freecadweb.org/viewforum.php?f=22 and look for prior art.

The most portable solution is probably to structure your script to be run from within FreeCAD's Python interpreter. The most flexible solution is probably to build FreeCAD from source on your system - it's actually pretty easy these days using Homebrew (though let someone know if you find bugs in https://www.freecadweb.org/wiki/CompileOnMac - I intend to clean up & update those soon).

If you do decide this is something that is supposed to work, then could you copy-and-paste your FreeCAD version info, and the header you get when running the python interpreter in interactive mode? Thanks!

(Edited to remove unintentional markup)

Rubinous

2017-02-20 10:11

reporter   ~0008383

Hi,

> I'm not sure that what you're trying is meant to work - are you basing this on some existing documentation?

Yes, I'm basing this on https://www.freecadweb.org/wiki/Embedding_FreeCAD

>
My understanding is that the Python interpreter needs to be built with the same C compiler, as the one used to build any compiled libraries (like the ones you're trying to use in FreeCAD) used with it. So, if the build system that made your FreeCAD is not using the same compiler that (presumably Apple?) used to make the python interpreter you're using, then I wouldn't expect it to work. Could be wrong though!

I'm using the compiled package of FreeCAD. I tried to compile it myself earlier, but there were some issues. I'm not sure which instructions I was reading on Thursday, but https://www.freecadweb.org/wiki/CompileOnMac looks different than what I read, so I'm going to retry and report back to you. Maybe you already updated that or maybe I was reading some other instructions.

> So, I'd suggest having a look through the "Python scripting and macros" section of the forum - https://forum.freecadweb.org/viewforum.php?f=22 and look for prior art.

Thanks for the resource.

I'm trying to do this, because I need to use FreeCAD without its GUI form my application. Coincidentally, I'm writing my application in python, so the prettiest solution would be importing FreeCAD modules to my application and calling the functions I need.

What I need, is to open a FreeCAD document, change parameters of some geometrical objects, and export the 3D model in another format. The file I'm opening, has a `PartDesign::Pad` type of object in it and when I'm opening the file, it reports `Cannot create object 'Pad': (No module named PartDesign)` in the console, but the program doesn't crash. However, when I call `doc.getObject("Pad")`, it returns `None`, so clearly something went wrong. Instead of reporting this, I reported the problem with `.addObject("PartDesign::Pad", ...)` as I'm guessing these errors have the same root cause, but the `addObject` case was simpler to explain.

The code works fine in the python shell found in FreeCAD GUI. But as I explained, I need to be able to use FreeCAD programmatically without opening the GUI.

I'm sorry I couldn't reply during weekend. But I'm going to monitor this actively during work hours as this is highly important regarding my ongoing project.

Rubinous

2017-02-20 10:31

reporter   ~0008384

Oh, and I wanted to add that I noticed that in /Applications/FreeCAD.app/Contents/lib, the part design module is named "_PartDesign" with underscore, while all the other modules are named like "FreeCAD" or "Part" or "Path" i.e. without underscore.

Rubinous

2017-02-20 13:07

reporter   ~0008385

I successfully built the HEAD on macOS. This didn't solve my problem though. Now the bugged code can't find any modules. That's probably because the modules are not in the lib directory anymore. I added Mod directory to path as well, as that's where the modules seem to be. But those are not importable. Maybe because they're not .so files anymore. I don't know.

The good news is that I found bin/FreeCADCmd, a command line utility, which I can use without the GUI. It's not as pretty as using FreeCAD modules directly in my application's python code (e.g. web server), but this'll suffice.

Rubinous

2017-02-20 13:09

reporter   ~0008386

In any case, https://www.freecadweb.org/wiki/Embedding_FreeCAD explains that you could embed FreeCAD directly to your own python application, which seems to me is currently impossible because of how the modules are organised.

Kunda1

2017-02-20 14:06

administrator   ~0008387

@Rubinous thanks for reaching out. In future please open a forum thread first to discuss your issue. We don't have our bug tracker guidelines promintently available ATM because we just upgraded the tracker and the UI is still being ported over. Here is a link to the tracker guidelines: https://www.freecadweb.org/wiki/Tracker#Reporting_bugs

Rubinous

2017-02-20 15:26

reporter   ~0008388

Also found this:

> FreeCAD can be imported as a Python module, inside other applications that can run Python scripts, or in a Python console. Like in console mode, the interface part of FreeCAD is unavailable, but all geometry tools are accessible.

from https://freecadweb.org/wiki/Feature_list

ian.rees

2017-02-22 17:39

developer   ~0008408

Rubinous - Sorry about the delayed response. I think you've found a real issue, really appreciate the reporting and investigation.

Glad to hear you've got FreeCAD to build! Yes, I did some reworking of the instructions, let me know if anything there was unclear.

There's a CMake variable (which we probably should set to default ON, but you would've seen in the instructions) FREECAD_CREATE_MAC_APP that will cause "make install" to create a new MacOS application bundle wherever you've got the install prefix set to. That process does a bunch of rearranging and setting library paths within the bundle*, but doesn't alter the contents of the build directory. I think that, if this is going to work, you'll need to set your PYTHONPATH to point in to the application bundle you've created with that tool, rather than pointing an external Pythin at your FreeCAD build directory.

* Just to complicate things, we've been doing some work on that script, and there's currently a pending PR https://github.com/FreeCAD/FreeCAD/pull/547 . That will likely be merged within hours, and could help this a lot. Probably the best plan is to wait until it's been merged, pull the latest git source, and re-run the cmake, make, and 'make install' steps.

If the above doesn't work, could you share some more details on the versions of your Python (the chunk of output when the interpreter starts up, with C compiler info, would be great), OS (MacOS version, and what package manager you're using), output from "c++ --version", and FreeCAD version info (run FreeCAD GUI, start Part Design, then copy info from "About FreeCAD")?

We should probably stick to the version of FreeCAD you built from source, assuming that it generally works OK in other usage. It's a moving target, but it can take a lot more effort to figure this stuff out with old code, which might've been fixed already. Once we know what's going on, we might be able to get it working with a more stable version of FC if that's important for your work.

Would be great to get to the bottom of your issue, even if that means updating the documentation.

Kunda1 - I think that Rubinous was fine in reporting this here. Don't want to dissuade people from reporting bugs...

Rubinous

2017-02-24 10:08

reporter   ~0008418

This isn't urgent to me anymore, because I found the FreeCADCmd. I'm now calling FreeCADCmd as a subprocess from my main python application and feeding it python scripts that I generate on the fly. It is not the prettiest solution, but it gets the work done.

But I'm willing to help you debug this ticket as long as you're interested in the subject, because I want to see CAD software have better APIs. FreeCAD's API is perhaps the most potential of them all, because python is such a powerful tool to build the API on imo. Compare to e.g. SolidWorks' Visual Basic API – it's awful. I'm also looking into Onshape's web API currently as that's the only other CAD API that doesn't require opening the GUI.

I will rebuild my FreeCAD with `FREECAD_CREATE_MAC_APP=1` as soon as https://github.com/FreeCAD/FreeCAD/pull/547 is merged in, add the bundle lib in `PYTHONPATH` and see if I can run the above code, and report back here.

ian.rees

2017-02-24 21:47

developer   ~0008427

Rubinous, thanks for the offer of help. I would like to get this figured out, and update the instructions as required. Have been playing around with the issue on my machine this morning, and can reproduce what you're seeing. So, no worries about doing more testing for now.

I'm curious in general about the constraints that keep you from using the Python inside FreeCAD?

Rubinous

2017-02-28 14:12

reporter   ~0008474

@ian.rees, this bug report is probably not the best medium for that discussion. Could you contact me via email?

ian.rees

2017-03-02 07:46

developer   ~0008497

Last edited: 2017-03-13 15:19

View 2 revisions

Would do, but I don't have permissions to see your email address. Mine is my username here, at gmail.

I've opened issue 0002927 to fix a problem that's getting in the way of me working on your issue. I expect that'll only take a couple evenings to fix, but I probably won't have those for FreeCAD until next week sometime.

Kunda1

2017-03-13 15:19

administrator   ~0008620

FYI @Rubinous per @ian.rees
> Would do, but I don't have permissions to see your email address. Mine is my username here, at gmail.

ian.rees

2017-03-14 00:48

developer   ~0008623

Kunda1 you have edited my comment, and also and reposted part of the same comment immediately after it. The first is a bit rude, the second is nothing but unnecessary noise.

What exactly are you trying to achieve/communicate/fix? Are you implying that Rubinous hasn't seen my comment? Trying to teach us that we can @mention people (who already receive email notifications)? Suggesting that we haven't moved on this item fast enough?

Kunda1

2017-03-14 03:48

administrator   ~0008624

ian.rees, linking the issue number using the mantisbt markup JIC in the future mantis will support better ticket linking, that's all. I was also making sure that OP connected with you off-tracker and that this ticket wouldn't subject to rot in the tracker like a bunch of others I've encountered. Thanks for the heads up and your feedback. I'll be less 'invasive' with tickets that you are more active on. Cheers.

ian.rees

2017-03-17 09:15

developer   ~0008642

Last edited: 2017-03-17 09:16

View 2 revisions

Rubinous,

    The main issue here is that FreeCAD identifies the path to modules based on the path to the current running executable - usually FreeCAD's. But, in your case, the current running executable is the external Python interpreter, and the FreeCAD modules aren't in a predetermined path relative to it.

I have a fix prepared, but want to step through a few scenarios tomorrow and make sure it works well before issuing a PR.

There's an additional step that might be required in setting the dynamic loader search path - this can be fixed if it's not specific to my machine. If you get an error about dlopen() not being able to find libFreeCADApp.dylib, you might need to do something like this before running your Python script:
export DYLD_LIBRARY_PATH=/path/to/FreeCAD.app/Contents/lib
. Or, I suppose that could be done from Python too.

ian.rees

2017-03-17 22:51

developer   ~0008646

Pull request https://github.com/FreeCAD/FreeCAD/pull/624 should fix the main issue on MacOS.

I've also done a little bit of tidying up in the wiki pages mentioned above - some of those are really quite stale...

Kunda1

2017-06-29 05:22

administrator   ~0009597

FreeCAD Pull Request 624

Kunda1

2017-06-29 05:37

administrator   ~0009598

Associated commit to ticket and closing ticket per 0002906:0008646

Related Changesets

FreeCAD: master caee7cae

2017-03-17 05:26:55

ian.rees


Committer: wmayer Details Diff
Get path on OSX when imported from external Python Affected Issues
0002906
mod - src/App/Application.cpp Diff File
mod - src/App/Application.h Diff File
mod - src/Main/MainPy.cpp Diff File

Issue History

Date Modified Username Field Change
2017-02-17 18:16 Rubinous New Issue
2017-02-18 02:10 ian.rees Note Added: 0008342
2017-02-18 02:11 ian.rees Note Edited: 0008342 View Revisions
2017-02-18 11:30 Kunda1 Status new => feedback
2017-02-20 10:11 Rubinous Note Added: 0008383
2017-02-20 10:11 Rubinous Status feedback => new
2017-02-20 10:31 Rubinous Note Added: 0008384
2017-02-20 13:07 Rubinous Note Added: 0008385
2017-02-20 13:09 Rubinous Note Added: 0008386
2017-02-20 14:06 Kunda1 Note Added: 0008387
2017-02-20 15:26 Rubinous Note Added: 0008388
2017-02-22 17:39 ian.rees Note Added: 0008408
2017-02-23 20:38 Kunda1 Status new => feedback
2017-02-24 10:08 Rubinous Note Added: 0008418
2017-02-24 10:08 Rubinous Status feedback => new
2017-02-24 20:18 ian.rees Assigned To => ian.rees
2017-02-24 20:18 ian.rees Status new => assigned
2017-02-24 20:19 ian.rees Status assigned => confirmed
2017-02-24 21:47 ian.rees Note Added: 0008427
2017-02-28 14:12 Rubinous Note Added: 0008474
2017-03-02 07:46 ian.rees Note Added: 0008497
2017-03-13 15:19 Kunda1 Note Added: 0008620
2017-03-13 15:19 Kunda1 Note Edited: 0008497 View Revisions
2017-03-13 15:20 Kunda1 Relationship added related to 0002927
2017-03-14 00:48 ian.rees Note Added: 0008623
2017-03-14 03:48 Kunda1 Note Added: 0008624
2017-03-17 09:15 ian.rees Note Added: 0008642
2017-03-17 09:16 ian.rees Note Edited: 0008642 View Revisions
2017-03-17 22:51 ian.rees Note Added: 0008646
2017-05-01 22:55 Kunda1 Tag Attached: python
2017-06-29 05:11 Kunda1 Tag Attached: MacOSX
2017-06-29 05:22 Kunda1 Note Added: 0009597
2017-06-29 05:33 Kunda1 Changeset attached => FreeCAD master caee7cae
2017-06-29 05:37 Kunda1 Status confirmed => closed
2017-06-29 05:37 Kunda1 Resolution open => fixed
2017-06-29 05:37 Kunda1 Fixed in Version => 0.17
2017-06-29 05:37 Kunda1 Note Added: 0009598