简体   繁体   English

Visual Studio Code - 如何将多个路径添加到 python 路径?

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

I am experimenting with Visual Studio Code and so far, it seems great (light, fast, etc).我正在尝试使用 Visual Studio Code,到目前为止,它看起来很棒(轻便、快速等)。

I am trying to get one of my Python apps running that uses a virtual environment, but also uses libraries that are not in the site-package of my virtual environment.我正在尝试运行我的 Python 应用程序之一,该应用程序使用虚拟环境,但也使用不在我的虚拟环境的站点包中的库。

I know that in settings.json , I can specify a python.pythonPath setting, which I have done and is pointing to a virtual environment.我知道在settings.json ,我可以指定一个python.pythonPath设置,我已经完成并指向一个虚拟环境。

I also know that I can add additional paths to python.autoComplete.extraPaths , where thus far I am adding the external libraries.我也知道我可以向python.autoComplete.extraPaths添加额外的路径,到目前为止我正在添加外部库。 The problem is, when I am debugging, it's failing because it's not finding the libraries specified in python.autoComplete.extraPaths .问题是,当我调试时,它失败了,因为它没有找到python.autoComplete.extraPaths中指定的库。

Is there another setting that must be used for this?是否必须为此使用其他设置?

Thanks谢谢

This worked for me:-这对我有用:-

in your launch.json profile entry, specify a new entry called "env", and set PYTHONPATH yourself.在您的 launch.json 配置文件条目中,指定一个名为“env”的新条目,并自己设置 PYTHONPATH。

"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"
        }
    }
]

The Python Extension in VS Code has a setting for python.envFile which specifies the path to a file containing environment variable definitions (Refer to: https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file ). VS Code 中的 Python 扩展有一个python.envFile设置,它指定包含环境变量定义的文件的路径(参考: https : //code.visualstudio.com/docs/python/environments#_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:因此,要将外部库添加到路径中,在工作区文件夹中创建一个名为.env的文件,如果您使用的是 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.您可能需要关闭并重新打开 VS Code 才能使设置生效。

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我遇到了同样的问题,直到我将分号更改为冒号后, malbs 的回答才对我有用您可以从 ShijiaCHEN 的评论中找到它

"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')

You could add a .pth file to your virtualenv's site-packages directory.您可以将 .pth 文件添加到您的 virtualenv 的 site-packages 目录中。

This file should have an absotute path per line, for each module or package to be included in the PYTHONPATH.对于要包含在 PYTHONPATH 中的每个模块或包,此文件的每行应该有一个绝对路径。

https://docs.python.org/2.7/install/index.html#modifying-python-s-search-path https://docs.python.org/2.7/install/index.html#modifying-python-s-search-path

Based on https://github.com/microsoft/vscode-python/issues/12085 , I added the following to the settings portion of the workspace config file.基于https://github.com/microsoft/vscode-python/issues/12085 ,我将以下内容添加到工作区配置文件的settings部分。 I'm using Linux.我正在使用 Linux。 For Windows, use terminal.integrated.env.windows .对于 Windows,请使用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.我还添加了一个.env文件,如上面许多帖子/评论所述。

Finally, I added the PyLance extension per https://stackoverflow.com/a/64103291/11262633 .最后,我根据https://stackoverflow.com/a/64103291/11262633添加了PyLance扩展。

I also reloaded my workspace.我还重新加载了我的工作区。

These two changes allowed me to run Python programs using the debugger and the Run menu.这两个更改使我能够使用调试器和运行菜单运行 Python 程序。 AutoComplete is aware of the added path, and my VSCode linter (was the default linter pylint , now ``pylance```) now works. AutoComplete 知道添加的路径,并且我的 VSCode linter(是默认的pylint ,现在是 ``pylance```)现在可以工作了。

I made it work through adding "python.analysis.extraPaths" when using Pylance and IntelliCode.我在使用 Pylance 和 IntelliCode 时通过添加“python.analysis.extraPaths”使其工作。

In 2022, the configuration is as file .vscode/settings.json :在 2022 年,配置文件为.vscode/settings.json

{
    "python.analysis.extraPaths": ["C:/Program Files/obs-studio/data/obs-scripting/64bit"],
    "terminal.integrated.env.windows": {
        "PYTHONPATH": "C:/Program Files/obs-studio/data/obs-scripting/64bit;${env:PYTHONPATH}",
        "PATH": "C:/Program Files/obs-studio/data/obs-scripting/64bit;${env:PATH}"
    }
}

bash escamotage (works with debugger AND autocomplete); bash escamotage(适用于调试器和自动完成); need to install code command in PATH (vsc shell command: install...)需要在PATH中安装代码命令(vsc shell命令:安装...)

#!/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'.通过键入“vscode”启动 VSC。

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根据环境文档扩展查找环境的位置包括一些默认值以及工作区settings python.venvPath的设置值

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

This allows you to find several (eg: virtualenvs) as mentioned:这允许您找到提到的几个(例如:virtualenvs):

To select a specific environment, use the Python: Select Interpreter command from the Command Palette要选择特定环境,请使用 Python:从命令面板中选择解释器命令

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

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