1

I followed mertyildiran's top answer to write a post-install script in setup.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,
    },
)

..but only when I pip install -e . do I get the output in the test file. When I try pip install . nothing gets written to my desired output file because the file path changes to /tmp/pip-req-build-*

How can I get the path to the package directory before setup?

Christian Macht
  • 456
  • 4
  • 20
  • It seems like I can get the path from `os.environ.get("PWD")` - is there anything wrong with this? – Christian Macht Jul 11 '19 at 01:02
  • ...this is from the Python 3 docs: "This mapping (`os.environ`) is captured the first time the os module is imported, typically during Python startup as part of processing site.py. Changes to the environment made after this time are not reflected in os.environ, except for changes made by modifying os.environ directly." https://docs.python.org/3/library/os.html – Christian Macht Jul 12 '19 at 12:25

1 Answers1

2

pip builds a wheel right away. That includes installing to a temporary directory as an intermediate step. So everything that's not eligible for packaging is lost.

While editable installation just runs setup.py develop.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • Thanks, that explains it! Any idea how I can get the original package path during the wheel build process? – Christian Macht Jul 11 '19 at 01:01
  • @ChristianMacht [the documentation](https://docs.python.org/3/distutils/apiref.html) is sketchy about that. Judging by the [implementation of `distutils.command.build_py`](https://github.com/python/cpython/blob/master/Lib/distutils/command/build_py.py#L42), it gets most info from the `Distribution` object (that is passed to a command's constructor). – ivan_pozdeev Jul 11 '19 at 02:17