Visual Studio Code - 如何向 python 路径添加多个路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41471578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio Code - How to add multiple paths to python path?
提问by mike01010
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. 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
,到目前为止我正在添加外部库。问题是,当我调试时,它失败了,因为它没有找到python.autoComplete.extraPaths
.
Is there another setting that must be used for this?
是否必须为此使用其他设置?
Thanks
谢谢
采纳答案by malbs
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"
}
}
]
回答by WebDev
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:
VS Code 中的 Python 扩展有一个设置,python.envFile
用于指定包含环境变量定义的文件的路径。默认情况下,它设置为:
"python.envFile": "${workspaceFolder}/.env"
So to add your external libraries to the path, create a file named .envin 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 才能使设置生效。
回答by Haifeng Zhang
I had the same issue, malbsanswer 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')
回答by tebanep
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
回答by Kabu
bash escamotage (works with debugger AND autocomplete); need to install code command in PATH (vsc shell command: install...)
bash escamotage(适用于调试器和自动完成);需要在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。
回答by Efren
According to the environments doc, the places the extension looks for environmentsinclude some defaults and also the setting value for python.venvPath
in the workspace settings
根据环境文档,扩展查找环境的位置包括一些默认值以及python.venvPath
工作区中的设置值。settings
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:从命令面板中选择解释器命令