gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:36:13 27 4
gpt4 key购买 nike

1。总结

我不明白,我该怎么做global site-packages path将在每个 Sublime Text 3 启动时添加到 Sublime Text 3 sys.path


2。原因

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

查看更多信息 Global Python packages in Sublime Text plugin development问题。


3。例子

我的插件的示例部分:

import os
import sublime_plugin
import sys

from duckduckgo import query # noqa
from pygoogling.googling import GoogleSearch # noqa

# Any actions

duckduckgopygoogling.googling - site-packages 文件夹中的 Python 模块。

Real plugin example .


4。没有帮助

4.1。手动添加控制台路径

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

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

现在:

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

我重新启动 Sublime Text → 我打开 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']

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

4.2。使用 PYTHONPATH

我的 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

现在:

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

但是 Sublime Text 不接受来自site-packages 的模块:

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'

4.3。站点包环境变量

我按照 Anthony Perrot answer 修改我的插件:

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

我得到 StopIteration控制台异常:

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

5。有效,但出乎意料

5.1。工作示例

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

PYTHONPACKAGES=C:\Python36\Lib\site-packages

地点:

修改我的插件:

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

插件将成功运行。

5.2。原因,为什么出乎意料

每个要下载我的插件的用户都需要为操作系统添加 PYTHONPACKAGES 环境变量,并且可能需要重新启动操作系统。

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

预期行为:用户安装插件 → 用户无需额外的设置操作即可使用插件。


6。不提供

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

最佳答案

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

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"
}

关于python - 将 site-packages 文件夹正确添加到 Sublime Text 3 sys.path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48276861/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com