在setup.py中进行pip安装期间获取文件路径

时间:2019-07-10 23:18:11

标签: python pip setuptools

我跟随mertyildiran's top answersetup.py中编写了安装后脚本:

from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install

here = os.path.abspath(os.path.dirname(__file__))

class PostDevelopCommand(develop):
    """Post-installation for development mode."""
    def run(self):
        file = os.path.join(here, 'TESTFILE')
        print(file) # /path/to/my/package/TESTFILE
        with open(file, 'a') as file:
            file.write('This is development.')
        develop.run(self)

class PostInstallCommand(install):
    """Post-installation for installation mode."""
    def run(self):
        file = os.path.join(here, 'TESTFILE')
        print(file) # /tmp/pip-req-build-*/TESTFILE
        with open(file, 'a') as file:
            file.write('This is Sparta!')    
        install.run(self)

setup(
    ..
    cmdclass={
        'install': PostInstallCommand,
        'develop': PostDevelopCommand,
    },
)

..但是只有当我pip install -e .时,我才会在测试文件中获得输出。当我尝试pip install .时,没有任何内容写入所需的输出文件,因为文件路径更改为/tmp/pip-req-build-*

如何在设置之前获取包目录的路径?

1 个答案:

答案 0 :(得分:1)

pip builds a wheel right away。这包括安装到临时目录作为中间步骤。因此,所有不符合包装条件的物品都会丢失。

可编辑的安装just runs setup.py develop中。

相关问题