title: Correct add site-packages folder to Sublime Text 3 sys.path
body: <h2>1. Summary</h2>
  
  <p>I don't understand, how I can make, that <a href="https://docs.python.org/3/install/index.html#how-installation-works" rel="nofollow noreferrer"><strong>global site-packages path</strong></a> will add to Sublime Text 3 <code>sys.path</code> in each Sublime Text 3 start.</p>
  
  <hr>
  
  <h2>2. Reason</h2>
  
  <p>I want, that in Sublime Text plugins would be possible use globally installed packages.</p>
  
  <p>See more in <a href="https://stackoverflow.com/q/42652998/5951529"><strong>Global Python packages in Sublime Text plugin development</strong></a> question.</p>
  
  <hr>
  
  <h2>3. Example</h2>
  
  <p>Example part of my plugin:</p>
  
  <pre class="lang-python prettyprint-override"><code>import os
  import sublime_plugin
  import sys
  
  from duckduckgo import query  # noqa
  from pygoogling.googling import GoogleSearch  # noqa
  
  # Any actions
  </code></pre>
  
  <p>Where <code>duckduckgo</code> and <code>pygoogling.googling</code> — Python modules from <code>site-packages</code> folder.</p>
  
  <p><a href="https://github.com/Kristinita/KristinitaLuckyLink/blob/4193ceaa009ef75382787121f6623c7a3db17da2/KristinitaLuckyLink.py" rel="nofollow noreferrer"><strong>Real plugin example</strong></a>.</p>
  
  <hr>
  
  <h2>4. Not helped</h2>
  
  <h3>4.1. Manually add path to console</h3>
  
  <p>I open Sublime Text console → I paste to it:</p>
  
  <pre class="lang-python prettyprint-override"><code>import sys; sys.path.append('C:\Python36\Lib\site-packages')
  </code></pre>
  
  <p>Now:</p>
  
  <pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; sys.path
  ['D:\\Sublime Text Build 3143 x64 For Debug', 'D:\\Sublime Text Build 3143 x64 For Debug\\python3.3.zip', 'D:\\Sublime Text Build 3143 x64 For Debug\\Data\\Lib\\python3.3', 'D:\\Sublime Text Build 3143 x64 For Debug\\Data\\Packages', 'C:\\Python36\\Lib\\site-packages']
  </code></pre>
  
  <p>I restart Sublime Text → I open Sublime Text console:</p>
  
  <pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; import sys; sys.path
  ['D:\\Sublime Text Build 3143 x64 For Debug', 'D:\\Sublime Text Build 3143 x64 For Debug\\python3.3.zip', 'D:\\Sublime Text Build 3143 x64 For Debug\\Data\\Lib\\python3.3', 'D:\\Sublime Text Build 3143 x64 For Debug\\Data\\Packages']
  </code></pre>
  
  <p>Environment variables, added manually, clean after each restart.</p>
  
  <h3>4.2. Using PYTHONPATH</h3>
  
  <p>My <code>PYTHONPATH</code> user variable in interpreter:</p>
  
  <pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; import sys; sys.path
  ['', 'C:\\Python36', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36\\lib\\site-packages']
  </code></pre>
  
  <p>I modify my plugin:</p>
  
  <pre class="lang-python prettyprint-override"><code>import os
  import sublime_plugin
  import sys
  
  sys.path.append((os.environ['PYTHONPATH']))
  
  from duckduckgo import query  # noqa
  from pygoogling.googling import GoogleSearch  # noqa
  
  # Any actions
  </code></pre>
  
  <p>Now:</p>
  
  <pre class="lang-python prettyprint-override"><code>&gt;&gt;&gt; import sys; sys.path
  ['D:\\Sublime Text Build 3143 x64 For Debug', 'D:\\Sublime Text Build 3143 x64 For Debug\\python3.3.zip', 'D:\\Sublime Text Build 3143 x64 For Debug\\Data\\Lib\\python3.3', 'D:\\Sublime Text Build 3143 x64 For Debug\\Data\\Packages', 'C:\\Python36']
  </code></pre>
  
  <p><strong>But</strong> Sublime Text doesn't accept modules from <code>site-packages</code>:</p>
  
  <pre class="lang-python prettyprint-override"><code>reloading plugin KristinitaLuckyLink.KristinitaLuckyLink
  Traceback (most recent call last):
    File "D:\Sublime Text Build 3143 x64 For Debug\sublime_plugin.py", line 109, in reload_plugin
      m = importlib.import_module(modulename)
    File "./python3.3/importlib/__init__.py", line 90, in import_module
    File "&lt;frozen importlib._bootstrap&gt;", line 1584, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 1565, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 1532, in _find_and_load_unlocked
    File "D:\Sublime Text Build 3143 x64 For Debug\sublime_plugin.py", line 915, in load_module
      exec(compile(source, source_path, 'exec'), mod.__dict__)
    File "D:\Sublime Text Build 3143 x64 For Debug\Data\Packages\KristinitaLuckyLink\KristinitaLuckyLink.py", line 40, in &lt;module&gt;
      from duckduckgo import query  # noqa
  ImportError: No module named 'duckduckgo'
  </code></pre>
  
  <h3>4.3. site-packages environment variable</h3>
  
  <p>I modify my plugin as in <a href="https://stackoverflow.com/a/31384782/5951529"><strong>Anthony Perrot answer</strong></a>:</p>
  
  <pre class="lang-python prettyprint-override"><code>import os
  import sublime_plugin
  import sys
  
  python_environment_variable = (os.environ['PYTHONPATH'])
  sys.path.append(python_environment_variable)
  
  site_packages = next(p for p in python_environment_variable if 'site-packages' in p)
  sys.path.append(site_packages)
  
  from duckduckgo import query  # noqa
  from pygoogling.googling import GoogleSearch  # noqa
  
  # Any actions
  </code></pre>
  
  <p>I get <a href="https://docs.python.org/3/library/exceptions.html#StopIteration" rel="nofollow noreferrer"><strong>StopIteration</strong></a> exception in console:</p>
  
  <pre class="lang-python prettyprint-override"><code>reloading plugin KristinitaLuckyLink.KristinitaLuckyLink
  Traceback (most recent call last):
    File "D:\Sublime Text Build 3143 x64 For Debug\sublime_plugin.py", line 109, in reload_plugin
      m = importlib.import_module(modulename)
    File "./python3.3/importlib/__init__.py", line 90, in import_module
    File "&lt;frozen importlib._bootstrap&gt;", line 1584, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 1565, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", line 1532, in _find_and_load_unlocked
    File "D:\Sublime Text Build 3143 x64 For Debug\sublime_plugin.py", line 915, in load_module
      exec(compile(source, source_path, 'exec'), mod.__dict__)
    File "D:\Sublime Text Build 3143 x64 For Debug\Data\Packages\KristinitaLuckyLink\KristinitaLuckyLink.py", line 36, in &lt;module&gt;
      site_packages = next(p for p in python_environment_variable if 'site-packages' in p)
  StopIteration
  </code></pre>
  
  <hr>
  
  <h2>5. Worked, but unexpected</h2>
  
  <h3>5.1. Working example</h3>
  
  <p>I add new environment variable to my operating system, for example:</p>
  
  <pre class="lang-python prettyprint-override"><code>PYTHONPACKAGES=C:\Python36\Lib\site-packages
  </code></pre>
  
  <p>Where:</p>
  
  <ul>
  <li><code>PYTHONPACKAGES</code> — name of variable,</li>
  <li><code>C:\Python36\Lib\site-packages</code> — <a href="https://docs.python.org/3/install/index.html#how-installation-works" rel="nofollow noreferrer"><strong>global site-packages path</strong></a>.</li>
  </ul>
  
  <p><strong>And</strong> modify my plugin:</p>
  
  <pre class="lang-python prettyprint-override"><code>import os
  import sublime_plugin
  import sys
  
  sys.path.append((os.environ['PYTHONPACKAGES']))
  
  from duckduckgo import query  # noqa
  from pygoogling.googling import GoogleSearch  # noqa
  
  # Any actions
  </code></pre>
  
  <p>Plugin will successful works.</p>
  
  <h3>5.2. Reason, why unexpected</h3>
  
  <p>Each user, who will download my plugin, need add <code>PYTHONPACKAGES</code> environment variable for operating system and, possibly, restart operating system.</p>
  
  <p>It would be nice, if would be possible, that users of plugin don't need add environment variables.</p>
  
  <p>Expected behavior: user install plugin → user can work with plugin without additional actions of setting up.</p>
  
  <hr>
  
  <h2>6. Do not offer</h2>
  
  <ol>
  <li>Please, do not offer, that I don't need to use global modules and I need to integrate external modules to plugin folder.</li>
  </ol>