From 0f61930872edc910020c125f6e899956043e0321 Mon Sep 17 00:00:00 2001 From: Sergei Silnov Date: Tue, 8 Jan 2019 11:40:49 +0100 Subject: [PATCH] python: Add check if current python is inside virtual environment --- tools/check_python_dependencies.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/check_python_dependencies.py b/tools/check_python_dependencies.py index 501595ad2c..c04c4dbd8c 100755 --- a/tools/check_python_dependencies.py +++ b/tools/check_python_dependencies.py @@ -14,9 +14,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +import argparse import os import sys -import argparse + try: import pkg_resources except Exception: @@ -34,6 +35,13 @@ def escape_backslash(path): return path +def is_virtualenv(): + """Detects if current python is inside virtualenv, pyvenv (python 3.4-3.5) or venv""" + + return (hasattr(sys, 'real_prefix') or + (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)) + + if __name__ == "__main__": idf_path = os.getenv("IDF_PATH") @@ -79,8 +87,10 @@ if __name__ == "__main__": else: print('Please refer to the Get Started section of the ESP-IDF Programming Guide for setting up the required' ' packages.') - print('Alternatively, you can run "{} -m pip install --user -r {}" for resolving the issue.' - ''.format(escape_backslash(sys.executable), escape_backslash(args.requirements))) + print('Alternatively, you can run "{} -m pip install {}-r {}" for resolving the issue.' + ''.format(escape_backslash(sys.executable), + '' if is_virtualenv() else '--user ', + escape_backslash(args.requirements))) sys.exit(1) print('Python requirements from {} are satisfied.'.format(args.requirements))