PyQgis3.4+PyCharm环境配置

1题记(第一篇博客)

最近需要用到qgis实现缓冲区的一个功能,需要来配环境。为此看了好多博客,发现好多博客内容都是老版本的qgis,新版本的qgis和之前都不一样。而且博客内容写的并不是非常详细,作为一个完全新手完全不懂,看了那么博客,我都没有看明白到底首先装什么软件。最后经历了四天的时间,尝试了各种方法卸了装,装了卸。最后翻墙用谷歌搜到了两篇对我帮助很大的文档。但是也并不是傻瓜式安装,其中也出现了很多文档上没有出现的问题,最后摸索着终于把环境配好,示例代码运行成功,特此分享给大家。

2安装包下载

2.1.下载osgeo4w

32位:http://download.osgeo.org/osgeo4w/osgeo4w-setup-x86.exe
64位: http://download.osgeo.org/osgeo4w/osgeo4w-setup-x86_64.exe

2.2下载安装包

安装过程中选Advanced Install就可以下载资源包了,安装路径建议保持默认路径。当然也可以修改,只是修改后会牵涉到相应的路径变量的修改,保持默认会比较简单些。
一路next到下一步的界面时,在Search栏目里输入“qgis”,可以查询到QGis的相关资源。其中”ltr”的版本表示long time release, 是比较稳定的发布版本。点击相应的库,使之由符号”n/a”变成一个打上叉的方框形状就表示选中相应的文件了,之后将会下载这个文件。
我下载的是qgis-ltr
在这里插入图片描述

2.3下载完成后的说明

目前先下载这一个包,如过后续出现什么问题那么继续再来这个页面下载,缺什么下载什么。

3配置环境

3.1建立.cmd文件

建立一个.cmd文件,然后添加一些路径和环境变量(我也不知道为什么这么操作,只是看文档做,所以按自己理解来做一些改变。)然后把下边的复制粘贴进去,双击该.cmd文件,可以任意命名,可以放在任何位置都行,我命名为pyqgis.cmd。(可能我添加的path有点多,主要后边试环境,一直出错,所以加了许多Path,有可能是用不到的,但是环境已经配过来了,还是不要浪着改了。)

  @echo off
  SET OSGEO4W_ROOT=C:\OSGeo4W64
  call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
  call "%OSGEO4W_ROOT%"\apps\grass\grass76\etc\env.bat
  @echo off
  path %PATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\bin
  path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass76\lib
  path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
  path %PATH%;C:\OSGeo4W64\apps\Python37\Scripts
  path %PATH%;C:\OSGeo4W64\apps\Python27\Scripts
  path %PATH%;C:\OSGeo4W64\bin
  path %PATH%;C:\OSGeo4W64\apps\Python37\DLLs
  path %PATH%;C:\OSGeo4W64\apps\Python37\lib
  path %PATH%;C:\OSGeo4W64\apps\Python37\lib\site-packages
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\bin
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\python
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\python\qgis
  path %PATH%;C:\OSGeo4W64\apps\Qt5
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\qtplugins
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\python\PyQt5

  set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\python
  set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
  
  set PATH=C:\Program Files;%PATH%
   cmd.exe

3.2双击pyqgis.cmd文件

在弹出的cmd命令框中,输入python3,然后分别输入
>>> import qgis.core
>>> import PyQt5.QtCore
如果没有异常,那此步骤就可以结束。
但是怎么可能没有问题,如果真这么按教程来就好了。
下边仅就我遇到的问题进行说明。

3.2.1问题1

