The SameEnvironment should not load by default if it's a portable

find_python_environments should only find Python versions if they are actually installed on the system. If people copy virtualenvs around etc. it will find nothing instead.
This commit is contained in:
Dave Halter
2018-04-13 21:53:06 +02:00
parent 8af4fc5728
commit fac773a60d

View File

@@ -20,6 +20,7 @@ _VersionInfo = namedtuple('VersionInfo', 'major minor micro')
_SUPPORTED_PYTHONS = ['3.6', '3.5', '3.4', '3.3', '2.7'] _SUPPORTED_PYTHONS = ['3.6', '3.5', '3.4', '3.3', '2.7']
_SAFE_PATHS = ['/usr/bin', '/usr/local/bin'] _SAFE_PATHS = ['/usr/bin', '/usr/local/bin']
_CURRENT_VERSION = '%s.%s' % (sys.version_info.major, sys.version_info.minor)
class InvalidPythonEnvironment(Exception): class InvalidPythonEnvironment(Exception):
@@ -155,6 +156,10 @@ def get_default_environment():
for environment in find_python_environments(): for environment in find_python_environments():
return environment return environment
# If no Python Environment is found, use the environment we're already
# using.
return SameEnvironment()
def find_virtualenvs(paths=None, **kwargs): def find_virtualenvs(paths=None, **kwargs):
""" """
@@ -204,15 +209,13 @@ def find_virtualenvs(paths=None, **kwargs):
def find_python_environments(): def find_python_environments():
""" """
Ignores virtualenvs and returns the different Python version environments. Ignores virtualenvs and returns the Python versions that were installed on
your system. This might return nothing, if you're running Python e.g. from
a portable version.
The environments are sorted from latest to oldest Python version. The environments are sorted from latest to oldest Python version.
""" """
current_version = '%s.%s' % (sys.version_info.major, sys.version_info.minor)
for version_string in _SUPPORTED_PYTHONS: for version_string in _SUPPORTED_PYTHONS:
if version_string == current_version:
yield SameEnvironment()
else:
try: try:
yield get_python_environment('python' + version_string) yield get_python_environment('python' + version_string)
except InvalidPythonEnvironment: except InvalidPythonEnvironment:
@@ -246,7 +249,10 @@ def get_python_environment(python):
""" """
exe = find_executable(python) exe = find_executable(python)
if exe: if exe:
if exe == sys.executable:
return SameEnvironment()
return _Environment(_get_python_prefix(exe), exe) return _Environment(_get_python_prefix(exe), exe)
if os.name == 'nt': if os.name == 'nt':
match = re.search('python(\d+\.\d+)$', python) match = re.search('python(\d+\.\d+)$', python)
if match: if match: