如何克服 Python 3.4 NameError: name 'basestring' 未定义 [英] How to overcome Python 3.4 NameError: name 'basestring' is not defined

查看:67
本文介绍了如何克服 Python 3.4 NameError: name 'basestring' 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地目录中的 test.py 旁边有一个名为 hello.txt 的文件,其中包含此 Python 3.4 代码:

导入easywebdavwebdav = easywebdav.connect('192.168.1.6', username='myUser', password='myPasswd', protocol='http', port=80)srcDir = "我的目录"webdav.mkdir(srcDir)webdav.upload("hello.txt", srcDir)

当我运行这个时,我得到这个:

回溯(最近一次调用最后一次):文件./test.py",第 196 行,位于 <module>webdav.upload("hello.txt", srcDir)上传文件/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/easywebdav/client.py",第153行如果是实例(local_path_or_fileobj,basestring):NameError: name 'basestring' 未定义

谷歌搜索结果有几个点击,所有这些指向相同的修复,以防将来移动路径,将包括导入类型之后":

尝试:单码 = 单码除了名称错误:# 'unicode' 未定义,必须是 Python 3str = strunicode = str字节 = 字节basestring = (str,bytes)别的:# 'unicode' 存在,必须是 Python 2str = str单码 = 单码字节 = str基串 = 基串

我没有使用导入类型,但是包含与否在 PyDev 中似乎没有什么不同 - 无论哪种方式,我都会收到错误消息.导致错误的行是:

unicode = unicode

说,'未定义的变量'.

好的,我的 Python 知识在这一点上停滞不前,我在这个网站上寻找了类似的帖子,但没有找到一个足够具体的 basestring,我理解可以提供帮助.我知道我需要指定 basestring,但我不知道如何指定.有没有人愿意为我指明正确的方向?

解决方案

您可以更改 easywebdav 的 client.py 文件,就像此签入中的前两个更改一样:https://github.com/hhaderer/easywebdav/commit/983ced508751788434c97b6815086e

更改包括在 client.py 中将 basestring 替换为 str.

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?

解决方案

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.

这篇关于如何克服 Python 3.4 NameError: name 'basestring' 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