具体是什么报错忘记了,也不能现在回去再让它再来报错一遍了,大概就是我运行pyqgis.cmd文件时出现找不到grass文件的意思,因为在pyqgis.cmd我配置的有call “%OSGEO4W_ROOT%”\apps\grass\grass76\etc\env.bat,它找不到该路径。我回到安装目录检查,确实没有该目录。那么就此回到osgeo4w安装界面进行安装grass,搜索grass,下载如下(keep的就是我下载过的):
在这里插入图片描述
然后继续双击pyqgis.cmd文件,在弹出的cmd命令框中,输入python3,然后分别输入
>>> import qgis.core
>>> import PyQt5.QtCore
如果没有异常,那此步骤就可以结束。
(此处我没有错误,如果各位有报错的话,报出缺什么东西,就回到osgeo4w安装界面进行安装相应部分就行)

3.3安装pb_tool

虽然我不知道这个东西给的什么,但是老外的配置文档上写了让安装那么就安装吧。
双击pyqgis.cmd文件,在弹出的cmd命令框中,输入python3 -m pip install pb_tool
,如果输出如下:

C:\Users\gsherman>pb_tool
    Usage: pb_tool [OPTIONS] COMMAND [ARGS]...

      Simple Python tool to compile and deploy a QGIS plugin. For help on a
      command use --help after the command: pb_tool deploy --help.

      pb_tool requires a configuration file (default: pb_tool.cfg) that declares
      the files and resources used in your plugin. Plugin Builder 2.6.0 creates
      a config file when you generate a new plugin template.

      See http://g-sherman.github.io/plugin_build_tool for for an example config
      file. You can also use the create command to generate a best-guess config
      file for an existing project, then tweak as needed.

      Bugs and enhancement requests, see:
      https://github.com/g-sherman/plugin_build_tool

    Options:
      --help  Show this message and exit.

    Commands:
      clean       Remove compiled resource and ui files
      clean_docs  Remove the built HTML help files from the...
      compile     Compile the resource and ui files
      config      Create a config file based on source files in...
      create      Create a new plugin in the current directory...
      dclean      Remove the deployed plugin from the...
      deploy      Deploy the plugin to QGIS plugin directory...
      doc         Build HTML version of the help files using...
      help        Open the pb_tools web page in your default...
      list        List the contents of the configuration file
      translate   Build translations using lrelease.
      update      Check for update to pb_tool
      validate    Check the pb_tool.cfg file for mandatory...
      version     Return the version of pb_tool and exit
      zip         Package the plugin into a zip file suitable...

那么就是正常的,可以继续进行,以上内容时复制老外文档的,我的配置过去,不想搞了,大概就是这个样子如果安装成功,不报什么错误,就可以继续进行下去。
但是我就比较惨了,老外成功,我继续报错(错误及解决方案如下)。

3.3.1错误一

错误信息:在C:\OSGeo4W64\apps\Python37\Scripts找不到pip
大概就是这个意思(配置过去了,不太好截图之前的错误),那么我就去到该文件夹下,查看有pip没有,确实没有。那么回到osgeo4w安装界面进行安装python3-pip
在这里插入图片描述

3.3.2错误二

安装完python3-pip后继续执行该步骤,报错信息如下:
You are using pip version 19.0.3, however version 19.1.1 is available. You should consider upgrading via the ‘python -m pip install --upgrade pip’ command.
该错误信息意思为所使用的pip版本太低,我们有的时19.0.3版本,但是需要的是19.1.1版本的。所以我们就需要对pip进行升级:在报错的cmd命令下输入
python3 -m pip install --upgrade pip
(记得一定是python3)
然后继续安装pb_tool,至此我没有了报错信息。此步骤结束。

3.4配置paycharm相关环境

