Search code examples
pythondistutils

distutils.spawn not available unless imported


I have distutils installed and it works in some cases. However when trying to use a submodule it won't import unless I explicitly import it.

$ python
Python 3.8.5 (default, Jul 21 2020, 10:42:08) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import distutils
>>> distutils.spawn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'distutils' has no attribute 'spawn'
>>> from distutils import spawn
>>> distutils.spawn
<module 'distutils.spawn' from '/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/distutils/spawn.py'>

I'm on Mac and have tried inside a venv and outside of one. I have a dependency that calls distutils.spawn.find_executable('python3') and isn't working.


Solution

  • This is perfectly normal. This is how Python import works. Unless distutils/__init__.py imports .spawn itself (which it doesn't do) you have to import it yourself to make it available. Importing just distutils is not enough to access submodules.

    Counterexample: import os makes os.path automatically available but that's because os.py makes it available for you.