pip3和python3的区别设置.pyinstall`about cmdclass argumen

2024-03-29 09:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图配置我的包,以便在安装过程中执行脚本。因此,我继承了setuptools.命令安装并创建了我的自定义类ActionOnInstall,以便在安装包时执行操作。这个类是通过setuptoolssetup()参数{}调用的,如here所述。在

一个很小的例子设置.py文件看起来像

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})

通过执行

^{pr2}$

运行成功,但不执行ActionOnInstall.run()中指定的命令。更直接的说法是设置.py通过

python3 setup.py install 

执行ActionOnInstall.run()中指定的命令。在

然后,我发现自己在问:这两种安装软件包的方法的实际区别是什么。我知道,就像其他帖子告诉我们的那样,pip让安装包的生活更轻松。但是这两种方法是如何区别对待cmdclass参数的setup()没有解释。因此,我非常感谢你们的来信。在


Tags: install方法runnamefrompyimport命令
2条回答

pip在安装包时运行python setup.py install,它不会改变设置.py感觉被执行。在

您看不到任何输出的原因是,如@phd所述,pip默认情况下会隐藏运行setup.py文件的所有输出,因为运行python setup.py install时打印的大多数信息对大多数用户都没有用。在

通过将“verbose”选项传递给pip install,您可以看到这个隐藏的输出以及pip所做的一切:

$ 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...

皮普打电话给你设置.py但它会重定向stdout/stderr。测试设置.py在pip write to a file in a fixed location(pip写入固定位置的文件)下:

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

调查/tmp/调试日志在pip install .之后

相关问题 更多 >