Python setuptools符号链接和自定义安装扩展

时间:2018-03-16 17:13:02

标签: python setuptools packaging

我想为我的python包实现以下两个功能,它包含setup.py并使用setuptools:

  1. 允许使用符号链接进行安装,而不是复制包 文件
  2. 允许在使用“install”时运行自定义代码 setup.py
  3. 首先,我通常会这样做:

    pip install -e . --user
    

    它工作正常。

    对于第二个,我在setup.py中执行:

    from __future__ import print_function                                                                                                                
    from setuptools import setup                                                                                                                         
    from setuptools.command.install import install                                                                                                       
    import os                                                                                                                                            
    
    
    class CustomInstallCommand(install):                                                                                                                 
        def run(self):                                                                                                                                   
            print ("Custom code here")                                                                                                  
            install.run(self)                                                                                                                            
    
    setup(...,
    packages=['package_name'],                                                                                                                         
          package_dir={'package_name':'package_name'},                                                                                                           
          cmdclass={'install': CustomInstallCommand},                                                                                                    
          zip_safe=False)     
    

    然而,我发现:

    1. 如果我按上述方式运行pip,则永远不会调用自定义代码
    2. 如果我这样做,自定义代码会运行:

      python setup.py install --user

    3. 但我不确定如何将此命令与等效-e选项一起使用,以便安装符号链接而不是文件副本。我怎样才能实现这两个目标?

1 个答案:

答案 0 :(得分:1)

那是因为python setup.py install不会被调用。有两种模式可供选择:

  • 通过python setup.py develop调用“真实”安装模式并复制源
  • “开发”模式通过develop调用,只会为来源创建符号链接。

所以你必须覆盖install命令,就像使用from setuptools.command.develop import develop ... class CustomDevInstallCommand(develop): def run(self): print('running custom develop command') super(CustomDevInstallCommand, self).run() setup( ..., cmdclass={ 'install': CustomInstallCommand, 'develop': CustomDevInstallCommand, }, ) 一样:

pip install --editable .

通过$ pip install --editable . -v Created temporary directory: /private/var/folders/_y/2qk6029j4c7bwv0ddk3p96r00000gn/T/pip-ephem-wheel-cache-1yw7baz2 ... Installing collected packages: spam Running setup.py develop for spam Running command python -c "import setuptools" Running command /Users/hoefling/.virtualenvs/stackoverflow/bin/python -c "import setuptools, tokenize;__file__='/Users/hoefling/projects/private/stackoverflow/so-49326214/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" develop --no-deps running develop running custom develop command running egg_info ... Successfully installed spam Cleaning up... 安装产生:

$ResourceGroupName = "group"
$ServerName = "serverName"
$DatabaseName = "databaseName"
$StorageKeyType = "StorageAccessKey"
$StorageKey = "storageKey"
$StorageUri = "storageUri"
$AdminLogin = "admin"
$AdminPassword = (ConvertTo-SecureString "12345" -AsPlainText -Force)

New-AzureRmSqlDatabaseExport `
        -AdministratorLogin $AdminLogin `
        -AdministratorLoginPassword $AdminPassword `
        -DatabaseName $DatabaseName `
        -ResourceGroupName $ResourceGroupName `
        -ServerName $ServerName `
        -StorageKey $StorageKey `
        -StorageKeyType $StorageKeyType `
        -StorageUri $StorageUri `