Issues with pythonically importing FreeCAD (after conda install)

Need help, or want to share a macro? Post here!
User avatar
onekk
Posts: 813
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Issues with pythonically importing FreeCAD (after conda install)

Postby onekk » Fri Mar 12, 2021 3:16 pm

Suppose you want something added to the PATH, I use this to import the location of a multimodule script in PATH at the beginning of the script, and avoid to import it many time

MODULE_PATH musto contain the PATH you want imported

Code: Select all


if not MODULE_PATH in sys.path:
    sys.path.insert(-1,MODULE_PATH)
I suppose that append will work, insert(-1, ....) will insert the thing in the last position of list, different way of specifying things.

Butfrom the little I know about conda, conda is working like a container specifying the module path is working only in the "same environment" on which you install freecad.

Maybe it useful to see:

https://wiki.freecadweb.org/Embedding_FreeCAD/

see the Caveats at the bottom of page

Regards

Carlo D.
Introduzione a FreeCAD in Italiano: https://github.com/onekk/freecad-doc
galou_breizh
Posts: 354
Joined: Wed Sep 15, 2010 9:38 am

Re: Issues with pythonically importing FreeCAD (after conda install)

Postby galou_breizh » Thu Apr 01, 2021 7:11 am

Why I did to have FreeCAD available in my $PYTHONPATH is

Code: Select all

