(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.