[QGIS-Developer] External python package dependency in plugins

Axel Andersson axel.n.c.andersson at gmail.com
Sat Jul 7 22:42:31 PDT 2018


Hi,

I added this to my code just 3 days ago :)

import sys

try:
    import matplotlib
except:
    import subprocess
    print('installing matplotlib')
    subprocess.call([sys.exec_prefix + '/python', "-m", 'pip',
'install', 'matplotlib'])
    import matplotlib
    print('installation completed')


I dont know if it is a better or worse solution, I've only tested it on a
Windows machine with the standalone version and the osgeo4w installation.

I tried calling sys.executable first, instead of sys.exec_prefix +
'/python', which resulted in a new qgis window started :)

Axel

On 7 July 2018 at 14:23, Richard Duivenvoorde <rdmailings at duif.net> wrote:

> 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
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20180708/f64b4eba/attachment.html>


More information about the QGIS-Developer mailing list