cd $HOME/.local/lib/python3.8/site-packages && for f in /usr/lib/freecad-daily-python3/lib/*.so; do ln -s $f; done
Adapt to your FreeCAD version if you don't use FreeCAD Daily.

I wish that a package manager will put the *.so files in the correct place in the future (but maybe not for the daily version, so that both version can be installed concurrently).

Gaël

OS: Ubuntu 20.04.2 LTS (i3/i3)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.
Build type: Release
Branch: unknown
Hash: e8566f22bbeb0b7204e3c45519d0963e8881100b
Python version: 3.8.5
Qt version: 5.12.8
Coin version: 4.0.0
OCC version: 7.5.1
Locale: English/United States (en_US)
User avatar
onekk
Posts: 813
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Issues with pythonically importing FreeCAD (after conda install)

Postby onekk » Thu Apr 01, 2021 2:20 pm

The problem with conda, is that from what i remember when I've tried it it's a sort of protected environment, where each "program" use "his own" environment, so when you fire a "conda package" it set "his own" environment I don't know how, but there is some documentation around, this mechanism isolate the "conda package" from the "host OS", so using some thing are doable only in the "conda package" environment.

I have tried to set up a conda environment, but I've gave up because it will download almost an entire OS to use in the "conda package" environment.

It is a sort of container, where only some basic system libraries are used (kernel and glibc and few others I suppose) but all the relevant other libraries are installed as a "per package" needs, to avoid compatibility problems.

the "OS python interpreter" is not aware of the "directory tree" of the "conda package" so it is not included, but chances are that even importing the "conda package" libraries hey are compiled for a different version of python so maybe it is not very "safe" and "error free".

Maybe it is doable to reuse the "conda package" python interpreter to launch a python program not packaged in the "conda package", and this could be the better way to reuse the "freecad conda environment", but this is a problem to be asked maybe to the people that make the "freecad conda package".

Hope it helps

Regards

Carlo D.
Introduzione a FreeCAD in Italiano: https://github.com/onekk/freecad-doc
User avatar
looo
Posts: 3630
Joined: Mon Nov 11, 2013 5:29 pm

Re: Issues with pythonically importing FreeCAD (after conda install)

Postby looo » Thu Apr 01, 2021 2:31 pm

If you install freecad via conda into a conda environment you should be able to import freecad from python via:

Code: Select all

import freecad
This will do the path modification.

But this is only true if you work within the conda-environment (virtual environment). It's not suggested to import from the system python as this will lead to issues because of mixed.dependencies...
User avatar
gbroques
Posts: 106
Joined: Thu Jan 23, 2020 3:28 am
Location: St. Louis, Missouri

Re: Issues with pythonically importing FreeCAD (after conda install)

Postby gbroques » Thu Apr 01, 2021 11:01 pm

I wrote a tiny shell script that can be executed one time, once you have a desired conda environment activated.

It will allow you to import FreeCAD, FreeCADGui, Part, etc. from <my_script.py> when executing:

Code: Select all

$ python <my_script.py>
See the following shell script:
https://github.com/gbroques/freecad-to- ... on_path.sh

Code: Select all

#!/bin/sh
# ----------------------------------------------------------------------------------
# Must be executed when a desired conda environment is activated.
#
# By adding the $CONDA_PREFIX/lib directory to $PYTHONPATH, python is able to find
# FreeCAD, FreeCADGui, and other various FreeCAD-related modules during execution.
# ----------------------------------------------------------------------------------
cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh

echo '#!/bin/sh' >> ./etc/conda/activate.d/env_vars.sh
echo 'export INITIAL_PYTHONPATH=${PYTHONPATH}' >> ./etc/conda/activate.d/env_vars.sh
echo "export PYTHONPATH=$CONDA_PREFIX/lib:\${PYTHONPATH}" >> ./etc/conda/activate.d/env_vars.sh

echo '#!/bin/sh' >> ./etc/conda/deactivate.d/env_vars.sh
echo 'export PYTHONPATH=${INITIAL_PYTHONPATH}' >> ./etc/conda/deactivate.d/env_vars.sh
echo 'unset INITIAL_PYTHONPATH' >> ./etc/conda/deactivate.d/env_vars.sh
Basically, it adds FreeCAD's lib/ directory, with all the .so or .dll files, to PYTHONPATH when you activate your conda environment.

Then it removes FreeCAD's lib/ directory from PYTHONPATH when you deactivate the conda environment.

See the following conda docs for more information:
https://docs.conda.io/projects/conda/en ... -variables

This is equivalent to sys.path.append("path/to/lib") or insert at the top of the script, but way cleaner.
User avatar
gbroques
Posts: 106
Joined: Thu Jan 23, 2020 3:28 am
Location: St. Louis, Missouri

Re: Issues with pythonically importing FreeCAD (after conda install)

Postby gbroques » Thu Apr 01, 2021 11:24 pm

looo wrote: :bell:
Would it be possible to add a post-link.sh to the freecad conda package so the standard FreeCAD is always ready to be imported from a Python script when a conda environment is activated?

See the following post-link.sh:
https://github.com/gbroques/ose-workben ... st-link.sh

Code: Select all

#!/bin/sh
# ----------------------------------------------------------------------------------
# Executed after the package is installed.
# An error is indicated by a nonzero exist and causes installation to fail.
# If there is an error, conda does not write any package metadata.
#
# By adding the $CONDA_PREFIX/lib directory to $PYTHONPATH, python is able to find
# FreeCAD, FreeCADGui, and other various FreeCAD-related modules during execution.
# 
# See Also:
#   https://docs.conda.io/projects/conda-build/en/latest/resources/link-scripts.html
# ----------------------------------------------------------------------------------
cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh

echo '#!/bin/sh' >> ./etc/conda/activate.d/env_vars.sh
echo 'export INITIAL_PYTHONPATH=${PYTHONPATH}' >> ./etc/conda/activate.d/env_vars.sh
echo "export PYTHONPATH=$CONDA_PREFIX/lib:\${PYTHONPATH}" >> ./etc/conda/activate.d/env_vars.sh

echo '#!/bin/sh' >> ./etc/conda/deactivate.d/env_vars.sh
echo 'export PYTHONPATH=${INITIAL_PYTHONPATH}' >> ./etc/conda/deactivate.d/env_vars.sh
echo 'unset INITIAL_PYTHONPATH' >> ./etc/conda/deactivate.d/env_vars.sh
and conda's docs on it:
https://docs.conda.io/projects/conda-bu ... ripts.html
User avatar
looo
Posts: 3630
Joined: Mon Nov 11, 2013 5:29 pm

Re: Issues with pythonically importing FreeCAD (after conda install)

Postby looo » Sat Apr 03, 2021 3:04 pm

gbroques wrote: Thu Apr 01, 2021 11:24 pm
looo wrote: :bell:
Would it be possible to add a post-link.sh to the freecad conda package so the standard FreeCAD is always ready to be imported from a Python script when a conda environment is activated?

See the following post-link.sh:
https://github.com/gbroques/ose-workben ... st-link.sh

Code: Select all

#!/bin/sh
# ----------------------------------------------------------------------------------
# Executed after the package is installed.
# An error is indicated by a nonzero exist and causes installation to fail.
# If there is an error, conda does not write any package metadata.
#
# By adding the $CONDA_PREFIX/lib directory to $PYTHONPATH, python is able to find
# FreeCAD, FreeCADGui, and other various FreeCAD-related modules during execution.
# 
# See Also:
#   https://docs.conda.io/projects/conda-build/en/latest/resources/link-scripts.html
# ----------------------------------------------------------------------------------
cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh

echo '#!/bin/sh' >> ./etc/conda/activate.d/env_vars.sh
echo 'export INITIAL_PYTHONPATH=${PYTHONPATH}' >> ./etc/conda/activate.d/env_vars.sh
echo "export PYTHONPATH=$CONDA_PREFIX/lib:\${PYTHONPATH}" >> ./etc/conda/activate.d/env_vars.sh

echo '#!/bin/sh' >> ./etc/conda/deactivate.d/env_vars.sh
echo 'export PYTHONPATH=${INITIAL_PYTHONPATH}' >> ./etc/conda/deactivate.d/env_vars.sh
echo 'unset INITIAL_PYTHONPATH' >> ./etc/conda/deactivate.d/env_vars.sh
and conda's docs on it:
https://docs.conda.io/projects/conda-bu ... ripts.html
Thx, good idea. I will try to integrate this in the next days.