4

(Also asked in stack overflow - https://stackoverflow.com/questions/43244062/import-pandas-or-any-python-module-into-qgis-internal-python )

I am developing a plugin for the current LTR version of QGIS (v2.14) using the Python installation included in it (Python v2.7).

This Python installation includes Numpy, but not Pandas. I am working with large datasets and performing large, slow operations on them, and Pandas would do some the work way more efficiently (faster) than using only Numpy - as I can judge when I run my scripts in a separate installation of Python 3 on my machine (on Windows 7). Unfortunately Pandas is not included in the Python 2.7 installation of QGIS.

Installing Pandas manually is an option if the plugin was to be used by 1-5 users, but I plan to distribute to a large group of users (using Win7 and Win10) so using Pandas would require an automatic way to include Pandas in the plugin somehow, or automatically install it for the internal Python QGIS installation.

Is there any automatic way (a Python script) that I could call at the beginning of my script to:

1) just call Pandas from a folder and import it without errors or

2a) check if Pandas is installed in QGIS-Python

2b) if not, install it in the internal QGIS Python version (without messing too much with environment variables for the PCs, as some of them have alternate versions of Python in use).

So something like

# My Python plugin script starts here

import numpy as np
import math
import ...

functionPandasPresent(): 
    #function to check if Pandas is present here
    #result = True or False, so for example
    result = False
    return result

pandas_present = functionPandasPresent()

if pandas_present is False:
    # function to install Pandas here
    # looking for whatever dir QGIS-Python was installed in
    # installing it just for that Python installation

import pandas as pd

# The rest of my script continues here...

With a script like this, not only Pandas but any other additional package could be used in custom QGIS plugins - so useful for any plugin developer I guess.

  • Please don't cross-post – PolyGeo Apr 6 '17 at 8:04
  • Thanks for your comment and for keeping the site 'clean'. This question can be answered by: a) an expert in PyQGIS (there aren't too many I guess) or b) someone who understands well how to handle multiple versions of Python in one machine. I think they visit different sites, and given that an answer is not easy to find (proof- zero answers on first day) I decided to post on both sites and make people aware of both posts. As soon as one of the posts gets an answer or at least some discussion I will delete the other question to help keeping the site clean. Hope this makes you happy. J – jberrio Apr 6 '17 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.