Sunday 29 September 2019

python 3 - OSGeo4W shell with python3


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?



Answer



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%

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...