Running pip from within FreeCAD

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
chennes
Veteran
Posts: 3877
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Running pip from within FreeCAD

Post by chennes »

I'm working on dependency resolution for the Addon Manager, and I'd like to be able to offer the user the ability to (attempt to) install missing Python dependencies. From a normal Python script I'd write something like:

Code: Select all

                subprocess.check_call([sys.executable, "-m", "pip", "--version"])
But of course this doesn't work within FreeCAD, because sys.executable is FreeCAD, not python! How do I figure out what the FreeCAD Python interpreter binary is, so I can execute this sort of command?

ETA: Or, if that doesn't really make sense (since I guess the "binary" really is FreeCAD), how can I use something like "runpy.run_module" to work?

ETA2: Some notes to self:

Code: Select all

# Something like:
import runpy
old_argv = sys.argv
sys.argv=["pip","install","ezdxf"]
sys.argc = len(sys.argv)
runpy.run_module("pip",run_name="__main__")

# This fails because it is expecting an interactive terminal, I think
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
User avatar
gbroques
Posts: 167
Joined: Thu Jan 23, 2020 3:28 am
Location: St. Louis, Missouri

Re: Running pip from within FreeCAD

Post by gbroques »

chennes wrote: Tue Jan 04, 2022 10:44 pm How do I figure out what the FreeCAD Python interpreter binary is, so I can execute this sort of command?
shutil.which may help you.

Code: Select all

>>> import shutil
>>> shutil.which('python')
'/home/g/miniconda3/envs/openafpm-cad-core/bin/python'
Reference: https://stackoverflow.com/questions/987 ... ch-command
User avatar
gbroques
Posts: 167
Joined: Thu Jan 23, 2020 3:28 am
Location: St. Louis, Missouri

Re: Running pip from within FreeCAD

Post by gbroques »

Additionally loo did some work with FreeCAD and pip you may want to look at.

See:
https://github.com/looooo/freecad.pip
looo wrote:
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Running pip from within FreeCAD

Post by ickby »

I tried to figure this out for my add-on. In the end I just called the python executable, with the following extra logic:

On windows:
- set the working directory for the call to the freecad bin folder, as there is the python executable compatible with freecad

On Linux:
- it is possible that freecad uses a different python version than the system default, e.g. python2 Vs python3. Hence this needs to be figured out

Appimage:
- did not get it to work

Here the code:
https://github.com/OpenCollaborationPla ... ler.py#L48
nic
Posts: 135
Joined: Thu Apr 18, 2019 1:14 pm
Location: France

Re: Running pip from within FreeCAD

Post by nic »

chennes wrote: Tue Jan 04, 2022 10:44 pm I'm working on dependency resolution for the Addon Manager, and I'd like to be able to offer the user the ability to (attempt to) install missing Python dependencies. From a normal Python script
Something like :

Code: Select all

import pip; pip.main(["install", "pynastran"])
?
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Running pip from within FreeCAD

Post by onekk »

There will be problem with appimages, but as i usually import things to use in FreeCAD, maybe it will be possible to state a directory in userdir from which importing things.

This will involve to retrieve user directory, maybe a subdir with appropriate name and add to the Path of the internal interpreter.

This should be working, but the way to have pip to install missing packages in userdir\site_packages is unknown to me.

I'm away from PC so I could not investigate further.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
wmayer
Founder
Posts: 20242
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Running pip from within FreeCAD

Post by wmayer »

To me the very first question is whether it's a good idea to let FreeCAD install itself any 3rd party packages and to be honest I doubt it.

My main concerns:
  • Do we have full control over what will be installed or in other words: can it be misused to install any malware? AFAIR there were already such cases. And according to https://stackoverflow.com/questions/382 ... stall-them there is not even an independent quality control of the PyPI.
  • In many cases the OS (i.e. mainly a Linux distribution) provides a suitable package in its repository and this should be preferred over any PyPI stuff because the risk to break your system with PyPI packages is much higher.
    In the past we have seen a couple of postings where a user broke his system because he installed PySide2 via pip that may pull in a different Qt version that (partially) overwrites the system Qt version.
Like always it's a trade-off between convenience and security and is the little gain of convenience really worth all the trouble? IMO, if an add-on requires a 3rd party package then it should inform the user about it and let him decide how to continue.
User avatar
ebrahim raeyat
Posts: 619
Joined: Sun Sep 09, 2018 7:00 pm
Location: Iran
Contact:

Re: Running pip from within FreeCAD

Post by ebrahim raeyat »

ickby wrote: Wed Jan 05, 2022 6:13 am
Here the code:
https://github.com/OpenCollaborationPla ... ler.py#L48
such a great code, it is excellent.
User avatar
chennes
Veteran
Posts: 3877
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Running pip from within FreeCAD

Post by chennes »

wmayer wrote: Wed Jan 05, 2022 12:49 pm To me the very first question is whether it's a good idea to let FreeCAD install itself any 3rd party packages and to be honest I doubt it.
I am happy to defer to you on this point (obviously it's less work!), but I have some concerns about the benefits of your suggested approach. To discuss your concerns first:
  • Do we have full control over what will be installed?
No: asking pip to install a package implies installing all of that package's dependencies as well. But I think it's worth remembering a) while malware distribution via pip has happened it is a rare occurrence, and has not called for a moratorium on using pip, and b) that not only might one of those dependent packages be malware, but the FreeCAD package itself might be malware! Further, if we say to a user "we are not going to install this package for you, go do it yourself", we're basically still telling them to install malware. A user sophisticated enough to recognize the possibility of this as a malware distribution vector will always have the option to say "no, don't install the dependencies", and for an unsophisticated user there's little difference between our actually installing it, and our instructing them to do so.
  • In many cases the OS (i.e. mainly a Linux distribution) provides a suitable package in its repository
This is a serious concern, and probably undetectable by us, though if there is a way I am all ears! Maybe try to do an apt/yum/brew/whatever search before offering pip?
wmayer wrote: Wed Jan 05, 2022 12:49 pm Like always it's a trade-off between convenience and security and is the little gain of convenience really worth all the trouble? IMO, if an add-on requires a 3rd party package then it should inform the user about it and let him decide how to continue.
My concern is that saying to a user not familiar with python installation: "Install this dependency" is the same as saying "You can't use this Addon," because they don't know how to do it. I suspect this is particularly true on systems where we are shipping a monolithic package (e.g. Windows, Appimage), where the user will not even know which copy of python they should be installing with.

The paths forward as I see them are:
  1. Keep the old behavior: tell the user there are dependencies they should install, and abort installation of the Addon
  2. Attempt to explain to the user how to install the addon (e.g. where is the python instance they need), but still tell them to do it themselves.
  3. Try to figure out really how the user should do the addon installation (e.g. detect if their system uses apt, see if apt has a package, offer instructions for using apt to install it), but still make the user do it themselves.
  4. Same as above, but actually offer to run the commands for them.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
LarryWoestman
Posts: 98
Joined: Fri Oct 09, 2020 4:56 pm
Location: Oregon, USA

Re: Running pip from within FreeCAD

Post by LarryWoestman »

Something else to consider when using something like "pip" is that you don't have
any guarantees of repeatability either of which packages get installed or what
versions of packages get installed. This makes it very hard to test or debug
a problem because you can't be confident that you can reproduce the same
configuration twice.
Post Reply