Geospatial python tutorial - Install Jupyter Notebook in QGIS3

July 28, 2019 | category 👉 python, GIS, QGIS, pyqgis

Share on:

Install Jupyter Notebook in QGIS3

Setup

Batch file to run pip install for Python3

Can we use Jupyter Notebook for PyQGIS? Yes..we can. This is how i do it, lets start with creating a batch file, *.bat. Copy and paste a script below and make sure you save it somewhere, I recommend you guys to save it into C:\OSGeo4W64\bin because we could use OSGeo4W Shell to run the script, and it is reproducible.

Lets name it as run-installer.bat. You can use other name, as long as you can remember them.

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"

@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
set GDAL_DRIVER_PATH=%OSGEO4W_ROOT%\bin\gdalplugins
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%

python3 -m pip install -r "path\to\your\requirements-dev.txt"

But why *.bat?

It is tricky to install python packages in QGIS since there are two version of Python which is 2 and 3. We need to run Python3 and also need to setting up the environment, so by writing a batch file, we can save our time and it is reproducible.

Now, as you can see at the final line of our script python3 -m pip install -r "path\to\your\requirements-dev.txt" … this is where we need to create a list of the package that we need. Lets create a requirement-dev.txt and save it in the C:\OSGeo4W64\bin.

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"

@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
set GDAL_DRIVER_PATH=%OSGEO4W_ROOT%\bin\gdalplugins
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%

python3 -m pip install -r requirement-dev.txt

So, our objective is to install jupyter, copy and paste the below text into your requirement-dev.txt file and save it.

jupyter
ipykernel
jupyter_client
nbconvert

Our file structure will be something like this,

C:\OSGeo4W64\bin
    |
    -- run-installer.bat
    |
    -- requirement-dev.txt

Run batch from OSGeo4W Shell

Go to Windows ‘start’ menu, and search for ‘ OSGeo4W ‘.

In OSGeo4W cmd, type run-installer.bat

run-installer.bat

there you go…you should be fine, if there an error please let me know. Now it will be so easy for us to install Python packages inside QGIS3 environment. Just edit requirement-dev.txt … then run run-installer.bat.

Lets say we need scikit-learn, delete everyting in requirement-dev.txt, and write scikit-learn then run our batch file.

⤎ back to posts