10

I would like to use OSGeo4W shell with Python3 but when typing python3 I get the following error:

Fatal Python error: Py_Initialize: unable to load the file system codec
  File "C:\OSGEO4~1\apps\Python27\lib\encodings\__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax

How can I use Python3?

16

There's no need to edit batch files (and break python 2.7 in the process) or set environment variables manually.

Just run py3_env which sets the shell up for Python 3 automatically, then run python3.

From a OSGeo4W shell with just python 2 & 3 installed:

C:\> o-help
                   -={ OSGeo4W Shell Commands }=-

       dllupdate           pip3                sqlite3
       easy_install3       python              textreplace
       nircmd              python3             vcredist_2015_x64
       nircmdc             pythonw             xxmklink
       osgeo4w-setup       pythonw3


       make-bat-for-py   py3_env  <<<=== This is the command you want
       o-help            setup-test
       o4w_env           setup



C:\>python3
Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

Current thread 0x000013e8 (most recent call first):
^C

C:\>py3_env
C:\>python3

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
8

There is a not well documented command build into OSGeo4W Shell which sets the shell up to python3 as Luke mentions

py3_env

Basically it sets your PYTHONHOME and the correct PATH. Then you can call Python3 with python3.

C:\>py3_env
C:\>SET PYTHONPATH=
C:\>SET PYTHONHOME=C:\OSGEO4~1\apps\Python36
C:\>PATH C:\OSGEO4~1\apps\Python36;C:\OSGEO4~1\apps\Python36\Scripts;C:\OSGEO4~1\apps\Python27\Scripts;C:\OSGEO4~1\bin;C:\Windows\system32;C:\Windows;C:\Windows\WBem
C:\>python3
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Update with auslander's comment:

To use Python3 with the OSGeo4W shell that comes with QGIS 2 you have to change the file {path you installed qgis}\etc\ini\python-core.bat:

SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python36
PATH %OSGEO4W_ROOT%\apps\Python36\Scripts;%PATH%
  • If you want to do this on Windows with the OSGeo4W shell that comes with QGIS 3, you will have to go into {path you installed qgis}\etc\ini and modify python-core.bat to reference Python36 instead of Python27 in the batch script that sets PYTHONHOME and PATH. Then call python3 from the CLI. However note this will break links to python 2.7 and give you a syntax error if you try to call just python. – auslander Mar 15 '18 at 14:23
  • @Auslander breaking python 2.7 is not the way to do this. See my answer, just run py3_env which sets the shell up for Python 3 automatically. – user2856 Apr 1 '18 at 4:03
  • @Luke clever! I stand corrected. – auslander Apr 5 '18 at 14:55
  • @Luke I've updated my answer. I overlooked the command in the shell and google did not give me any answer. Still, the OSGeo4W shell that comes with QGIS 2 does not have the py3_env – bennos Jun 14 '18 at 9:34
  • Before qgis 3 (which requires python 3) was released there was only a need for a single version of python as qgis 2 requires python 2. OSGeo4W couldn't really drop qgis 2 so they had to find some way of supporting both qgis 2 + python 2 and qgis 3 + python 3. – user2856 Jun 14 '18 at 9:46
1

If you want to make the change permanent you can modify the batch file OSGeo4W.bat located in your QGIS (or OSGEO) directory and just add:

call "py3_env"

below the line calling

call "%~dp0\bin\o4w_env.bat"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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