Visual Studio Code - How to add multiple paths to python path?

前端 未结 7 704
我在风中等你
我在风中等你 2020-12-04 12:40

I am experimenting with Visual Studio Code and so far, it seems great (light, fast, etc).

I am trying to get one of my Python apps running that uses a virtual enviro

相关标签:
7条回答
  • 2020-12-04 12:50

    This worked for me:-

    in your launch.json profile entry, specify a new entry called "env", and set PYTHONPATH yourself.

    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "stopOnEntry": false,
            "request": "launch",
            "pythonPath": "${config.python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ],
            "env": {
                "PYTHONPATH": "/path/a:path/b"
            }
        }
    ]
    
    0 讨论(0)
  • 2020-12-04 12:50

    bash escamotage (works with debugger AND autocomplete); need to install code command in PATH (vsc shell command: install...)

    #!/bin/bash
    
    #
    # vscode python setup
    #
    
    function fvscode {
      # you just want one of this:
      export PYTHONPATH=<your python installation ../bin/python3>
      # you may want many of these:
      export PYTHONPATH=<your lib dir here>:$PYTHONPATH
      # launch vscode
      code 
    }
    alias vscode='fvscode'
    

    the launch VSC by typing 'vscode'.

    0 讨论(0)
  • 2020-12-04 12:51

    I had the same issue, malbs answer doesn't work for me until I change semicolon to a colon,you can find it from ZhijiaCHEN's comments

    "env": { "PYTHONPATH": "/path/to/a:/path/to/b" }
    

    Alternatively, I have a hack way to achieve the same:

    # at the top of project app script:
    import sys
    sys.path.append('/path/to/a')
    sys.path.append('/path/to/b')
    
    0 讨论(0)
  • 2020-12-04 12:52

    According to the environments doc, the places the extension looks for environments include some defaults and also the setting value for python.venvPath in the workspace settings

    eg: "python.venvPath": "~/.virtualenvs"

    This allows you to find several (eg: virtualenvs) as mentioned:

    To select a specific environment, use the Python: Select Interpreter command from the Command Palette

    0 讨论(0)
  • 2020-12-04 13:00

    The Python Extension in VS Code has a setting for python.envFile which specifies the path to a file containing environment variable definitions. By default it is set to:

    "python.envFile": "${workspaceFolder}/.env"
    

    So to add your external libraries to the path, create a file named .env in your workspace folder and add the below line to it if you are using Windows:

    PYTHONPATH="C:\path\to\a;C:\path\to\b"
    

    The advantage of specifying the path here is that both the auto-complete as well as debugging work with this one setting itself. You may need to close and re-open VS Code for the settings to take effect.

    0 讨论(0)
  • 2020-12-04 13:02

    Based on https://github.com/microsoft/vscode-python/issues/12085, I added the following to the settings portion of the workspace config file. I'm using Linux. For Windows, use terminal.integrated.env.windows.

    "terminal.integrated.env.linux": {
        "PYTHONPATH": "addl-path-entry1:addl-path-entry2"
    }
    

    I also added an .env file as described by many posts/comments above.

    Finally, I added the PyLance extension per https://stackoverflow.com/a/64103291/11262633.

    I also reloaded my workspace.

    These two changes allowed me to run Python programs using the debugger and the Run menu. AutoComplete is aware of the added path, and my VSCode linter (was the default linter pylint, now ``pylance```) now works.

    0 讨论(0)
提交回复
热议问题