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
  File "/home/hasan/.qgis2/python/plugins/tplUserStatsReport/tplUserStatsReport.py", line 203, in initGui
  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
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:

How can i resolve this conflict that is occuring due to a plugin build on windows and run on Linux?

  • I cannot tell from your error but are you using Python 2.7 with the __future__ package with unicode_literals?
    – RomaH
    Jul 7 '17 at 21:24
  • I don't know about future package Jul 8 '17 at 5:54

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:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.