PythonAPI/Import question

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
Post Reply
o0l10
Posts: 15
Joined: Mon Sep 02, 2013 3:39 pm

PythonAPI/Import question

Post by o0l10 »

Hello,
Can someone tell me if the python api from freecadweb.org (http://www.freecadweb.org/wiki/index.ph ... =Draft_API) is current? The code from that page:

Code: Select all

import FreeCAD
from Draft import *
returns:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-24-ffb2a6bed5d2> in <module>()
----> 1 import Draft

ImportError: No module named Draft

i'm importing the modules to Ipython. FreeCAD and FreeCADGui import with a notification:
No modules found in /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/Mod
But otherwise seem to work. I built from the git. FreeCAD.Version() returns:
['0',
'14',
'2433 (Git)',
'https://github.com/FreeCAD/FreeCAD_sf_master.git',
'2013/09/04 12:18:20',
'master',
'8f9a9b71a20aeac0a4f395b194acc47a52e33a6e']
Has the api changed or do I need to do some more build work?
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PythonAPI/Import question

Post by wmayer »

The code is still correct. So, if it doesn't work for you then something with your FreeCAD build is still broken. Your FreeCAD build directory should have a folder "Mod" and inside there you should have all the modules -- among them "Draft".
o0l10
Posts: 15
Joined: Mon Sep 02, 2013 3:39 pm

Re: PythonAPI/Import question

Post by o0l10 »

I've recompiled FreeCAD with some tweaks (hard won tweaks at that) and the build seems to operate without hitch. All the modules seem to load in the gui, no freezes, no errors, no warnings in console.

But I still can't get python to find a module named Draft (or Drawing, or anything that appears in the Mod folder). I've added added ../build/lib to paths, ../, and ../Mod:
>>> import FreeCAD
>>> from Draft import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Draft
>>> print sys.path[19:]
['/Users/---/Freecad_sf_master/build/lib', '/Users/---/Freecad_sf_master/build', '/Users/---/Freecad_sf_master/build/Mod']
Does anyone have any idea what the problem might be?
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PythonAPI/Import question

Post by wmayer »

Can post the result of "print sys.path"? The "---" is that really part of the path or did you change it to hide your user name?
o0l10
Posts: 15
Joined: Mon Sep 02, 2013 3:39 pm

Re: PythonAPI/Import question

Post by o0l10 »

To hide my username from the dataminers :roll:

I didn't build with a template and I just noticed there's a warning when the first time I try to import FreeCAD in python although the second time it seems to load:
In [1]: import sys

In [2]: sys.path.append('/Users/tpierson/Freecad_sf_master/build/lib')

In [3]: sys.path.append('/Users/tpierson/Freecad_sf_master/build/lib')

In [4]: import FreeCAD
FreeCAD 0.14, Libs: 0.14R2436 (Git)
Initialization of FreeCAD failed:
While initializing FreeCAD the following exception occurred: '__import__ not found'


Please contact the application's support team for more information.


In [5]: import FreeCAD

In [6]: FreeCAD.Version()
Out[6]:
['0',
'14',
'2436 (Git)',
'https://github.com/FreeCAD/FreeCAD_sf_master.git',
'2013/09/06 17:58:30',
'master',
'803d2195780a8d87959c9b3a940974f9598e73f3']

In [7]: from Draft import *
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-35841e17dfd8> in <module>()
----> 1 from Draft import *

ImportError: No module named Draft

In [8]: from Fem import *
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-8-36a91fd7b9dd> in <module>()
----> 1 from Fem import *

ImportError: No module named Fem

In [9]: from Part import *
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-9-b06b45e69e44> in <module>()
----> 1 from Part import *

ImportError: No module named Part
and here's the full sys.path with usr name redacted:
['', '/usr/local/bin', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-1.1-py2.7.egg', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg', '/usr/local/lib/python2.7/site-packages/setuptools-1.1-py2.7.egg', '/usr/local/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL', '/Library/Python/2.7/site-packages', '/usr/local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/site-packages/PIL', '/usr/local/lib/python2.7/site-packages/IPython/extensions', '/Users/tpierson/Freecad_sf_master/build/lib', '/Users/*******/Freecad_sf_master/build/lib', '/Users/********/Freecad_sf_master/build/Mod']
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PythonAPI/Import question

Post by wmayer »

To hide my username from the dataminers
OK, no problem.

In this sys.path all entries of the form "/Users/********/Freecad_sf_master/build/Mod/Arch", "/Users/********/Freecad_sf_master/build/Mod/Draft", ... are missing. Actually it should retrieve the path of the FreeCAD module and relative to this path it goes one directory up and add all sub-directories in the Mod directory. So it seems it fails to get the (correct) path of the FreeCAD module and thus doesn't find the modules. So, you should have a look at initFreeCAD() inside MainPy.cpp. Maybe this even never worked.
o0l10
Posts: 15
Joined: Mon Sep 02, 2013 3:39 pm

Re: PythonAPI/Import question

Post by o0l10 »

Ah, I see how it works. I looked at the .cpp but don't know C++ well enough to know what the problem would be. (My 'hard won tweaks' were noob tweaks involving install sources; that's why they were hard won.) What in the cmake could have broken it?

*EDIT* if I add the subfolders under Mod to sys.path, I can, unsurprisingly, import the modules in each subfolder so long as they link to other subfolders already added in the sys.path. There are some errors though:
import sys
sys.path.append('/Users/___/FreeCAD_sf_master/build/lib')
sys.path.append('/Users/___/FreeCAD_sf_master/build/Mod/Draft')
sys.path.append('/Users/___/FreeCAD_sf_master/build/Mod/Part')
sys.path.append('/Users/___/FreeCAD_sf_master/build/Mod/Drawing')
import FreeCAD as fc
import FreeCADGui as fcg
import Draft, Part, Drawing
AttributeError Traceback (most recent call last)
<ipython-input-2-ca1c1dd2ea47> in <module>()
---> 10 import Draft, Part, Drawing
11

/Users/___/FreeCAD_sf_master/build/Mod/Draft/Draft.py in <module>()
78 from FreeCAD import Vector
79
---> 80 if FreeCAD.GuiUp:
81 import FreeCADGui, WorkingPlane
82 gui = True

AttributeError: 'module' object has no attribute 'GuiUp'
And sure enough, fc.GuiUp doesn't exist and trying to import Draft fails.
Post Reply