`import somemodule` does not check all paths in `sys.path`

stefand986 at gmail.com stefand986 at gmail.com
Mon Jul 30 03:18:49 EDT 2018


Note that in the `sys.path`variable the path is given as absolute path (I only wrote it relative to `~` in the in my post, but in the script the absolute path is used everywhere). See line 2:

  ``` 
  ['/home/pyuser/.homeassistant', 
   '/home/pyuser/.homeassistant/deps/lib/python3.7/site-packages',
   '/home/pyuser', 
   '/usr/local/lib/python37.zip', 
   '/usr/local/lib/python3.7', 
   '/usr/local/lib/python3.7/lib-dynload', 
   '/home/pyuser/.local/lib/python3.7/site-packages', 
   '/usr/local/lib/python3.7/site-packages'] 
  ``` 

Also by the time the import happens, the directory is existing. It is not existing from the beginning, but I also tried calling `importlib.invalidate_caches()` after installing the packages, which should force python to check the directories again.

Another interesting thing that I found out is that if I use the (now deprecated) `imp.find_module('sqlalchemy')` it returns the correct path. However if I do the same with `importlib.util.find_spec('sqlalchemy')`  the package is not found! (`importlib` is the suggested replacement for `imp`).

To me it really looks like a bug in `importlib`.



Am Montag, 30. Juli 2018 07:16:01 UTC+2 schrieb dieter:
> When I remember right:
> 
>  * Python silently ignores non existent directories in "sys.path"
>  * "~" is not automatically expanded into the home directory.
> 
> This would suggest, remplace "~/.homeassistent/..." by
> "/home/pyuser/.homeassistent/..." and try again.



More information about the Python-list mailing list