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:
- Get the full path to the Python interpreter executing
setup.py
fromsys.executable
. Classes inheriting from
distutils.cmd.Command
(such asdistutils.command.install.install
which we use here) implement theexecute
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
Detect Face Then Autocrop Pictures
Pandas Get Topmost N Records Within Each Group
What Are the Risks of Running 'Sudo Pip'
Having Django Serve Downloadable Files
How to Get the Ascii Value of a Character
Simple Prime Number Generator in Python
How to Calculate the Time Interval Between Two Time Strings
Pip Install from Pypi Works, But from Testpypi Fails (Cannot Find Requirements)
Staleelementexception When Iterating with Python
Why Use Python's Os Module Methods Instead of Executing Shell Commands Directly
How to Make a Discontinuous Axis in Matplotlib
Apply Function to Each Element of a List
How to Redirect 'Print' Output to a File
Create an Empty List with Certain Size in Python
Python List in SQL Query as Parameter