Title: site.abs_paths should handle None __cached__ type
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, demian.brecht, eric.snow, ncoghlan
Priority: normal Keywords: patch

Created on 2018-05-01 17:56 by demian.brecht, last changed 2018-05-15 18:41 by brett.cannon.

Pull Requests
URL Status Linked Edit
PR 6675 closed demian.brecht, 2018-05-01 17:57
Messages (2)
msg316008 - (view) Author: Demian Brecht (demian.brecht) * (Python triager) Date: 2018-05-01 17:56
Echoing an email sent to python-list:

I recently ran into an issue using the site package and I wanted to confirm that I'm not doing something wrong here before creating an issue:

I have a need to `git subtree` modules into a Django project and then add the dependencies such that they're discoverable in code. I also need to have consistent behavior between `python (develop|install)` and `pip install -r requirements.txt` (the latter is used by Heroku for project installation). An example project that demonstrates the issue is here:

In the example, I've subtree'd requests into _vendor and forced site hooks to fire using `site.main()` in testme/ Forcing the execution is not needed when the package is actually installed using, but is needed when using `pip install -r requirements.txt`. This works great when running within a virtualenv, but I get the following on system python:

$ python3.6 ./ test
Traceback (most recent call last):
 File "./", line 22, in <module>
 File "/usr/local/lib/python3.6/site-packages/django/core/management/", line 364, in execute_from_command_line
 File "/usr/local/lib/python3.6/site-packages/django/core/management/", line 308, in execute
 File "/usr/local/lib/python3.6/site-packages/django/conf/", line 56, in __getattr__
 File "/usr/local/lib/python3.6/site-packages/django/conf/", line 41, in _setup
   self._wrapped = Settings(settings_module)
 File "/usr/local/lib/python3.6/site-packages/django/conf/", line 110, in __init__
   mod = importlib.import_module(self.SETTINGS_MODULE)
 File "/usr/local/lib/python3.6/importlib/", line 126, in import_module
   return _bootstrap._gcd_import(name[level:], package, level)
 File "<frozen importlib._bootstrap>", line 978, in _gcd_import
 File "<frozen importlib._bootstrap>", line 961, in _find_and_load
 File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
 File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
 File "<frozen importlib._bootstrap_external>", line 678, in exec_module
 File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
 File "/<path>/<to>/testme/testme/", line 13, in <module>
 File "/usr/local/lib/python3.6/", line 525, in main
 File "/usr/local/lib/python3.6/", line 110, in abs_paths
   m.__cached__ = os.path.abspath(m.__cached__)
 File "/usr/local/lib/python3.6/", line 369, in abspath
   path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not NoneType

Using venv and system comparisons for debugging doesn't help much as is different in the two environments. Any insight into what may be going on here or what I'm doing wrong would be much appreciated. I realize that I could just muck with PYTHONPATH, but I thought this approach would be a little nicer as it's transparent to the user.

Digging a little deeper, it seems that site.abs_paths fails when a module's __cached__ value is None. I haven't spent enough time digging into it to understand why this is valid behavior, hoping to get some insight from folks who have spent more time on it.
msg316692 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-05-15 18:41
I didn't bother digging as I assume this was done for a reason. I left a request for changes on the PR to make the code has a stricter `try` block.
Date User Action Args
2018-05-15 18:41:25brett.cannonsetmessages: + msg316692
2018-05-08 11:53:32serhiy.storchakasetnosy: + brett.cannon, ncoghlan, eric.snow

type: behavior
versions: + Python 3.7, Python 3.8
2018-05-01 17:57:41demian.brechtsetkeywords: + patch
stage: patch review
pull_requests: + pull_request6369
2018-05-01 17:56:11demian.brechtcreate