2

I have python scripts that I use to do some geo-processing. I need to share it with QGIS users that are not familiar with python. I've written QGIS tools in the past using the python interface from QGIS, but the scripts I want to use now contain some modules (e.g., pandas) that I was unable to install on QGIS.

Is there a way to convert my python scripts "as is" to run in QGIS as a tool for non-python users? I can also convert my script to .exe if it makes it easier.

Edited: My inputs are vector file as geojson and my rasters are geo-tifs. I was able to install some modules in QGIS in the past but could not install others. I'm looking for a straight-forward solution (if it exists). Meaning, I have a python (standalone) script that works great on IDE and also works great as exe file (from the cmd), I want to take this script "as is" and convert it so that it would work on QGIS. Is there an easy and simple way to do that?

4
  • 2
    you can create a plugin: qgistutorials.com/en/docs/building_a_python_plugin.html
    – RutgerH
    Apr 9, 2018 at 8:00
  • What are the inputs and outputs of your python scripts? I'm guessing they take shapefiles or GeoTIFFs as arguments? You would have to export QGIS layers as those, run your standalone script, and then import the results. Give some examples.
    – Spacedman
    Apr 9, 2018 at 8:14
  • To install other modules you have to install pip via the osgeo shell, and then the modules... Otherwise you could add a .egg file with the module inside the plugin. Apr 9, 2018 at 15:25
  • If you need external packages in your Python scripts to use in QGIS tool, then you have two options - tell user to install this modules himself or download source code of the library and include it within the script (remember about the license). This way those libraries will be accessible from the script (and only from the script).
    – adamczi
    Apr 9, 2018 at 19:00

1 Answer 1

2

Let me see if I understand: The point is that “QGIS, but the scripts I want to use now contain some modules (e.g., pandas) that I was unable to install on QGIS.

If you are running on windows, don’t use the qgis.exe to start. You will always find a bat file to start QGIS and setup the environment. Depending if you use QGIS 2.18 or 3.0.1.

For 2.18 Look for the qgis-ltr.bat or qgis.bat in

C:\Program Files\QGIS 2.18\bin

If you edit the bat. You can add the

set PYTHONPATH = where pandas is ;%PYTHONPATH%

or

set PATH= where pandas is;%PATH%

Then use the .bat to start QGIS not the exe.

3
  • it does not work. I'm using QGIS 2.18 on win10, I did as you suggested, added set PATH= where pandas is;%PATH% to the “qgis.bat” file and started QGIS from that file. When I try to import pandas from the python console in QGIS I get an error No module named pandas. I don't get this error for numpy for example. Anyway, I'm not sure if this is the right solution in my case since I need to share the tool I wrote with other people that use different computers and are not familiar with python or bat files.
    – user88484
    Apr 11, 2018 at 5:58
  • Did you add set PYTHONPATH = where pandas is ;%PYTHONPATH% Apr 11, 2018 at 21:15
  • I'm not sure if I understand what you mean. Do you mean if I changed the "where pandas is" to the path where the pandas module is located on my computer? Anyway, I looked up in my system variables where my python is and the name of the variable was PATH. So I added it to the bat file. I do not have PYTHONPATH as a system variable.
    – user88484
    Apr 12, 2018 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.