Skip to content

Commit

Permalink
Fix bug: site may have no getusersitepackages
Browse files Browse the repository at this point in the history
  • Loading branch information
hsfzxjy committed Feb 8, 2021
1 parent 6e1dfb8 commit f174187
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lambdex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
import sys


def _sitepaths():
import site

if hasattr(site, 'getsitepackages'):
yield from site.getsitepackages()

if hasattr(site, 'getusersitepackages'):
yield site.getusersitepackages()


def _is_run_as_script():
"""
Check that whether lambdex is run as top-level script.
Expand All @@ -12,7 +22,6 @@ def _is_run_as_script():
If the importer has any system prefixes, we assert that lambdex is run
as top-level script.
"""
import site
from os.path import dirname, abspath

f = sys._getframe(1)
Expand All @@ -25,7 +34,7 @@ def _is_run_as_script():
return False

filename = abspath(f.f_code.co_filename)
for sitepath in site.getsitepackages() + [site.getusersitepackages()]:
for sitepath in _sitepaths():
prefix = dirname(dirname(dirname(sitepath))) # such as '/usr/local'
if filename.startswith(prefix):
return True
Expand All @@ -44,6 +53,6 @@ def _is_run_as_script():
from .keywords import *
from .keywords import __all__

del os, sys, _is_run_as_script
del os, sys, _sitepaths, _is_run_as_script

__version__ = "0.2.0"

0 comments on commit f174187

Please sign in to comment.