简体   繁体   English

Windows、Python、VENV 和 Sublime Text 构建系统:激活 VENV 时出现路径错误

[英]Windows, Python, VENV, and Sublime Text Build systems: Path Error When Activating VENV

Okay, I've looked through a bunch of python venv with sublime text questions, and I'm pretty sure this issue is due to the specifics of venv on windows.好的,我已经查看了一堆带有崇高文本问题的 python venv,我很确定这个问题是由于 windows 上的 venv 的细节造成的。

I'm on Windows, using Python 3.10, and the virtualenv package to manage my virtual environments.我在 Windows 上,使用 Python 3.10 和 virtualenv package 来管理我的虚拟环境。

I'm trying to set up a custom build in a Sublime project file that does the following:我正在尝试在 Sublime 项目文件中设置自定义构建,该文件执行以下操作:

  • activate the venv for the local project为本地项目激活 venv
  • echos the VIRTUAL_ENV to the shell to show the correct venv has been activated将 VIRTUAL_ENV 回显到 shell 以显示正确的 venv 已激活

I've been having issues with getting both into one build command, so here's the current array with the two steps as seperate builds:我一直在将两者都放入一个构建命令时遇到问题,所以这是当前数组,其中两个步骤分别构建:

    "build_systems":
    [
        {
            "name": "activate",
            "cmd": "$project_path/venv/Scripts/activate.bat",
            "shell": true
        },
        {
            "name": "Current VENV?",
            "cmd": "echo %VIRTUAL_ENV%",
            "shell": true
        },
    ]

Currently, when I run the activate build, I get the following:目前,当我运行激活构建时,我得到以下信息:

The system cannot find the path specified.
[Finished in 80ms with exit code 1]
[cmd: C:\Users\kreeh\Repos\project/venv/Scripts/activate.bat]
[dir: C:\Users\kreeh\Repos\project]

If I run C:\Users\kreeh\Repos\project/venv/Scripts/activate.bat in a separate cmd.exe window, it correctly activates the venv, and when I print the %VIRTUAL_ENV% var, it correctly returns the venv location. If I run C:\Users\kreeh\Repos\project/venv/Scripts/activate.bat in a separate cmd.exe window, it correctly activates the venv, and when I print the %VIRTUAL_ENV% var, it correctly returns the venv地点。

C:\Users\kreeh>C:\Users\kreeh\Repos\project/venv/Scripts/activate.bat

(venv) C:\Users\kreeh>echo %VIRTUAL_ENV%
C:\Users\kreeh\Repos\project\venv

I assume this is an issue with how the Sublime Text build system is handling the windows path formatting.我认为这是 Sublime Text 构建系统如何处理 windows 路径格式的问题。 If I try and utilize the working directory param, it doesn't work, because the cmd.exe doesn't do relative paths nicely.如果我尝试使用工作目录参数,它不起作用,因为 cmd.exe 不能很好地执行相对路径。

        {
            "name": "activate",
            "cmd": "venv/Scripts/activate.bat",
            "working_dir": "${project_path}",
            "shell": true
        },

returns返回

'venv' is not recognized as an internal or external command, operable program or batch file. 'venv' 不是内部或外部命令、可运行程序或批处理文件。 [Finished in 60ms] [60ms 完成]

So, is there a way to have the Sublime Build system handle the windows path correctly, or is there a way to use a relative path in the cmd?那么,有没有办法让 Sublime Build 系统正确处理 windows 路径,或者有没有办法在 cmd 中使用相对路径?

@MattDMo pointed out the flaw in my logic, which is that the venv activation doesn't actually do anything in and of itself -- it changes the python interpreter used for future commands in that prompt window. @MattDMo 指出了我的逻辑中的缺陷,即 venv 激活本身实际上并没有做任何事情——它改变了 python 解释器,用于该提示 window 中的未来命令。 As such, it's kind of a useless thing for a build system.因此,对于构建系统来说,这是一种无用的东西。 Instead, I added a venv-specific pip install and build commands so far.相反,到目前为止,我添加了一个特定于 venv 的 pip 安装和构建命令。

{
    "build_systems":
    [
        {
            "name": "pip install",
            "cmd": "${project_path}/venv/Scripts/pip3.exe install -r requirements.txt ",
            "working_dir": "${project_path}",
            "shell": true
        },
        {
            "name": "VENV Build",
            "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
            "selector": "source.python",
            "cmd": "${project_path}/venv/Scripts/python.exe $file",
            "shell": true,
        },

    ],
    "folders":
    [
        {
            "follow_symlinks": true,
            "path": "."
        }
    ],

One thing to note, as a Windows user, I had to add the follow_symlinks=true to the folders list -- this is because the way venv works on windows (or at least on MY install), there's not an actual python.exe in that venv folder, it's a symlink.需要注意的一点是,作为 Windows 用户,我必须将follow_symlinks=true添加到文件夹列表中——这是因为 venv 在 windows 上的工作方式(或至少在我的安装上),没有实际的 Z2DFCZ6EEEB4343BDDB755557EE6那个venv文件夹,它是一个符号链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM