1 # SPDX-FileCopyrightText: 2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 # ----------------------------------------------------------
6 # Author: Stephen Leger (s-leger)
8 # ----------------------------------------------------------
14 PYPATH
= sys
.executable
#bpy.app.binary_path_python
23 def _ensure_user_site_package():
27 site_package
= site
.getusersitepackages()
28 if not os
.path
.exists(site_package
):
29 site_package
= bpy
.utils
.user_resource('SCRIPTS', path
="site_package", create
=True)
30 site
.addsitedir(site_package
)
31 if site_package
not in sys
.path
:
32 sys
.path
.append(site_package
)
35 def _ensure_user_site_package():
39 site_package = site.getusersitepackages()
40 if os.path.exists(site_package):
41 if site_package not in sys.path:
42 sys.path.append(site_package)
44 site_package = bpy.utils.user_resource('SCRIPTS', "site_package", create=True)
45 site.addsitedir(site_package)
47 def _cmd(self
, action
, options
, module
):
48 if options
is not None and "--user" in options
:
49 self
._ensure
_user
_site
_package
()
51 cmd
= [PYPATH
, "-m", "pip", action
]
53 if options
is not None:
54 cmd
.extend(options
.split(" "))
59 def _popen(self
, cmd
):
60 popen
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
, universal_newlines
=True)
61 for stdout_line
in iter(popen
.stdout
.readline
, ""):
69 for line
in self
._popen
(cmd
):
75 if "Successfully" in line
:
88 self
._run
([PYPATH
, "-m", "ensurepip", "--default-pip"])
92 return Pip()._cmd
("install", "--upgrade", "pip")
95 def uninstall(module
, options
=None):
97 :param module: string module name with requirements see:[1]
98 :param options: string command line options see:[2]
99 :return: True on uninstall, False if already removed, raise on Error
100 [1] https://pip.pypa.io/en/stable/reference/pip_install/#id29
101 [2] https://pip.pypa.io/en/stable/reference/pip_install/#id47
103 if options
is None or options
.strip() == "":
106 return Pip()._cmd
("uninstall", options
, module
)
109 def install(module
, options
=None):
111 :param module: string module name with requirements see:[1]
112 :param options: string command line options see:[2]
113 :return: True on install, False if already there, raise on Error
114 [1] https://pip.pypa.io/en/stable/reference/pip_install/#id29
115 [2] https://pip.pypa.io/en/stable/reference/pip_install/#id47
117 if options
is None or options
.strip() == "":
118 # store in user writable directory, use wheel, without deps
119 options
= "--user --only-binary all --no-deps"
120 return Pip()._cmd
("install", options
, module
)
123 def blender_version():
125 :return: blender version tuple
127 return bpy
.app
.version
130 def python_version():
132 :return: python version object
135 # version.major, version.minor, version.micro
136 return sys
.version_info