calling QgsDataSourceUri with QGIS for python plugin - python

TLDR : using QGIS, I'm trying to develop a python plugin to update a database. Unfortunately I get immediatly an error : Traceback (most recent call last):
File "", line 1, in
NameError: name 'QgsDataSourceUri' is not defined
More detailed:
I work on QGIS2.18 to develop a plugin to update some data located on a postgres database.
for this, I want to use this kind of code:
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtCore import QSettings
from PyQt4.QtCore import QSettings
from qgis.core import QgsVectorLayer, QgsDataSourceURI
uri = QgsDataSourceUri()
# set host name, port, database name, username and password
uri.setConnection(hote_IP, "5432", base_de_donnee, utilisateur, mot_de_passe)
# set database schema, table name, geometry column and optionally
# subset (WHERE clause)
#uri.setDataSource("public", "roads", "the_geom", "cityid = 2643")
uri.setDataSource("", sql, "geom", "", "gid")
vlayer = QgsVectorLayer(uri.uri(), zapm, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
(I got the code from the net, I'll adapt it later on)
My problem : when I try to run this code on the Python console of QGIS, I immediatly get the error
Traceback (most recent call last):
File "", line 1, in
NameError: name 'QgsDataSourceUri' is not defined
even when I only run the import and the line uri = QgsDataSourceUri(), I get the same error message.
I have not been able to find out how to correct this issue.
problem of installation of QGIS? of python? bad imports?
Config:
qgis 2.18.20
python 3.6.5
If anyone has an idea on how to solve this, I would be really glad.
Thanks,
Erwann

You simply are using the wrong class name. It should be uri = QgsDataSourceURI() instead of uri = QgsDataSourceUri() because you've imported QgsDataSourceURI and not QgsDataSourceUri
QGIS and QT Python classes are case-sensitive. You can confirm the exact syntax looking at QGIS 2.18 related API.

Related

I can not figure out why I can not start this simlple python script

My directory looks like this
When I start directly with PyCharm it works.
But when I try to start the script with a commandline I get this error messsage
> python .\PossibilitiesPlotter.py
Traceback (most recent call last):
File "C:\Users\username\PycharmProjects\SwapMatrixPlotter\possibilitiesplotter\PossibilitiesPlotter.py", line 7, in <module>
from plotterresources.PlotterProps import PlotterProps
ModuleNotFoundError: No module named 'plotterresources'
This is how the import looks from my main class PossibilitesPlotter.py
import sys
sys.path.append("plotterresources/PlotterProps.py")
from csv import reader
from pathlib import Path
from plotterresources.PlotterProps import PlotterProps
from possibilitiesplotter.PossibilitiesGraph import PossibilitiesGraph
from possibilitiesplotter.PossibilitiesModel import PossibilitiesModel
class PossibilitiesPlotter:
As a workaround, add the following line to PossibilitesPlotter.py:
sys.path.append("../plotterresources/PlotterProps.py")
This will add the directory one level above the commandline pwd to the PATH variable. So this is always relative to the location of the calling script/shell.
Thus in general:
NEVER append to the PATH/PYTHONPATH variable from within modules. Instead restructure your module. For more details, take a look at the documentation on Packaging Python Projects

ValueError: source code string cannot contain null bytes when importing a package in python

from pyModbusTCP.client import ModbusClient
I am using pycharm and I have a class file which uses methods from pyModbusTCP package to communicate with devices over modbus. This class files exists as part of a directory in my project, where under the project folder I have a folder for device1 containing that particular class file along with some others. The idea was to simply duplicate the folder and title it device2, the intent being device2 functions as a backup to device1.
I created a device2 folder, made a main.py and copied most of the other contents from device1. The console is giving me only the error from the title at line 1, meaning the very first import statement that I linked is the cause for me receiving the error from the title. Given all that I have no idea what the error is even referring to, could anyone help me troubleshoot? Thanks!
EDIT:
Traceback (most recent call last):
File "/home/env1/Simulation/main.py", line 11, in <module>
import read_from_modbus
File "/home/env1/Simulation/read_from_modbus.py", line 1, in <module>
from pyModbusTCP.client import ModbusClient
ValueError: source code string cannot contain null bytes

ModuleNotFoundError: No module named '_beatbox'

I am trying to use python to connect with SF.
Saw some articles that show how to use it with beatbox library and I did install it.
However when trying to run simple code I'm getting error below.
Traceback (most recent call last):
File "c:/Users/user/hello/.vscode/hello.py", line 16, in <module>
import beatbox
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\beatbox\__init__.py", line 1, in <module>
from _beatbox import _tPartnerNS, _tSObjectNS, _tSoapNS, SoapFaultError, SessionTimeoutError
ModuleNotFoundError: No module named '_beatbox'
I navigate to the folder where the beatbox is installed and I see there the file _beatbox.py.
I think the file __init__.py try to import _beatbox but cannot find it for some reason.
Any idea how to solve it? What I'm missing?
Code:
import beatbox
sf_username = "xxxxxx"
sf_password = "xxxxxx"
sf_token = "xxxxxx"
def getAccount():
sf = beatbox._tPartnerNS
sf_client = beatbox.PythonClient()
password = str("%s%s" % (sf_password, sf_token))
sf_client.login(sf_username, sf_password)
accQuery = "Select Id,Name From Account limit 5"
queryResult = sf_client.query(accQuery)
print ("query result: " + str(queryResult[sf.size]))
for rec in queryResult[sf.records:]:
print str(rec[2]) + " : " + str(rec[3])
return
Can close the case. I first found that in python 3+ should use beatbox3. But then found additional errors (possible compatibility issues).
Since I notice it taking me too long, instead I tried using library simple-salesforce 0.74.2 to connect, and it worked perfect.
Probably, Python does not know where to search for the module. By default, only the sitepackages directory and your working directory is searched. You can resove this by placing a symbolic link to the beatbox package or moving it to the sitepackages directory
If you change the sitepackage folder name from "beatbox" to "_beatbox" this will solve your problem. You can then import it as: "import beatbox" and it will load in Python.

Import .json to Database

I successfully downloaded tweets into a json file. Now I try to import it in a database with this function:
def import_json(fi):
logging.warning("Loading tweets from json file {0}".format(fi))
for line in open(fi, "rb"):
data = json.loads(line.decode('utf-8'))
database.create_tweet_from_dict(data)
the json-file "keywords_BVBS04.json" lays in a folder called data which is in the current directory. The function is in a file called BVBS04.py
to start the import I type BVBS04.import_json(keywords_BVBS04.json) in ipython in the console. this is what I get back:
NameError Traceback (most recent call
last) in ()
----> 1 BVBS04.import_json(keywords_BVBS04.json)
NameError: name 'keywords_BVBS04' is not defined
Now here comes the is a beginner's question: Where/how do I have to define "keywords_BVBS04"? I tried a lot :(
Thanks!
This is what you want,
1) you need to import the function from the script, not use dot-notation on the script.
2) Quote the filename.
>>> from BVBS04 import import_json
>>> import_json("keywords_BVBS04.json")
Good luck with the rest of things

Import Error using cPickle in Python

I am using Pickle in Python2.7. I am getting error while using cPickle.load() method. The code and error is shown below. Can someone guide me through this?
Code:
#! usr/bin/python
import cPickle
fo = open('result','rb')
dict1 = cPickle.load(fo)
Error:
Traceback (most recent call last):
File "C:\Python27\test.py", line 7, in <module>
dicts = cPickle.load(fo)
ImportError: No module named options
It seems like you can not do
import options
but when you or someone else did
cpickle.dump(xxx, open('result', 'rb'))
there was an object with a class or function of a module options that existed at this point in time, in xxx.
Solution
You can open the file binarily and replace options with the module you replaced the old module options with.
You probably created the file in your package like in module package.main by executing the file main.py or something like it, having a module options in the same directory.
Now you do import package.main, try to read the file and options is now called package.options and the module options can not be found.
How did you create this file? How do you load it now? cPickle/pickle does not transfer source code - so if you use a function you need the module when you load it.

Categories

Resources