Import FreeCAD in Python

If you’ve compiled FreeCAD, but don’t know how to import it as a package in Python then read on.

First of all, you need to locate where the FreeCAD.so file is.

locate FreeCAD.so

Mine is in /home/mandeep/Desktop/build/lib.

To be able to do:

import FreeCAD

You’ll have to go to that directory and run python and if you now try to import, it will work. But most probably, you won’t agree to use it as it might feel impractical.

Another method is to append this path to sys.path. For this, you need to edit a file site.py.

For example,

sys.path.append(“/home/mandeep/Desktop/build/lib”)

The site.py would probably be where your python lib directory is. It may probably look like: /usr/lib/python2.7. But I am using the Python within the virtualenv so the python lib is within the virtualenv e.g. venv/lib/python2.7. Here venv is my virtualenv.

Read more about this at https://docs.python.org/2/library/site.html

The idea to edit the site.py file is that it gets initialized with Python. So you can now import your packages from anywhere.

Sourcehttp://www.freecadweb.org/wiki/index.php?title=Embedding_FreeCAD

Referencehttp://stackoverflow.com/a/15109881/3784226

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s