Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overcome Python 3.4 NameError: name 'basestring' is not defined

Tags:

python-3.4

I've got a file called hello.txt in the local directory along side the test.py, which contains this Python 3.4 code:

import easywebdav
webdav = easywebdav.connect('192.168.1.6', username='myUser', password='myPasswd', protocol='http', port=80)
srcDir = "myDir"
webdav.mkdir(srcDir)
webdav.upload("hello.txt", srcDir)

When I run this I get this:

Traceback (most recent call last):
  File "./test.py", line 196, in <module>
    webdav.upload("hello.txt", srcDir)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/easywebdav/client.py", line 153, in upload
    if isinstance(local_path_or_fileobj, basestring):
NameError: name 'basestring' is not defined

Googling this results in several hits, all of which point to the same fix which, in case the paths moved in future, is to include "right after import types":

try:
    unicode = unicode
except NameError:
    # 'unicode' is undefined, must be Python 3
    str = str
    unicode = str
    bytes = bytes
    basestring = (str,bytes)
else:
    # 'unicode' exists, must be Python 2
    str = str
    unicode = unicode
    bytes = str
    basestring = basestring

I wasn't using import types, but to include it or not doesn't appear to make a difference in PyDev - I get an error either way. The line which causes an error is:

unicode = unicode

saying, 'undefined variable'.

OK my python knowledge falters at this point and I've looked for similar posts on this site and not found one specific enough to basestring that I understand to help. I know I need to specify basestring but I don't know how to. Would anyone be charitable enough to point me in the right direction?

like image 799
volvox Avatar asked Oct 21 '22 00:10

volvox


1 Answers

You can change easywebdav's client.py file like the top two changes in this checkin: https://github.com/hhaderer/easywebdav/commit/983ced508751788434c97b43586a68101eaee67b

The changes consist in replacing basestring by str in client.py.

like image 195
thebjorn Avatar answered Oct 24 '22 00:10

thebjorn