更正将site-packages文件夹添加到Sublime Text 3 sys.path

时间:2018-01-16 08:25:57

标签: python python-3.x sublimetext3 sublimetext sublime-text-plugin

1。摘要

我不明白,我怎么做,global site-packages path将在每个Sublime Text 3开头添加Sublime Text 3 []

2。原因

我想,在Sublime Text插件中可以使用全局安装的包。

Global Python packages in Sublime Text plugin development问题中查看更多内容。

3。实施例

我插件的示例部分:

sys.path

import os import sublime_plugin import sys from duckduckgo import query # noqa from pygoogling.googling import GoogleSearch # noqa # Any actions duckduckgo - pygoogling.googling文件夹中的Python模块。

Real plugin example

4。没帮忙

4.1。手动添加控制台路径

我打开Sublime Text控制台→我粘贴到它:

site-packages

现在:

import sys; sys.path.append('C:\Python36\Lib\site-packages')

我重新启动Sublime Text→我打开Sublime Text控制台:

>>> 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']

环境变量,每次重启后手动添加,清理。

4.2。使用PYTHONPATH

解释器中的>>> 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'] 用户变量:

PYTHONPATH

我修改了我的插件:

>>> import sys; sys.path
['', 'C:\\Python36', 'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36\\lib\\site-packages']

现在:

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

Sublime Text不接受来自>>> 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'] 的模块:

site-packages

4.3。 site-packages环境变量

我在Anthony Perrot answer中修改我的插件:

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 "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", 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 <module>
    from duckduckgo import query  # noqa
ImportError: No module named 'duckduckgo'

我在控制台中遇到StopIteration异常:

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

5。工作,但意外

5.1。工作示例

我向操作系统添加了新的环境变量,例如:

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 "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", 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 <module>
    site_packages = next(p for p in python_environment_variable if 'site-packages' in p)
StopIteration

其中:

修改我的插件:

C:\Python36\Lib\site-packages

插件将成功运作。

5.2。原因,为什么意外

每个将下载我的插件的用户都需要为操作系统添加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 环境变量,并可能重新启动操作系统。

如果可能的话,插件的用户不需要添加环境变量会很好。

预期行为:用户安装插件→用户可以使用插件而无需其他设置操作。

6。不提供

  1. 请不要提供,我不需要使用全局模块,我需要将外部模块集成到插件文件夹中。

1 个答案:

答案 0 :(得分:0)

您也可以使用以下方法执行此操作:

import site

# if outside of a sublime text plugin class
all_views = sublime.active_window().views()
# or if inside use the 'view' variable, skip to line 9 and change 
# all_views[0].settings to view.settings

if len(all_views) > 0:
    external_python_path = all_views[0].settings().get("external_python_path")

    sp = site.getsitepackages(external_python_path)
    sp = [x for x in sp if "site-packages" in x.lower()]
    sys.path.append(sp)

然后在您的Preferences.sublime-settings文件中,添加像

这样的key:value
{
    "somekey": "somevalue",
    ...,
    "external_python_path": "path_to_python folder excluding the python.exe"
}