Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

python - Unable to import distutils.dir_util on Windows

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.

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:appsPython27libdistutilsdir_util.pyc'>

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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")

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

NameError: name 'distutils' is not defined

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

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...