在pip安装期间获取文件路径设置.py

2024-05-15 21:48:09 发布

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

我跟随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-*,所以没有任何内容写入到我想要的输出文件中

如何在安装之前获取包目录的路径?在


Tags: installpippathrunfromimportselfhere