2

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'
>>>

2 Answers 2

1

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.

1
  • You're right, I have two Python installations on my system. But what makes no sense to me is that I get ModuleNotFoundError when I try to import qgis.core in the OSGeo4W shell. And when I try to install the colour module in the OSGeo4W shell, I get the following message: Requirement already satisfied. It's having two Python installations that's causing the issue, but I'm having difficulty troubleshooting it.
    – Josh.s
    Commented May 14, 2020 at 21:12
0

I had 2 installations of QGIS. One was through OSGeo4W, while the other one was installed independently.

I too faced the same qgis import error issue. This is what I did.

Just TWO changes:

  1. PYTHONPATH = C:\OSGeo4W\apps\qgis\python; C:\OSGeo4W\apps\qgis\python\plugins; C:\OSGeo4W\apps\Python39\Lib\site-packages; C:\OSGeo4W\apps\qgis\python\qgis;

  2. Path(in system variables): C:\OSGeo4W\apps\qgis\python C:\OSGeo4W\apps\qgis\python\qgis (Add this path and "move down" any other existing path to QGIS)

Also, make sure that the paths that you require are moved to the top of Path variable list.

Your Answer

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

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