25
25
'SearchPaths' ,
26
26
[('python_path' , Tuple [str , ...]), # where user code is found
27
27
('mypy_path' , Tuple [str , ...]), # from $MYPYPATH or config variable
28
- ('package_path' , Tuple [str , ...]), # from get_site_packages_dirs ()
28
+ ('package_path' , Tuple [str , ...]), # from get_search_dirs ()
29
29
('typeshed_path' , Tuple [str , ...]), # paths in typeshed
30
30
])
31
31
@@ -585,28 +585,7 @@ def default_lib_path(data_dir: str,
585
585
586
586
587
587
@functools .lru_cache (maxsize = None )
588
- def get_prefixes (python_executable : Optional [str ]) -> Tuple [str , str ]:
589
- """Get the sys.base_prefix and sys.prefix for the given python.
590
-
591
- This runs a subprocess call to get the prefix paths of the given Python executable.
592
- To avoid repeatedly calling a subprocess (which can be slow!) we
593
- lru_cache the results.
594
- """
595
- if python_executable is None :
596
- return '' , ''
597
- elif python_executable == sys .executable :
598
- # Use running Python's package dirs
599
- return pyinfo .getprefixes ()
600
- else :
601
- # Use subprocess to get the package directory of given Python
602
- # executable
603
- return ast .literal_eval (
604
- subprocess .check_output ([python_executable , pyinfo .__file__ , 'getprefixes' ],
605
- stderr = subprocess .PIPE ).decode ())
606
-
607
-
608
- @functools .lru_cache (maxsize = None )
609
- def get_site_packages_dirs (python_executable : Optional [str ]) -> Tuple [List [str ], List [str ]]:
588
+ def get_search_dirs (python_executable : Optional [str ]) -> Tuple [List [str ], List [str ], List [str ]]:
610
589
"""Find package directories for given python.
611
590
612
591
This runs a subprocess call, which generates a list of the egg directories, and the site
@@ -615,17 +594,17 @@ def get_site_packages_dirs(python_executable: Optional[str]) -> Tuple[List[str],
615
594
"""
616
595
617
596
if python_executable is None :
618
- return [], []
597
+ return [], [], []
619
598
elif python_executable == sys .executable :
620
599
# Use running Python's package dirs
621
- site_packages = pyinfo .getsitepackages ()
600
+ site_packages , sys_path = pyinfo .getsearchdirs ()
622
601
else :
623
602
# Use subprocess to get the package directory of given Python
624
603
# executable
625
- site_packages = ast .literal_eval (
626
- subprocess .check_output ([python_executable , pyinfo .__file__ , 'getsitepackages ' ],
604
+ site_packages , sys_path = ast .literal_eval (
605
+ subprocess .check_output ([python_executable , pyinfo .__file__ , 'getsearchdirs ' ],
627
606
stderr = subprocess .PIPE ).decode ())
628
- return expand_site_packages (site_packages )
607
+ return expand_site_packages (site_packages ) + ( sys_path ,)
629
608
630
609
631
610
def expand_site_packages (site_packages : List [str ]) -> Tuple [List [str ], List [str ]]:
@@ -758,10 +737,8 @@ def compute_search_paths(sources: List[BuildSource],
758
737
if options .python_version [0 ] == 2 :
759
738
mypypath = add_py2_mypypath_entries (mypypath )
760
739
761
- egg_dirs , site_packages = get_site_packages_dirs (options .python_executable )
762
- base_prefix , prefix = get_prefixes (options .python_executable )
763
- is_venv = base_prefix != prefix
764
- for site_dir in site_packages :
740
+ egg_dirs , site_packages , sys_path = get_search_dirs (options .python_executable )
741
+ for site_dir in site_packages + sys_path :
765
742
assert site_dir not in lib_path
766
743
if (site_dir in mypypath or
767
744
any (p .startswith (site_dir + os .path .sep ) for p in mypypath ) or
@@ -770,15 +747,10 @@ def compute_search_paths(sources: List[BuildSource],
770
747
print ("See https://mypy.readthedocs.io/en/stable/running_mypy.html"
771
748
"#how-mypy-handles-imports for more info" , file = sys .stderr )
772
749
sys .exit (1 )
773
- elif site_dir in python_path and (is_venv and not site_dir .startswith (prefix )):
774
- print ("{} is in the PYTHONPATH. Please change directory"
775
- " so it is not." .format (site_dir ),
776
- file = sys .stderr )
777
- sys .exit (1 )
778
750
779
751
return SearchPaths (python_path = tuple (reversed (python_path )),
780
752
mypy_path = tuple (mypypath ),
781
- package_path = tuple (egg_dirs + site_packages ),
753
+ package_path = tuple (egg_dirs + site_packages + sys_path ),
782
754
typeshed_path = tuple (lib_path ))
783
755
784
756
0 commit comments