Difference between pip3 and `python3 setup.py inst

2020-03-12 05:30发布

问题:

I tried to configure my package such that a script is executed on the installation process. Therefore, I inherited from setuptools.command install and created my custom class ActionOnInstall to do stuff when package is installed. This class is called via setuptools setup() argument cmdclass as described here.

A minimal example of such a setup.py file looks like

from setuptools import find_packages, setup
from setuptools.command.install import install


class ActionOnInstall(install):
    def run(self):
        print("Call install.run(self) works!")
        install.run(self)


setup(name='name',
      cmdclass={
      'install': ActionOnInstall})

Building the package by executing

pip3 install <path-to-dir-with-setup.py>

runs successfully but does not execute commands specified in ActionOnInstall.run(). More directly calling this setup.py by

python3 setup.py install 

executes commands specified in ActionOnInstall.run().

Then, I found myself asking: what is the actual difference of these both approaches to install a package. I know, like other posts tell us, pip makes life easier regarding package installation. But how these both approaches treat the cmdclass argument of setup() differently is not explained. Thus, I would highly appreciate to hear from you guys.

回答1:

pip calls your setup.py but it redirects stdout/stderr. To test setup.py under pip write to a file in a fixed location:

class ActionOnInstall(install):
    def run(self):
        print("Call install.run(self) works!", file=open('/tmp/debug.log', 'w'))
        install.run(self)

Look into /tmp/debug.log after pip install .



回答2:

pip does run python setup.py install when installing your package - it does not change the way your setup.py feel is executed.

The reason you don't see any output is, as @phd mentioned, that pip by default hides all the output from running the setup.py file since most of the information printed when running python setup.py install is not useful to most users.

You can see this hidden output, along with everything else pip does, by passing the "--verbose" option to pip install:

$ pip install --verbose ./foo
Processing ./foo
Running setup.py (path:/private/var/folders/4d/bt0_xfx56bjfmmt2bv3r5_qh0000gn/T/pip-ti0o0gtu-build/setup.py) egg_info for package from file:///Users/pradyunsg/.venvwrap/venvs/t
mp-c0ebb35987c76ad/foo
    Running command python setup.py egg_info
    running egg_info
    creating pip-egg-info/foo.egg-info
    writing pip-egg-info/foo.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/foo.egg-info/dependency_links.txt
    writing top-level names to pip-egg-info/foo.egg-info/top_level.txt
    writing manifest file 'pip-egg-info/foo.egg-info/SOURCES.txt'
    reading manifest file 'pip-egg-info/foo.egg-info/SOURCES.txt'
    writing manifest file 'pip-egg-info/foo.egg-info/SOURCES.txt'
Source in /private/var/folders/4d/bt0_xfx56bjfmmt2bv3r5_qh0000gn/T/pip-ti0o0gtu-build has version 0.0.0, which satisfies requirement foo==0.0.0 from file:///Users/pradyunsg/.ve
nvwrap/venvs/tmp-c0ebb35987c76ad/foo
Could not parse version from link: file:///Users/pradyunsg/.venvwrap/venvs/tmp-c0ebb35987c76ad/foo
Installing collected packages: foo
Running setup.py install for foo ...     Running command /Users/pradyunsg/.venvwrap/venvs/tmp-c0ebb35987c76ad/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/privat
e/var/folders/4d/bt0_xfx56bjfmmt2bv3r5_qh0000gn/T/pip-ti0o0gtu-build/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(comp
ile(code, __file__, 'exec'))" install --record /var/folders/4d/bt0_xfx56bjfmmt2bv3r5_qh0000gn/T/pip-cetn8xa9-record/install-record.txt --single-version-externally-managed --compi
le --install-headers /Users/pradyunsg/.venvwrap/venvs/tmp-c0ebb35987c76ad/bin/../include/site/python3.6/foo
    running install
    Call install.run(self) works!
    running build
    running install_egg_info
    running egg_info
    creating foo.egg-info
    writing foo.egg-info/PKG-INFO
    writing dependency_links to foo.egg-info/dependency_links.txt
    writing top-level names to foo.egg-info/top_level.txt
    writing manifest file 'foo.egg-info/SOURCES.txt'
    reading manifest file 'foo.egg-info/SOURCES.txt'
    writing manifest file 'foo.egg-info/SOURCES.txt'
    Copying foo.egg-info to /Users/pradyunsg/.venvwrap/venvs/tmp-c0ebb35987c76ad/lib/python3.6/site-packages/foo-0.0.0-py3.6.egg-info
    running install_scripts
    writing list of installed files to '/var/folders/4d/bt0_xfx56bjfmmt2bv3r5_qh0000gn/T/pip-cetn8xa9-record/install-record.txt'
done
Removing source in /private/var/folders/4d/bt0_xfx56bjfmmt2bv3r5_qh0000gn/T/pip-ti0o0gtu-build
Successfully installed foo-0.0.0
Cleaning up...