[GIS] ModuleNotFoundError: No module named ‘colour’ in QGIS Python Console

importerrorosgeo4wpythonqgisqgis-python-console

I'm trying to run a Python script in QGIS which imports the colour module. However, when I run it via the Python console in QGIS, I receive the ModuleNotFoundError:

Python Console
Use iface to access QGIS API interface or Type help(iface) for more info
Security warning: typing commands from an untrusted source can lead to data loss and/or leak
import colour
Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS 3.10\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "C:/PROGRA~1/QGIS 3.10/apps/qgis-ltr/./python\qgis\utils.py", line 744, in _import
    mod = _builtin_import(name, globals, locals, fromlist, level)
ModuleNotFoundError: No module named 'colour'
from qgis.core import *

This seems to be a fairly common issue and probably relates to the environment variables. I followed the instructions here for setting my PATH and PYTHONPATH variables but still haven't had much success. Oddly enough, I can import colour but not qgis.core in OSGeo4W. Any advice on how to import the colour module in QGIS successfully?

C:\WINDOWS\system32>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import colour
>>> from qgis.core import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'qgis'
>>>

Best Answer

Probably you have two Pythons on your machine, one was installed with QGIS, and you install another independent of your QGIS. If you start your Python from the OSGeo4W shell the enviromnet will be set to QGIS (e.g. qgis.core is on the Python path). If you would like to use colour modul in QGIS

  • you have to install it through the OSGeo4W shell

or

  • you can try to extend the PYTHONPATH in both Python environment to have the same modules available (it may raise a version issue)

You can check your environment in both Python instance:

import os, sys
sys.path
os.environ["PYTHONPATH"]

You may get an error message after the os.environ command if no PYTHONPATH environment variable was set. The Python modules are seached in the folders listed in sys.path and PYTHONPATH.

Related Question