I have created a plugin in QGIS windows version using
"Plugin Builder". which on a button click opens another window, that is calling another class in the dialgoue python file, with another .ui file. That plugin is working fine on windows but when i installed that plugin on linux QGIS i got below error
Couldn't load plugin tplUserStatsReport due to an error when calling its initGui() method
TypeError: Expected bytes or unicode string, got newstr instead
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/qgis/utils.py", line 342, in startPlugin
plugins[packageName].initGui()
File "/home/hasan/.qgis2/python/plugins/tplUserStatsReport/tplUserStatsReport.py", line 203, in initGui
self.setInitialStats();
File "/home/hasan/.qgis2/python/plugins/tplUserStatsReport/tplUserStatsReport.py", line 330, in setInitialStats
connect= psycopg2.connect("dbname='"+str(Configuration.dbname)+"' user='"+str(Configuration.username)+"' host='"+str(Configuration.host_ip)+"' port="+str(Configuration.port)+" password='"+str(Configuration.password)+"'")
File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 129, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/psycopg2/extensions.py", line 155, in make_dsn
parse_dsn(dsn)
TypeError: Expected bytes or unicode string, got newstr instead
Python version: 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
QGIS version: 2.18.2 Las Palmas, 102ee87
Python Path:
/usr/share/qgis/python/plugins/processing
/usr/share/qgis/python
/home/hasan/.qgis2/python
/home/hasan/.qgis2/python/plugins
/usr/share/qgis/python/plugins
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/home/hasan/.local/lib/python2.7/site-packages
/usr/local/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages/TileStache-1.50.1-py2.7.egg
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
/home/hasan/.qgis2//python
How can i resolve this conflict that is occuring due to a plugin build on windows and run on Linux?
1 Answer 1
The problem is addressed in the error: it is a TypeError. Your connection string is of the class newstr
, not unicode
or bytes
.
The __future__
package provides unicode utilities to Python 2 and overrides str()
but now the class is of future.types.newstr.newstr
.
While future.types.newstr.newstr
has inherited from unicode
it seem that the psycopg2 package doesn't check to see to see if the connection string is isinstance()
. Perhaps the __future__
package isn't something that was depended on to remain stable during the development of Python 3 and documentation wasn't amazing. It wouldn't be good to base your API on an unstable feature for easy unicode usage.
Try explicitly encoding the connection string by calling the encode()
method, which returns bytes encoded in utf-8.
I cannot see your code and my Python skills are super rusty but I believe this is the cause of your error. Why does this not happen on Windows? ...not sure maybe the Linux packages of Python are more strict, and I am not sure which version of Python your are using with QGIS on Windows.
More reading:
-
I have QGIS 2.18Ahsan Mukhtar– Ahsan Mukhtar2017年07月08日 05:54:13 +00:00Commented Jul 8, 2017 at 5:54
Explore related questions
See similar questions with these tags.
__future__
package withunicode_literals
?