如何在python 3.6中使用分布式软件包?

2024-09-29 17:09:37 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试使用python3.6执行以下代码时,我遇到了一个问题,但它在python2.7上运行良好:

from distributed.compatability import FileExistsError
try:
   os.mkdir(name)
   logging.info("dir created")
except FileExistsError:
   logging.info("dir already exists")

Error:
======
 from distributed.compatability import FileExistsError
ImportError: cannot import name 'FileExistsError'

我已经使用conda安装了分布式软件包,但仍然收到相同的错误。我错过什么了吗?有人能帮我使它与python3.6一起工作吗


Tags: 代码namefromimportinfoosloggingdir
2条回答

正如@rozumir提到的,FileExistsError是在Python 3中添加的。因此,只需要在Python2中导入它

如果你查看他们的Github,你会发现它只是出于兼容性的原因添加的。因此命名为distributed.compatibility

它是在2016年10月的this提交中添加的,但是在2019年7月Python 2 support was dropped,消除了对^{中大部分内容的需要

所以本质上,在Python3版本中,比如Python3.6,FileExistsError就在那里,不需要导入它来使用它

不要从distributed.compatibility导入FileExistError,请删除它,因为FileExistError是标准库的一部分

import distributed.compatability
try:
   os.mkdir(name)
   logging.info("dir created")
except FileExistsError:
   logging.info("dir already exists")

相关问题 更多 >

    热门问题