逛论坛时,看好多老外都说pycharm2019.1会出问题,我之前也用这个配不好,虽然不一定是pycharm2019.1的锅,但是以免万一还是不要用这一版本。
我用了pycharm2018.3.4版本的。然后对我们之前生成的pyqgis.cmd进行里边内容的改写,当然你也可以新建立一个.cmd文件,具体内容如下:

 @echo off
  SET OSGEO4W_ROOT=C:\OSGeo4W64
  call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
  call "%OSGEO4W_ROOT%"\apps\grass\grass76\etc\env.bat
  @echo off
  path %PATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\bin
  path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass76\lib
  path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
  path %PATH%;C:\OSGeo4W64\apps\Python37\Scripts
  path %PATH%;C:\OSGeo4W64\apps\Python27\Scripts
  path %PATH%;C:\OSGeo4W64\bin
  path %PATH%;C:\OSGeo4W64\apps\Python37\DLLs
  path %PATH%;C:\OSGeo4W64\apps\Python37\lib
  path %PATH%;C:\OSGeo4W64\apps\Python37\lib\site-packages
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\bin
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\python
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\python\qgis
  path %PATH%;C:\OSGeo4W64\apps\Qt5
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\qtplugins
  path %PATH%;C:\OSGeo4W64\apps\qgis-ltr\python\PyQt5

  set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis-ltr\python
  set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37


  set PATH=C:\Program Files;%PATH%

  start "PyCharm aware of Quantum GIS" /B "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.4\bin\pycharm64.exe"

然后双击这个.cmd文件,便可以通过这个打开pycharm,然后对pycharm的python解释器进行配置,选择如下路径的python即可:
在这里插入图片描述

4代码测试

运行如下代码:
os.environ[‘QT_QPA_PLATFORM_PLUGIN_PATH’]=
r’C:\OSGeo4W64\apps\Qt5\plugins’
os.environ[‘PATH’] += r’;C:\OSGeo4W64\apps\qgis-ltr\bin;C:\OSGeo4W64\apps\Qt5\bin’
这两行代码一定要加,不然会报出找不到qt插件的错误

from qgis.core import *
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'C:\OSGeo4W64\apps\Qt5\plugins'
os.environ['PATH'] += r';C:\OSGeo4W64\apps\qgis-ltr\bin;C:\OSGeo4W64\apps\Qt5\bin'
# supply path to qgis install location
QgsApplication.setPrefixPath("C:\OSGeo4W64", True)

# create a reference to the QgsApplication, setting the second argument to False disables the GUI
qgs = QgsApplication([], False)

# load providers
qgs.initQgis()
# Write your code here to load some layers, use processing , algorithms, etc.

my_project = QgsProject()
my_project.write(r"D:\my_pro.qgs")

my_pro = QgsProject()
my_pro.read(r"D:\my_pro.qgs")
print(my_pro.fileName())


# When your script is complete, call exitQgis() to remove the
# provider and layer registries from memory
qgs.exitQgis()

该代码是创建一个qgis工程,然后读取该工程,输出该工程的名字。运行结果如下:
在这里插入图片描述
在d盘该工程也创建成功。
在这里插入图片描述

5总结

为什么配置一个pyqgis的环境花费了这么久?可能确实国内qgis用的不是很多,pyqgis少之更少。官方给的文档配置环境仅两句话,根本没有什么用。百度出来的博客写的内容不是很详细,而且都是老版本的qgis。新版本的qgis变化还是挺大的,作为一个新手却只能照着博客写的教程,来一步步操作,一报错就不知道怎么做了,就开始怀疑自己从开始装的软件对不对,卸了装,装了卸,尝试各种版本,时间都花费到这里了。
最后还是心态放正,不急,耐心在谷歌看一些英文的论坛,找到了几篇优质文档,按照文档一步一步操作,但还是报了各种错误,最后按照自己的理解给解决了。
总结就是,尽信教程不如半信教程,每个人的环境都不一样,别人不会报错,但自己就会报错,要学会去看错误信息,根据错误信息去解决问题,不要教程上没有自己的报错信息就无法解决。

6参考文档

1.Quick Guide to Getting Started with PyQGIS 3 on Windows
http://spatialgalaxy.net/2018/02/13/quick-guide-to-getting-started-with-pyqgis3-on-windows/
2.Error from create a reference to the QgsApplication
https://gis.stackexchange.com/questions/302774/error-from-create-a-reference-to-the-qgsapplication

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值