Unable to import distutils.dir_util on Windows
Asked Answered
C

1

14

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:\apps\Python27\lib\distutils\dir_util.pyc'>

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

Cheesewood answered 20/9, 2013 at 4:42 Comment(2)
https://mcmap.net/q/196862/-backing-up-copying-an-entire-folder-tree-in-batch-or-pythonMenjivar
I found that answer incomplete, so I answered it here.Koster
K
28

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.

Koster answered 29/10, 2015 at 14:53 Comment(2)
Weird! The same for me on Python 3.6.4Vendible
and same on Python 3.5.2.Stationmaster

© 2022 - 2024 — McMap. All rights reserved.