是否可以从TCL激活和执行Virtualenv Python?

发布于 2025-01-24 03:28:23 字数 390 浏览 2 评论 0 原文

这是我执行的bash命令:

  source ./virtualenv/Scripts/activate && cd ./deps/scripts && python generate_system_info.py

现在我想在tcl脚本中运行它。我不确定如何实现这一目标。

我知道需要在TCL中进行 exec 。但是,我不确定是否可以使用上面显示的方法从TCL进行Virtualenv的激活。当我在TCL Shell中拨打 exec 时,我也不确定如何集成&

我想实现的目标吗?

TCL外壳来自一个名为Xilinx Vivado的程序。

This is the bash command that I execute:

  source ./virtualenv/Scripts/activate && cd ./deps/scripts && python generate_system_info.py

Now I want to run this from within a TCL script. I am not sure how this can be achieved.

I know that the exec in TCL is required. However, I am not sure if I can do the activate of virtualenv from TCL using the method shown above. I am also not sure how to integrate the && when I make call to exec inside the TCL shell.

Is what I am trying to achieve even possible?

The TCL shell is from a program called Xilinx Vivado.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月寒剑心 2025-01-31 03:28:23

激活脚本(由此模板通过明显的替换)设置了这些环境变量(您需要关心):

  1. virtual_env - 虚拟环境根的路径。
  2. 路径 - 在...很多程序中找到二进制文件!这是脚本的扩展
  3. PS1 - 系统提示。

它还删除 pythonhome 环境变量(如果是设置),并定义一个称为 deactivate 的函数以撤消所有这些。


要在 tclsh 中执行等效词,将Python作为子过程运行时,通常只需要设置前两个变量(并执行此操作)即可。当TCL退出时,清洁会自动发生。 (我们省略配置提示;只有在交互式工作时,您才需要它。)

proc activatePythonVirtualEnvironment {path} {
    global env tcl_platform
    # Allow standard Tcl paths
    set path [file nativename [file normalize $path]]
    set binpath [file nativename [file join [file normalize $path] bin]]
    
    # We can just set this here
    set env(VIRTUAL_ENV) $path

    # Prepend the right form; good thing we have a special global to help
    set env(PATH) $binpath$tcl_platform(pathSeparator)$env(PATH)

    # Eliminate this environment variable if it is set
    unset -nocomplain env(PYTHONHOME)
}

之后,您可以 exec 正常,而事物将只能使用™。

activatePythonVirtualEnvironment ./virtualenv/
cd deps/scripts
exec python generate_system_info.py

The activate script (made from this template by obvious substitutions) sets these environment variables (that you need to care about):

  1. VIRTUAL_ENV — The path of the root of the virtual environment.
  2. PATH — Where binaries are found by... lots of programs! This is extended by the script.
  3. PS1 — The system prompt.

It also removes the PYTHONHOME environment variable (if it was set) and defines a function called deactivate to undo all this.


To do the equivalent in tclsh when running Python as a subprocess, you usually just need to set the first two variables (and do that remove). The clean up happens automatically when Tcl exits. (We omit configuring the prompt; you would only need that if working interactively.)

proc activatePythonVirtualEnvironment {path} {
    global env tcl_platform
    # Allow standard Tcl paths
    set path [file nativename [file normalize $path]]
    set binpath [file nativename [file join [file normalize $path] bin]]
    
    # We can just set this here
    set env(VIRTUAL_ENV) $path

    # Prepend the right form; good thing we have a special global to help
    set env(PATH) $binpath$tcl_platform(pathSeparator)$env(PATH)

    # Eliminate this environment variable if it is set
    unset -nocomplain env(PYTHONHOME)
}

After that, you can exec as normal and things will Just Work™.

activatePythonVirtualEnvironment ./virtualenv/
cd deps/scripts
exec python generate_system_info.py
她如夕阳 2025-01-31 03:28:23

来自 venv 文档:

您不需要特别激活环境;激活
只需将虚拟环境的二进制目录送到您的路径,
这样“ python”调用了虚拟环境的python解释器
而且您可以运行安装脚本而无需完整的
小路。但是,在虚拟环境中安装的所有脚本都应
可以在不激活它的情况下运行,并使用虚拟运行
环境的python自动。

因此,基本上激活只需替换Python脚本的路径(和 $ PATH 环境变量,至少在Linux中)。您只需使用完整/相对路径即可从 virtualenv 目录中运行 porpor python。

如果您在哪里挣扎,则可以激活 virtualenv ,并使用命令来找到它!

From the venv documentation:

You don’t specifically need to activate an environment; activation
just prepends the virtual environment’s binary directory to your path,
so that “python” invokes the virtual environment’s Python interpreter
and you can run installed scripts without having to use their full
path. However, all scripts installed in a virtual environment should
be runnable without activating it, and run with the virtual
environment’s Python automatically.

So basically activate just replaces path to your python script (and $PATH environment variable, at least in linux). You can just run proper python from virtualenv directory using full/relative path.

If you struggle where it is, you can activate virtualenv and use which command to find it out!

夜司空 2025-01-31 03:28:23

如果没有别的,您可以执行这样的整个管道:

exec bash -c {source ./virtualenv/Scripts/activate && cd ./deps/scripts && python generate_system_info.py}

&& 只是说“如果上一个命令成功,请继续执行下一个命令”。因此,您可以在三个单独的行上执行这三个命令。问题是 source 命令。它需要在当前过程中设置环境变量,默认情况下,TCL将启动一个子壳,当该子壳结束时,环境将消失。您可以在TCL中设置本地环境变量,因此也许选项是将 activate shell脚本转换为tcl命令。那你可以做

activate.tcl
cd deps/scripts
exec python generate_system_info.py

If nothing else, you can execute the whole pipeline like this:

exec bash -c {source ./virtualenv/Scripts/activate && cd ./deps/scripts && python generate_system_info.py}

The && just says "if the previous command succeeded, continue on to the next command". Thus, you could do those three commands on three separate lines. The PROBLEM is that source command. It needs to set environment variables in the current process, and by default tcl would launch a subshell, and when that subshell ended the environment would go away. You CAN set local environment variables in tcl, so maybe the option would be to convert that activate shell script into tcl commands. Then you could do

activate.tcl
cd deps/scripts
exec python generate_system_info.py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文