[QGIS-Developer] External python package dependency in plugins

Richard Duivenvoorde rdmailings at duif.net
Sat Jul 7 05:23:03 PDT 2018


On 07/07/2018 01:26 PM, shiva reddy wrote:
> Thanks,
> In OS X following works:
> subprocess.call(['pip3','install','pyxform'])
> 
> Does same works  works for Ubuntu/linux also?
>  
> for windows, I am assuming 'python' refers to python 3 inside QGIS3.

Yep:

import subprocess
subprocess.call(['pip3','install','pyxform','--user'])

works too.

But as said, not sure if all distro's call the python3 pip: pip3 (and/or
python3). I still would be afraid to force a specific version of a
module, as I would be afraid to override some excisting one.
I do like your solution though, the libs end up in my user site-packages
dir:
/home/richard/.local/lib/python3.5/site-packages


Ah: http://zetcode.com/lang/python/introspection/
>>> import sys
>>> sys.executable
'/usr/bin/python3'

gives you your actual python exe...

I wonder if you can create an venv from within QGIS in the subprocess way.
Mmm, this works (from within QGIS):

>>> import sys
>>> py_exe = sys.executable
>>> venv_path =
'/home/richard/.local/share/QGIS/QGIS3/profiles/default/venv'
>>> subprocess.call([py_exe,'-m','venv', venv_path])

Had to install python3-venv though:
sudo apt-get install python3-venv

sourcing it does not work with me:

>>> subprocess.call(['source', venv_path+'/bin/activate'])
FileNotFoundError: [Errno 2] No such file or directory: 'source'

Now somebody with more knowledge of the different python-path/lib-dir's
should come in, to tell us if this is usable after activating that venv...

Not sure if this would work with the already running python interpreter
in QGIS...

Idea's?

Regards,

Richard Duivenvoorde


More information about the QGIS-Developer mailing list