How to Use Python Distutils

How to use Python distutils?

See the distutils simple example. That's basically what it is like, except real install scripts usually contain a bit more information. I have not seen any that are fundamentally more complicated, though. In essence, you just give it a list of what needs to be installed. Sometimes you need to give it some mapping dicts since the source and installed trees might not be the same.

Here is a real-life (anonymized) example:

#!/usr/bin/python 

from distutils.core import setup

setup (name = 'Initech Package 3',
description = "Services and libraries ABC, DEF",
author = "That Guy, Initech Ltd",
author_email = "that.guy@initech.com",
version = '1.0.5',
package_dir = {'Package3' : 'site-packages/Package3'},
packages = ['Package3', 'Package3.Queries'],
data_files = [
('/etc/Package3', ['etc/Package3/ExternalResources.conf'])
])

how to install python distutils

The simplest way to install setuptools when it isn't already there and you can't use a package manager is to download ez_setup.py and run it with the appropriate Python interpreter. This works even if you have multiple versions of Python around: just run ez_setup.py once with each Python.

Edit: note that recent versions of Python 3 include setuptools in the distribution so you no longer need to install separately. The script mentioned here is only relevant for old versions of Python.

Execute a Python script post install using distutils / setuptools

The way to address these deficiences is:

  1. Get the full path to the Python interpreter executing setup.py from sys.executable.
  2. Classes inheriting from distutils.cmd.Command (such as distutils.command.install.install which we use here) implement the execute method, which executes a given function in a "safe way" i.e. respecting the dry-run flag.

    Note however that the --dry-run option is currently broken and does not work as intended anyway.

I ended up with the following solution:

import os, sys
from distutils.core import setup
from distutils.command.install import install as _install

def _post_install(dir):
from subprocess import call
call([sys.executable, 'scriptname.py'],
cwd=os.path.join(dir, 'packagename'))

class install(_install):
def run(self):
_install.run(self)
self.execute(_post_install, (self.install_lib,),
msg="Running post install task")

setup(
...
cmdclass={'install': install},
)

Note that I use the class name install for my derived class because that is what python setup.py --help-commands will use.

How to require and install a package using python 3.x distutils?

Automatic downloading of dependencies is a feature introduced by setuptools which is a third-party add-on to distutils, in particular, the install_requires argument it adds. See the setuptools documentation for more information.

Another option is to use requirements.txt file with pip rather than using easy_install as a package installer. pip has now become the recommended installer; see the Python Packaging User Guide for more information.

Update [2015-01]: The previous version of this answer referred to the distribute fork of setuptools. The distribute fork has since been merged back into a newer active setuptools project. distribute is now dead and should no longer be used. setuptools and pip are now very actively maintained and support Python 3.

no module named distutils....but distutils installed?

It looks like distutils has versioning , so after

jeremy@jeremy-Blade:~$ sudo apt-get install python3.10-distutils 
Reading package lists... Done
Building dependency tree
Reading state information... Done
...
Setting up python3.10-lib2to3 (3.10.0-1+focal1) ...
Setting up python3.10-distutils (3.10.0-1+focal1) ...

jeremy@jeremy-Blade:~$ python3.10 -m pip install opencv-python

seems to be able to proceed.

Trying to install Distutils and receiving an error that I need distutils.core

distutils is a part of Python standard library since the dawn of time. You don't need to install the package, it must always be available. If it's not available your standard library is broken and should be fixed, perhaps by reinstalling.

If you're trying to install this Distutils — it's a very old version from 2007. Forget about it.



Related Topics



Leave a reply



Submit