【问题标题】:How do I fix visual studio code path error?如何修复 Visual Studio 代码路径错误?
【发布时间】:2021-05-19 00:31:28
【问题描述】:

所以我在可视代码中遇到了这个问题,当我在项目的同一目录中打开文件时,除非我给他整个目录,否则可视代码无法识别该文件

这是错误的图像:

【问题讨论】:

  • 这不是 VS Code 的问题。这与iconbitmap() 搜索文件系统的方式以及它从哪个目录开始有关。
  • 但它适用于 Pycharm 和 python 启动器
  • 这取决于你的python程序是如何启动的。我很确定 VSCode 不会从当前工作目录开始,除非您将其配置为这样做。
  • @SuperStormer 或者更准确地说,这取决于VS Code启动时当前工作目录是什么。

标签: python visual-studio-code vscode-settings


【解决方案1】:

cwd

指定调试器的当前工作目录,它是代码中使用的任何相对路径的基本文件夹。如果省略,则默认为 ${workspaceFolder}(在 VS Code 中打开的文件夹)。

您可以将其设置为:${fileDirname} - 当前打开文件的目录名(official docs)

【讨论】:

    【解决方案2】:

    尝试这种方式手动设置工作目录:

    /tmp/1/
    /tmp/2/file
    
    import os
    
    print("Current working directory: {0}".format(os.getcwd()))
    
    > Current working directory: /tmp/1
    
    open('file', 'r')
    
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in <module>
    > FileNotFoundError: [Errno 2] No such file or directory: 'file'
    
    os.chdir('/tmp/2')
    
    open('file', 'r')
    
    > <_io.TextIOWrapper name='file' mode='r' encoding='UTF-8'>
    
    

    你也可以设置与运行的python文件相关的目录:

    import os
    print(os.path.dirname(os.path.abspath(__file__)))
    
    python /tmp/1/file.py
    
    > /tmp/1
    

    最好的做法是在全局配置文件中的某处设置应用程序路径

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2019-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      相关资源
      最近更新 更多