在Windows上无法导入distutils.dir_util [英] Unable to import distutils.dir_util on Windows

查看:285
本文介绍了在Windows上无法导入distutils.dir_util的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows 7 64bit上使用distutils.dir_util。据我了解,我可能需要单独安装一些distutils软件包吗?我确实有基本的distutils软件包,但是它似乎非常精简,缺少许多组件。尝试研究distutils和Windows总是使我进入python构建脚本,以及如何将distutils打包为可再发行的python项目的一部分或构建我不感兴趣的EXE,我根本看不出在哪里获得此东西的任何吸引力

I'm trying to using distutils.dir_util on Windows 7 64bit. As far as I can glean from various googling, I may need to install some distutils package separately? I do have the base distutils package available, however it seems to be very stripped down and many components missing. Trying to research distutils and windows always leads me to python build scripts and how to package distutils as part of redistributable python projects or building EXEs which I'm not interested in, I simply can't see to get any traction on where to acquire this code from.

已经很长时间了,但是我想我是从MSI安装程序安装Python的,不确定其他方法是否通用。这是我的解释器输出:

It's been a long time, however I think I installed Python from an MSI installer, not sure if other methods are common. Here's my interpreter output:

Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils
>>> distutils.dir_util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'dir_util'

确定看起来像这样可能使用以下代码:

OK looks like this is possible with the following code:

Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils.core
>>> distutils.dir_util
<module 'distutils.dir_util' from 'C:\apps\Python27\lib\distutils\dir_util.pyc'>

任何人都可以解释为什么需要这种方法或它如何工作吗?

Can anybody please explain why this approach is needed or how it even works?

推荐答案

在2.7.5,我拥有的最早的版本中,以下对我有用:

On 2.7.5, the oldest I have available, the following works for me:


    #Both imports are needed to avoid errors
    import distutils
    from distutils import dir_util

    distutils.dir_util.copy_tree("./foo", "./bar")


如果我遗漏了 import distutils,则会出现错误:

If I leave out "import distutils", I get the error:


NameError:名称'distutils'未定义

NameError: name 'distutils' is not defined

如果从导入中忽略掉,则会出现错误:

If I leave out the from import, I get the error:


AttributeError:'模块'对象没有属性'dir_util'

AttributeError: 'module' object has no attribute 'dir_util'

对于您提出的最后一个问题,我只能猜测为什么,也许开发人员可能想隐藏distutils导入的大块尺寸/ spac e /效率原因。

For the last question you ask, I can only guess at why, perhaps the developers may have wanted to hide large parts for the distutils import for size/space/efficiency reasons.

这篇关于在Windows上无法导入distutils.dir_util的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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