Skip to content Skip to sidebar Skip to footer

How To Embed Freecad In A Python Virtual Environment?

How do I setup a Python virtual environment with the FreeCAD library embedded as to enable import as a module into scripts? I would like to avoid using the FreeCAD GUI as well as b

Solution 1:

This is what I did to get FreeCAD to work with PyCharm and virtualenv:

Download FreeCAD AppImage

https://www.freecadweb.org/downloads.php

Make AppImage executable

chmod -v +x ~/Downloads/FreeCAD_*.AppImage

Create folder for extracting AppImage

mkdir -v ~/Documents/freecad_appimage

Extract AppImage from folder (note: this expands to close to 30000 files requiring in excess of 2 GB disk space)

cd ~/Documents/freecad_appimage

~/Downloads/./FreeCAD_*.AppImage --appimage-extract 

Create folder for PyCharm project

mkdir -v ~/Documents/pycharm_freecad_project

Create pycharm project using Python interpreter from extracted AppImage

Location: ~/Documents/pycharm_freecad_project
New environment using: Virtualenv
Location: ~/Documents/pycharm_freecad_project/venv
Base interpreter: ~/Documents/freecad_appimage/squashfs-root/usr/bin/python
Inherit global site-packages: False
Make available to all projects: False

Add folder containing FreeCAD.so library as Content Root to PyCharm Project Structure and mark as Sources (by doing so, you shouldn't have to set PYTHONPATH or sys.path values, as PyCharm provides module location information to the interpreter)

File: Settings: Project: Project Structure: Add Content Root
~/Documents/freecad_appimage/squashfs-root/usr/lib

After this PyCharm is busy indexing files for a while.

Open Python Console in PyCharm and run command to check basic functioning

import FreeCAD

Create python script with example functionality

import FreeCAD
vec = FreeCAD.Base.Vector(0, 0, 0)
print(vec)

Run script

Debug script

All FreeCAD functionality I have used in my scripts so far has worked. However, one kink seems to be that the FreeCAD module needs to be imported before the Path module. Otherwise the Python interpreter exits with code 139 (interrupted by signal 11: SIGSEGV).

There are a couple of issues with PyCharm: It is showing a red squiggly line under the import name and claiming that an error has happened because "No module named 'FreeCAD'", even though the script is running perfectly. Also, PyCharm fails to provide code completion in the code view, even though it manages to do so in it's Python Console. I am creating new questions to address those issues and will update info here if I find a solution.

Post a Comment for "How To Embed Freecad In A Python Virtual Environment?"