[QGIS-Developer] External python package dependency in plugins

shiva reddy kotishiva at gmail.com
Mon Jul 9 03:38:46 PDT 2018


Hi,
Finally I found following solution, which works in both windows and mac:


import subprocess
try:
        subprocess.call(['python3', '-m', 'pip', 'install','pyxform'])
        from pyxform.builder import create_survey_element_from_dict
        print('package is installed after python3')
    except:
        subprocess.call(['python3', '-m', 'pip',
'install','pyxform','--user'])
        print ("after python3 --user call")
        try:
            from pyxform.builder import create_survey_element_from_dict
        except:
            print('not able to install pyxform, install mannually')

   - First 'subprocess.call' works in Mac without permission issue.
   - Second 'subprocess.call' works in windows without permission issue.

Can anybody tries this code in ubuntu/linux and verify.


Thanks


If somebody can test this in other platform, I would be very glad.
Thanks & Regards
Shiva Reddy K.
Scientist/Engineer 'SD'
Indian Institute of Remote Sensing,
Indian Space Research Organisation
Department of Space
4-Kalidas Road
Dehradun
mobile: 8791806093


On Sun, Jul 8, 2018 at 4:20 PM Luke Pinner <lukepinnerau at gmail.com> wrote:

> My mistake. Importing pip is not supported and this functionality has been
> removed in pip>=10
>
> https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program
>
>
>
> On Sun., 8 Jul. 2018, 16:28 Luke Pinner, <lukepinnerau at gmail.com> wrote:
>
>> Rather than using a subprocess and needing to figure out which pip/python
>> etc., have you tried importing and using the pip module directly? Something
>> like:
>>
>> import sys
>>
>> try:
>>     import matplotlib
>> except:
>>     import pip    pip.main(['install', 'matplotlib'])    import matplotlib
>>
>> Luke
>>
>> On Sun., 8 Jul. 2018, 15:42 Axel Andersson, <axel.n.c.andersson at gmail.com>
>> wrote:
>>
>>> 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
>>>>
>>>
>>> _______________________________________________
>>> 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/20180709/b83cb83c/attachment-0001.html>


More information about the QGIS-Developer mailing list