]> granicus.if.org Git - esp-idf/commitdiff
python: Add check if current python is inside virtual environment
authorSergei Silnov <sergei.silnov@espressif.com>
Tue, 8 Jan 2019 10:40:49 +0000 (11:40 +0100)
committerRoland Dobai <dobai.roland@gmail.com>
Thu, 10 Jan 2019 08:37:21 +0000 (09:37 +0100)
tools/check_python_dependencies.py

index 501595ad2cefe9b4cbdcab4e14cc308c544f7c47..c04c4dbd8c7a43aae1c8681731fe930afc3d6c95 100755 (executable)
 # 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))