Closed anntzer closed 7 years ago
Hmm, what does this do? Does it run on interpreter initialization and prettify all tracebacks if the env var is set?
Yes. See last line of the following paragraph from https://docs.python.org/3.6/library/site.htm:
A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.
Ah, that's very interesting, thank you. I'd welcome a PR, or I will get to this myself later on.
See also https://github.com/ionelmc/python-hunter/blob/master/setup.py where I got this trick from, and https://github.com/xolox/python-coloredlogs/blob/master/setup.py for another usage.
An important point is that the site module (which is imported by default) already imports the os module at every interpreter startup (https://docs.python.org/3/library/os.html#os.environ), so the startup cost of the pth is basically 1) byte-compiling a tiny code fragment and 2) checking whether a dict entry is set, so that "should" be acceptable...
I would love it if someone implemented this and issued a PR.
The merged implementation is rather brittle; for example, it does not support packaging into a wheel
$ pip wheel tbvaccine==0.2 && unzip -l tbvaccine*.whl
Collecting tbvaccine==0.2
Using cached tbvaccine-0.2.0.tar.gz
Building wheels for collected packages: tbvaccine
Running setup.py bdist_wheel for tbvaccine ... done
Stored in directory: /tmp
Successfully built tbvaccine
Archive: tbvaccine-0.2.0-py3-none-any.whl
Length Date Time Name
--------- ---------- ----- ----
110 2017-07-22 12:18 usr/lib/python3.6/site-packages/tbvaccine.pth
715 2017-07-22 12:07 tbvaccine/tests.py
7674 2017-07-22 12:07 tbvaccine/tbv.py
22 2017-07-22 12:32 tbvaccine/version.py
104 2017-07-22 12:07 tbvaccine/__init__.py
1104 2017-07-22 12:07 tbvaccine/cli.py
1918 2017-07-22 12:07 tbvaccine/__main__.py
2653 2017-07-22 19:05 tbvaccine-0.2.0.dist-info/DESCRIPTION.rst
50 2017-07-22 19:05 tbvaccine-0.2.0.dist-info/entry_points.txt
1243 2017-07-22 19:05 tbvaccine-0.2.0.dist-info/metadata.json
10 2017-07-22 19:05 tbvaccine-0.2.0.dist-info/top_level.txt
92 2017-07-22 19:05 tbvaccine-0.2.0.dist-info/WHEEL
3541 2017-07-22 19:05 tbvaccine-0.2.0.dist-info/METADATA
1162 2017-07-22 19:05 tbvaccine-0.2.0.dist-info/RECORD
--------- -------
20398 14 files
Note the first entry, which should be tbvaccine.pth and not usr/lib/python3.6/site-packages/tbvaccine.pth.
You can really only know where to put the pth file when install.run
is being executed, see the link in the first post.
Ah, I see, thank you. I will have another look then.
I've created a new PR using the trick from https://github.com/ionelmc/python-hunter/blob/master/setup.py. I've tested this both with wheel and sdist and it seems to work, hopefully resolves this. It's a pity there is not a consistent and recommended way to do this and we have to resort to hacks.
Fantastic, thank you! Does wheel packaging work properly?
pip wheel . && unzip -l tbvaccine*.whl
Archive: tbvaccine-0.2.0-py3-none-any.whl
Length Date Time Name
--------- ---------- ----- ----
110 2017-07-22 11:45 tbvaccine.pth
1104 2017-07-25 17:21 tbvaccine/cli.py
7674 2017-07-25 17:21 tbvaccine/tbv.py
715 2017-07-25 17:21 tbvaccine/tests.py
22 2017-07-25 17:20 tbvaccine/version.py
104 2017-07-25 17:21 tbvaccine/__init__.py
1918 2017-07-25 17:21 tbvaccine/__main__.py
2653 2017-07-27 16:14 tbvaccine-0.2.0.dist-info/DESCRIPTION.rst
50 2017-07-27 16:14 tbvaccine-0.2.0.dist-info/entry_points.txt
1243 2017-07-27 16:14 tbvaccine-0.2.0.dist-info/metadata.json
10 2017-07-27 16:14 tbvaccine-0.2.0.dist-info/top_level.txt
97 2017-07-27 16:14 tbvaccine-0.2.0.dist-info/WHEEL
3541 2017-07-27 16:14 tbvaccine-0.2.0.dist-info/METADATA
1130 2017-07-27 16:14 tbvaccine-0.2.0.dist-info/RECORD
--------- -------
20371 14 files
tbvaccine.pth is where it should be, also I tested building the wheel and installing from it and it seems to work fine.
If tbvaccine provides (e.g.) a
tbvaccine.pth
file with contents similar tothis would make it much easier to switch its functionality on and off without having to modify the "vaccined" code. You could even load the kwargs to TBVaccine from the environment variable, say as json: see https://github.com/anntzer/mplcursors/blob/master/setup.py for a self-contained example.