【问题标题】:error while installing python extensions " can't open file 'directory + filename': [Errno 2] No such file or directory "安装python扩展时出错“无法打开文件'目录+文件名':[Errno 2]没有这样的文件或目录”
【发布时间】:2020-07-01 21:25:11
【问题描述】:

我尝试在 vs 代码中安装 mypy 扩展,但它一直在下面显示此错误

C:\Users\Yourusername\Dev\django_project_boilerplate\env\Scripts\python.exe: can't open file 'c:UsersYourusername.vscodeextensionsms-python.python-2020.6.89148pythonFilespyvsc-run-isolated.py': [Errno 2] No such file or directory
(env)

然后我意识到终端正在从下面命令中的文件位置删除反斜杠,因为它是一个特殊字符

c:/Users/Yourusername/Dev/django_project_boilerplate/env/Scripts/python.exe c:\Users\Yourusername\.vscode\extensions\ms-python.python-2020.6.89148\pythonFiles\pyvsc-run-isolated.py pip install -U mypy 

【问题讨论】:

    标签: visual-studio-code terminal python-extensions


    【解决方案1】:

    尝试在 VS Code 中使用 Python 交互模式时,单击弹出窗口中的“安装 ipykernel”时出现相同的错误。

    我的问题是我在 Windows 上运行 VS Code 时将 git bash 作为我的默认 shell,因此文件路径没有得到正确处理。

    我通过更改终端的默认 shell 解决了这个问题:

    1. 类型:ctrl + shift + p
    2. 在下拉列表中,选择Terminal: Select Default Shell
    3. 在随后的下拉列表中,选择Command Prompt C:\...
    4. 再次尝试运行交互模式(例如,在一行代码上方添加 # %%,然后单击 Run Cell
    5. 现在点击右下角弹出窗口中的“安装 ipykernel”。

    现在从 cmd shell 执行安装没有任何问题,交互模式运行良好。

    【讨论】:

    • 我不敢相信这没有更多的赞成票。 Windows + Git bash 感觉就像每个人都会使用的东西,如果他们使用 Windows
    • 是的,当我点击 linter,格式化程序弹出窗口时,我总是遇到这个问题
    • 当我尝试重命名变量并被提示安装“rope”python库时遇到了问题,此时我收到了有关目录的错误。我不理解上面的第 4 步和第 5 步,所以在第 3 步之后我只是重新启动了 Visual Studio 代码,然后只是尝试再次重命名变量,此时使用 Visual Studio 代码终端中的命令提示符成功安装了绳索,而不是的 git bash。
    • @hBrent 好电话。第 4 步和第 5 步是针对 ipython 的,而第 1 步到第 3 步与其他 python 库和框架相关。
    【解决方案2】:

    我所做的是添加一个额外的反斜杠,它工作正常:

    c:/Users/Yourusername/Dev/django_project_boilerplate/env/Scripts/python.exe c:\\Users\\Yourusername\\.vscode\\extensions\\ms-python.python-2020.6.89148\\pythonFiles\\pyvsc-run-isolated.py pip install -U mypy 
    

    【讨论】:

    • 或者把反斜杠改成正斜杠
    【解决方案3】:

    因为我也在 Windows 中使用 git bash 作为我的默认终端,所以我使用了以下命令:

    python /c/Users/uname/.vscode/extensions/ms-python.python-2021.3.680753044/pythonFiles/pyvsc-run-isolated.py pip install -U black --user
    

    而我之前遇到过这样的错误:

    C:/Users/uname/AppData/Local/Programs/Python/Python38/python.exe c:\Users\uname\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\pyvsc-run-isolated.py pip install -U black --user
    C:\Users\uname\AppData\Local\Programs\Python\Python38\python.exe: can't open file 'c:Usersigour.vscodeextensionsms-python.python-2021.3.680753044pythonFilespyvsc-run-isolated.py': [Errno 2] 
    No such file or directory
    

    如您所见,这是由于 git bash 处理文件位置的方式不同,我已修复此问题,但更改后效果很好。

    老实说,我喜欢看到 windows 开始遵循与其他 Linux 系统相同的目录结构(希望有一天这会成为现实)。

    【讨论】:

      【解决方案4】:

      将你的 vs 代码的终端改回默认 Shell > 命令提示符 C:\Windows...

      该错误是由于在 VS Code 上使用 git bash 作为默认终端时 PATH 冲突造成的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-05
        • 2019-10-24
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多