【问题标题】:Pip not picking up a custom install cmdclassPip 没有选择自定义安装 cmdclass
【发布时间】:2013-10-24 15:01:21
【问题描述】:

所以我找到了this answer 我的确切问题,但由于某种原因它不起作用:

$ cat /tmp/testinstall/setup.py:

from setuptools.command.install import install

from setuptools import setup


class verifying_install(install):
    def run(self):
        print "running........"
        install.run(self)
        print "verifying........"


setup(name='test',
      version='1',
      py_modules=['test'],
      include_package_data=True,
      zip_safe=True,
      cmdclass={'install': verifying_install}
)

但是,即使setup.py install 有效::

➜  /tmp/testinstall
$ mktmpenv && cd -
This is a temporary environment. It will be deleted when you run 'deactivate'.

(5bc7db7ca1b34ec5)➜  /tmp/testinstall
$ python setup.py install
running install
running........
running build
running build_py
creating build
creating build/lib.linux-x86_64-2.7
copying test.py -> build/lib.linux-x86_64-2.7
running egg_info
creating test.egg-info
writing test.egg-info/PKG-INFO
writing top-level names to test.egg-info/top_level.txt
writing dependency_links to test.egg-info/dependency_links.txt
writing manifest file 'test.egg-info/SOURCES.txt'
reading manifest file 'test.egg-info/SOURCES.txt'
writing manifest file 'test.egg-info/SOURCES.txt'
running install_lib
copying build/lib.linux-x86_64-2.7/test.py -> /home/bwm/.virtualenvs/5bc7db7ca1b34ec5/lib/python2.7/site-packages
byte-compiling /home/bwm/.virtualenvs/5bc7db7ca1b34ec5/lib/python2.7/site-packages/test.py to test.pyc
running install_egg_info
Copying test.egg-info to /home/bwm/.virtualenvs/5bc7db7ca1b34ec5/lib/python2.7/site-packages/test-1-py2.7.egg-info
running install_scripts
verifying........

(注意running...verifying...... 行)

pip install的目录不起作用:

(5bc7db7ca1b34ec5)➜  /tmp/testinstall
$ deactivate && mktmpenv && cd - && pip install .
Removing temporary environment: 5bc7db7ca1b34ec5
Removing 5bc7db7ca1b34ec5...
New python executable in 4cac61c13d080257/bin/python
Installing Setuptools...done.
Installing Pip....done.
This is a temporary environment. It will be deleted when you run 'deactivate'.
/tmp/testinstall
Unpacking /tmp/testinstall
  Running setup.py egg_info for package from file:///tmp/testinstall

Cleaning up...

sdist 的 pip install 也不起作用:

(4cac61c13d080257)➜  /tmp/testinstall
$ python setup.py sdist
running sdist
# ..snip..
creating dist
Creating tar archive
removing 'test-1' (and everything under it)
(4cac61c13d080257)➜  /tmp/testinstall
$ deactivate && mktmpenv && cd -
Removing temporary environment: 4cac61c13d080257
Removing 4cac61c13d080257...
New python executable in 9a42f3a58809f1a3/bin/python
Installing Setuptools...done.
Installing Pip...done.
This is a temporary environment. It will be deleted when you run 'deactivate'.
/tmp/testinstall

(9a42f3a58809f1a3)➜  /tmp/testinstall
$ ls dist
test-1.tar.gz
(9a42f3a58809f1a3)➜  /tmp/testinstall
$ pip install dist/test-1.tar.gz
Unpacking ./dist/test-1.tar.gz
  Running setup.py egg_info for package from file:///tmp/testinstall/dist/test-1.tar.gz

Cleaning up...

请注意这两个词中都缺少 running...verifying... 字样。

有人知道这里发生了什么吗?

【问题讨论】:

    标签: python pip


    【解决方案1】:

    我刚才遇到了这个问题。看起来pip install my-package 可以翻译成许多不同的命令。

    1. setup.py install
    2. setup.py egg_info
    3. setup.py develop

    所以你需要处理这些不同的情况。

    from setuptools.command.install import install
    from setuptools.command.develop import develop
    from setuptools.command.egg_info import egg_info
    
    '''
    BEGIN CUSTOM INSTALL COMMANDS
    These classes are used to hook into setup.py's install process. Depending on the context:
    $ pip install my-package
    
    Can yield `setup.py install`, `setup.py egg_info`, or `setup.py develop`
    '''
    
    
    def custom_command():
        import sys
        if sys.platform in ['darwin', 'linux']:
            os.system('./custom_command.sh')
    
    
    class CustomInstallCommand(install):
        def run(self):
            install.run(self)
            custom_command()
    
    
    class CustomDevelopCommand(develop):
        def run(self):
            develop.run(self)
            custom_command()
    
    
    class CustomEggInfoCommand(egg_info):
        def run(self):
            egg_info.run(self)
            custom_command()
    
    '''
    END CUSTOM INSTALL COMMANDS 
    '''
    
    setup(
        ...
        cmdclass={
            'install': CustomInstallCommand,
            'develop': CustomDevelopCommand,
            'egg_info': CustomEggInfoCommand,
        },
        ...
    )
    

    【讨论】:

    • 我已经尝试过了,但是在尝试从 pypi 安装 sdist 包时它不起作用。请参阅相关讨论chat.stackoverflow.com/rooms/150536/…。只有本地安装和直接从 git 存储库安装似乎有效。
    • 你终于找到解决这个问题的办法了吗?我也面临同样的情况
    【解决方案2】:

    我遇到了同样的问题。尝试运行pip install -vvv <path> - 可能是消息以某种方式被隐藏(我不知道为什么 - 不是点子专家!)。在任何情况下,您都可以通过将自定义代码打印到某个文件而不是 STDOUT 来确认代码正在运行。

    【讨论】:

    【解决方案3】:

    试试:

    pip install dist/test-1.tar.gz -U
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 2021-01-19
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      相关资源
      最近更新 更多