简体   繁体   English

无法在 Python 3.9.2 中导入模块 qgis

[英]Can't import module qgis in Python 3.9.2

I'm working on Python 3.9.2 and got this我正在研究 Python 3.9.2 并得到了这个

error:No module named qgis错误:没有名为 qgis 的模块

I'm trying to connect to a database and query within it.我正在尝试连接到数据库并在其中进行查询。 I've seen the code beLow working under python 2. Is there any difference with Python 3?我已经看到下面的代码在 python 2 下工作。与 Python 3 有什么区别吗?

[...]
from qgis.core import *
import qgis.utils


uri.setConnection('{0}'.format(hote_IP), "5432", '{0}'.format(xxx), '{0}'.format(xxx),'{0}'.format(xxx))    
db = QSqlDatabase.addDatabase("QPSQL")    
db.setHostName(uri.host())    
db.setDatabaseName(uri.database())    
db.setPort(int(uri.port()))    
db.setUserName(uri.username())    
db.setPassword(uri.password())

sql = '''select field1, field2 from schema.table'''    
query = db.exec_(sql)
print(str(query.value(0))

Try to install it again via python 3:尝试通过 python 3 再次安装:

python3 -m pip install qgis

I have had the same problem when creating virtual environments for python projects and then trying to import QGIS.在为 python 项目创建虚拟环境然后尝试导入 QGIS 时,我遇到了同样的问题。 It is quite complicated to install QGIS in a virtual environment.在虚拟环境中安装 QGIS 相当复杂。 What I do to solve this is to create venvs that have access to the system level site packages where QGIS lives after install.我要解决这个问题是创建可以访问安装后 QGIS 所在的系统级站点包的 venv。 I use the following:我使用以下内容:

python3 -m venv myvenv --system-site-packages

The prerequisite for this to work, however, is that you have properly install QGIS and can access that from your system level python interpreter.然而,这个工作的先决条件是你已经正确安装了 QGIS,并且可以从你的系统级 python 解释器访问它。 I use Ubuntu and the instructions properly installing QGIS on Ubuntu are here .我使用 Ubuntu 和在 Ubuntu 上正确安装 QGIS 的说明在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM