parameters: (self)
docstring: Add the current virtualenv to sys.path so the user can import modules from it.
          This isn't perfect: it doesn't use the Python interpreter with which the
          virtualenv was built, and it ignores the --no-site-packages option. A
          warning will appear suggesting the user installs IPython in the
          virtualenv, but for many cases, it probably works well enough.
          Adapted from code snippets online.
          http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
docstring_summary: Add the current virtualenv to sys.path so the user can import modules from it.
          This isn't perfect: it doesn't use the Python interpreter with which the
          virtualenv was built, and it ignores the --no-site-packages option. A
          warning will appear suggesting the user installs IPython in the
          virtualenv, but for many cases, it probably works well enough.
          Adapted from code snippets online.
          http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
docstring_tokens: Add the current virtualenv to sys . path so the user can import modules from it . This isn t perfect : it doesn t use the Python interpreter with which the virtualenv was built and it ignores the -- no - site - packages option . A warning will appear suggesting the user installs IPython in the virtualenv but for many cases it probably works well enough . Adapted from code snippets online . http : // blog . ufsoft . org / 2009 / 1 / 29 / ipython - and - virtualenv
function: def init_virtualenv(self):
          """Add the current virtualenv to sys.path so the user can import modules from it.
          This isn't perfect: it doesn't use the Python interpreter with which the
          virtualenv was built, and it ignores the --no-site-packages option. A
          warning will appear suggesting the user installs IPython in the
          virtualenv, but for many cases, it probably works well enough.
          Adapted from code snippets online.
          http://blog.ufsoft.org/2009/1/29/ipython-and-virtualenv
          """
          if 'VIRTUAL_ENV' not in os.environ:
              # Not in a virtualenv
              return
          elif os.environ["VIRTUAL_ENV"] == "":
              warn("Virtual env path set to '', please check if this is intended.")
              return
  
          p = Path(sys.executable)
          p_venv = Path(os.environ["VIRTUAL_ENV"])
  
          # fallback venv detection:
          # stdlib venv may symlink sys.executable, so we can't use realpath.
          # but others can symlink *to* the venv Python, so we can't just use sys.executable.
          # So we just check every item in the symlink tree (generally <= 3)
          paths = [p]
          while p.is_symlink():
              p = Path(os.readlink(p))
              paths.append(p.resolve())
          
          # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible
          if p_venv.parts[1] == "cygdrive":
              drive_name = p_venv.parts[2]
              p_venv = (drive_name + ":/") / Path(*p_venv.parts[3:])
  
          if any(p_venv == p.parents[1] for p in paths):
              # Our exe is inside or has access to the virtualenv, don't need to do anything.
              return
  
          if sys.platform == "win32":
              virtual_env = str(Path(os.environ["VIRTUAL_ENV"], "Lib", "site-packages"))
          else:
              virtual_env_path = Path(
                  os.environ["VIRTUAL_ENV"], "lib", "python{}.{}", "site-packages"
              )
              p_ver = sys.version_info[:2]
  
              # Predict version from py[thon]-x.x in the $VIRTUAL_ENV
              re_m = re.search(r"\bpy(?:thon)?([23])\.(\d+)\b", os.environ["VIRTUAL_ENV"])
              if re_m:
                  predicted_path = Path(str(virtual_env_path).format(*re_m.groups()))
                  if predicted_path.exists():
                      p_ver = re_m.groups()
  
              virtual_env = str(virtual_env_path).format(*p_ver)
  
          warn(
              "Attempting to work in a virtualenv. If you encounter problems, "
              "please install IPython inside the virtualenv."
          )
          import site
          sys.path.insert(0, virtual_env)
          site.addsitedir(virtual_env)
function_tokens: def init_virtualenv ( self ) : if 'VIRTUAL_ENV' not in os . environ : # Not in a virtualenv return elif os . environ [ "VIRTUAL_ENV" ] == "" : warn ( "Virtual env path set to '', please check if this is intended." ) return p = Path ( sys . executable ) p_venv = Path ( os . environ [ "VIRTUAL_ENV" ] ) # fallback venv detection: # stdlib venv may symlink sys.executable, so we can't use realpath. # but others can symlink *to* the venv Python, so we can't just use sys.executable. # So we just check every item in the symlink tree (generally <= 3) paths = [ p ] while p . is_symlink ( ) : p = Path ( os . readlink ( p ) ) paths . append ( p . resolve ( ) ) # In Cygwin paths like "c:\..." and '\cygdrive\c\...' are possible if p_venv . parts [ 1 ] == "cygdrive" : drive_name = p_venv . parts [ 2 ] p_venv = ( drive_name + ":/" ) / Path ( * p_venv . parts [ 3 : ] ) if any ( p_venv == p . parents [ 1 ] for p in paths ) : # Our exe is inside or has access to the virtualenv, don't need to do anything. return if sys . platform == "win32" : virtual_env = str ( Path ( os . environ [ "VIRTUAL_ENV" ] , "Lib" , "site-packages" ) ) else : virtual_env_path = Path ( os . environ [ "VIRTUAL_ENV" ] , "lib" , "python{}.{}" , "site-packages" ) p_ver = sys . version_info [ : 2 ] # Predict version from py[thon]-x.x in the $VIRTUAL_ENV re_m = re . search ( r"\bpy(?:thon)?([23])\.(\d+)\b" , os . environ [ "VIRTUAL_ENV" ] ) if re_m : predicted_path = Path ( str ( virtual_env_path ) . format ( * re_m . groups ( ) ) ) if predicted_path . exists ( ) : p_ver = re_m . groups ( ) virtual_env = str ( virtual_env_path ) . format ( * p_ver ) warn ( "Attempting to work in a virtualenv. If you encounter problems, " "please install IPython inside the virtualenv." ) import site sys . path . insert ( 0 , virtual_env ) site . addsitedir ( virtual_env )