Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 480
DanH Avatar asked Sep 20 '13 04:09

DanH


1 Answers

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.

like image 127
kmarsh Avatar answered Sep 30 '22 19:09

kmarsh