Created
May 8, 2018 12:46
-
-
Save DIYer22/5cc015dcef53d62c16bdff0f8f345e96 to your computer and use it in GitHub Desktop.
Revisions
-
DIYer22 revised this gist
May 8, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -42,6 +42,7 @@ so, we can auto detection all Python Environment by following code: ''' import sys,os def jupyterNotebookOrQtConsole(): env = 'Unknow' cmd = 'ps -ef' @@ -67,7 +68,6 @@ def jupyterNotebookOrQtConsole(): except: return env pyv = sys.version_info.major py3 = (pyv == 3) py2 = (pyv == 2) -
DIYer22 created this gist
May 8, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,99 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue May 8 20:40:30 2018 @author: yanglei """ ''' # Detection Python Environment, Especially distinguish Spyder, Jupyter notebook, Qtconsole As far as I know, Here has 3 kinds of ipython that used `ipykernel` 1. `ipython qtconsole` ("qtipython" for short) 2. IPython in spyder ("spyder" for short) 3. IPython in jupyter notebook ("jn" for short) use `'spyder' in sys.modules` can distinguish spyder but for qtipython and jn are hard to distinguish cause they have same `sys.modules` and same IPython config:`get_ipython().config` I find a different between qtipython and jn: first run `os.getpid()` in IPython shell get the pid number then run `ps -ef|grep [pid number]` my qtipython pid is 8699 ``` yanglei 8699 8693 4 20:31 ? 00:00:01 /home/yanglei/miniconda2/envs/py3/bin/python -m ipykernel_launcher -f /run/user/1000/jupyter/kernel-8693.json ``` my jn pid is 8832 ``` yanglei 8832 9788 13 20:32 ? 00:00:01 /home/yanglei/miniconda2/bin/python -m ipykernel_launcher -f /run/user/1000/jupyter/kernel-ccb962ec-3cd3-4008-a4b7-805a79576b1b.json ``` the different of qtipython and jn is the ipython's json name, jn's json name are longer than qtipython's so, we can auto detection all Python Environment by following code: ''' def jupyterNotebookOrQtConsole(): env = 'Unknow' cmd = 'ps -ef' try: with os.popen(cmd) as stream: if not py2: stream = stream._stream s = stream.read() pid = os.getpid() ls = list(filter(lambda l:'jupyter' in l and str(pid) in l.split(' '), s.split('\n'))) if len(ls) == 1: l = ls[0] import re pa = re.compile(r'kernel-([-a-z0-9]*)\.json') rs = pa.findall(l) if len(rs): r = rs[0] if len(r)<12: env = 'qtipython' else : env = 'jn' return env except: return env import sys,os pyv = sys.version_info.major py3 = (pyv == 3) py2 = (pyv == 2) class pyi(): ''' python info plt : Bool mean plt avaliable env : belong [cmd, cmdipython, qtipython, spyder, jn] ''' pid = os.getpid() gui = 'ipykernel' in sys.modules cmdipython = 'IPython' in sys.modules and not gui ipython = cmdipython or gui spyder = 'spyder' in sys.modules if gui: env = 'spyder' if spyder else jupyterNotebookOrQtConsole() else: env = 'cmdipython' if ipython else 'cmd' cmd = not ipython qtipython = env == 'qtipython' jn = env == 'jn' plt = gui or 'DISPLAY' in os.environ print('Python Envronment is %s'%pyi.env)