30
30
'SearchPaths' ,
31
31
[('python_path' , Tuple [str , ...]), # where user code is found
32
32
('mypy_path' , Tuple [str , ...]), # from $MYPYPATH or config variable
33
- ('package_path' , Tuple [str , ...]), # from get_site_packages_dirs ()
33
+ ('package_path' , Tuple [str , ...]), # from get_search_dirs ()
34
34
('typeshed_path' , Tuple [str , ...]), # paths in typeshed
35
35
])
36
36
@@ -608,28 +608,7 @@ def default_lib_path(data_dir: str,
608
608
609
609
610
610
@functools .lru_cache (maxsize = None )
611
- def get_prefixes (python_executable : Optional [str ]) -> Tuple [str , str ]:
612
- """Get the sys.base_prefix and sys.prefix for the given python.
613
-
614
- This runs a subprocess call to get the prefix paths of the given Python executable.
615
- To avoid repeatedly calling a subprocess (which can be slow!) we
616
- lru_cache the results.
617
- """
618
- if python_executable is None :
619
- return '' , ''
620
- elif python_executable == sys .executable :
621
- # Use running Python's package dirs
622
- return pyinfo .getprefixes ()
623
- else :
624
- # Use subprocess to get the package directory of given Python
625
- # executable
626
- return ast .literal_eval (
627
- subprocess .check_output ([python_executable , pyinfo .__file__ , 'getprefixes' ],
628
- stderr = subprocess .PIPE ).decode ())
629
-
630
-
631
- @functools .lru_cache (maxsize = None )
632
- def get_site_packages_dirs (python_executable : Optional [str ]) -> Tuple [List [str ], List [str ]]:
611
+ def get_search_dirs (python_executable : Optional [str ]) -> Tuple [List [str ], List [str ], List [str ]]:
633
612
"""Find package directories for given python.
634
613
635
614
This runs a subprocess call, which generates a list of the egg directories, and the site
@@ -638,17 +617,17 @@ def get_site_packages_dirs(python_executable: Optional[str]) -> Tuple[List[str],
638
617
"""
639
618
640
619
if python_executable is None :
641
- return [], []
620
+ return [], [], []
642
621
elif python_executable == sys .executable :
643
622
# Use running Python's package dirs
644
- site_packages = pyinfo .getsitepackages ()
623
+ site_packages , sys_path = pyinfo .getsearchdirs ()
645
624
else :
646
625
# Use subprocess to get the package directory of given Python
647
626
# executable
648
- site_packages = ast .literal_eval (
649
- subprocess .check_output ([python_executable , pyinfo .__file__ , 'getsitepackages ' ],
627
+ site_packages , sys_path = ast .literal_eval (
628
+ subprocess .check_output ([python_executable , pyinfo .__file__ , 'getsearchdirs ' ],
650
629
stderr = subprocess .PIPE ).decode ())
651
- return expand_site_packages (site_packages )
630
+ return expand_site_packages (site_packages ) + ( sys_path ,)
652
631
653
632
654
633
def expand_site_packages (site_packages : List [str ]) -> Tuple [List [str ], List [str ]]:
@@ -781,10 +760,8 @@ def compute_search_paths(sources: List[BuildSource],
781
760
if options .python_version [0 ] == 2 :
782
761
mypypath = add_py2_mypypath_entries (mypypath )
783
762
784
- egg_dirs , site_packages = get_site_packages_dirs (options .python_executable )
785
- base_prefix , prefix = get_prefixes (options .python_executable )
786
- is_venv = base_prefix != prefix
787
- for site_dir in site_packages :
763
+ egg_dirs , site_packages , sys_path = get_search_dirs (options .python_executable )
764
+ for site_dir in site_packages + sys_path :
788
765
assert site_dir not in lib_path
789
766
if (site_dir in mypypath or
790
767
any (p .startswith (site_dir + os .path .sep ) for p in mypypath ) or
@@ -793,15 +770,10 @@ def compute_search_paths(sources: List[BuildSource],
793
770
print ("See https://mypy.readthedocs.io/en/stable/running_mypy.html"
794
771
"#how-mypy-handles-imports for more info" , file = sys .stderr )
795
772
sys .exit (1 )
796
- elif site_dir in python_path and (is_venv and not site_dir .startswith (prefix )):
797
- print ("{} is in the PYTHONPATH. Please change directory"
798
- " so it is not." .format (site_dir ),
799
- file = sys .stderr )
800
- sys .exit (1 )
801
773
802
774
return SearchPaths (python_path = tuple (reversed (python_path )),
803
775
mypy_path = tuple (mypypath ),
804
- package_path = tuple (egg_dirs + site_packages ),
776
+ package_path = tuple (egg_dirs + site_packages + sys_path ),
805
777
typeshed_path = tuple (lib_path ))
806
778
807
779
0 commit comments