MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / activate_shell

Method activate_shell

mod/project/python/pyenv_tool.py:281–320  ·  view source on GitHub ↗

获取激活环境的shell命令, venv和conda执行对应的脚本即可,

(self)

Source from the content-addressed store, hash-verified

279 return self._site_pkg_name2bin("gunicorn")
280
281 def activate_shell(self) -> Optional[str]:
282 """获取激活环境的shell命令, venv和conda执行对应的脚本即可,"""
283 if self.env_type == "venv":
284 res_sh = self.activate_sh
285 elif self.env_type == "conda":
286 init_sh = "{}/etc/profile.d/conda.sh".format(os.path.dirname(os.path.dirname(self.conda_path)))
287 remove_panel_py = (
288 "bt_env_deactivate > /dev/null 2>&1 \n"
289 "export PATH=$(echo $PATH | tr ':' '\\n' | grep -v '{}' | tr '\\n' ':') \n"
290 "export PATH=$(echo $PATH | tr ':' '\\n' | grep -v '{}' | tr '\\n' ':') \n".format(
291 python_manager_path(),
292 pyenv_path()
293 )
294 )
295 return "{}{} init >/dev/null 2>&1\nsource {}\n{}".format(
296 remove_panel_py, self.conda_path, init_sh, self.activate_sh
297 )
298 elif self.env_type == "system":
299 if any((self.bin_path.startswith(i) for i in _SYS_BIN_PATH)):
300 res_sh = 'export PATH="{}":"$PATH"'.format(os.path.dirname(self.bin_path))
301 else:
302 res_sh = "export _BT_PROJECT_ENV={} && source {} NULL \n".format(
303 os.path.dirname(os.path.dirname(self.bin_path)),
304 self._BT_PROJECT_ENV_SHELL
305 )
306 else:
307 return ""
308
309 if self.env_type != "conda" and len(CondaDetector().find_conda_executable()) > 0:
310 res_sh = """# 彻底停用 Conda 环境并清除所有 Conda 相关路径
311conda deactivate
312unset CONDA_PREFIX
313unset CONDA_EXE
314unset _CE_CONDA
315unset _CE_M
316export PATH=$(echo $PATH | tr ':' '\\n' | grep -v 'conda' | tr '\\n' ':')
317export LD_LIBRARY_PATH=$(echo $LD_LIBRARY_PATH | tr ':' '\\n' | grep -v 'conda' | tr '\\n' ':')
318""" + res_sh
319
320 return res_sh + "\n"
321
322 def _install_pip(self, call_log:Optional[Callable[[str], None]] = None) -> Optional[str]:
323 if not callable(call_log):

Callers 12

_install_pipMethod · 0.95
pip_installMethod · 0.95
pip_uninstallMethod · 0.95
pip_listMethod · 0.95
exec_shellMethod · 0.95
shell_envMethod · 0.80
set_profile_envMethod · 0.80
get_active_shellMethod · 0.80

Calls 6

python_manager_pathFunction · 0.85
pyenv_pathFunction · 0.85
CondaDetectorClass · 0.85
dirnameMethod · 0.80
find_conda_executableMethod · 0.80
formatMethod · 0.45

Tested by

no test coverage detected