简体   繁体   English

在 Windows 的 VS 代码中设置 Python 路径

[英]Setting Python Path in VS Code in Windows

I'm trying to import some local modules in Python while using VS Code as my editor.我正在尝试在 Python 中导入一些本地模块,同时使用 VS Code 作为我的编辑器。 Something like this:像这样的东西:

import folder1.subfolder2.program3

We'll say "folder1" is located at 'C:/folder1'我们会说“folder1”位于“C:/folder1”

VS Code does not recognize this and I keep getting failed import statements. VS Code 无法识别这一点,我不断收到失败的导入语句。 It instead thinks 'C:/folder6' (for example) is the path.相反,它认为“C:/folder6”(例如)是路径。 How can I change the Python Path in VS Code?如何更改 VS Code 中的 Python 路径?

I tried changing the PythonPath in Environment Variables and that didn't fix the problem.我尝试更改环境变量中的 PythonPath 并没有解决问题。 I know there is a launch.json file in VS Code I can create by debugging.我知道我可以通过调试创建的 VS Code 中有一个 launch.json 文件。 Can I set the PythonPath there and how would I do it?我可以在那里设置 PythonPath 吗?我该怎么做?

Edit: Some updates.编辑:一些更新。 I can manually add a PythonPath to a file like this:我可以手动将 PythonPath 添加到这样的文件中:

os.environ['PYTHONPATH'] += os.pathsep + "C:\\folder1\\etc"

This is fine for running 1 file, but I often have dozens of interconnected files and don't want to add that line to every single one.这对于运行 1 个文件来说很好,但我经常有几十个相互连接的文件,并且不想将该行添加到每个文件中。

I also tried adding a launch.json:我还尝试添加一个launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal", 
            "env": {
                "PYTHONPATH": "C://folder1//etc"
            }
        }
    ]
}

That doesn't seem to be changing anything, though, when I run a.py file from the command line.但是,当我从命令行运行 a.py 文件时,这似乎并没有改变任何东西。

You need to tell the Python extension you want it to look for code outside of your current workspace.您需要告诉 Python 扩展您希望它在当前工作空间之外查找代码。 You can either add a "python.autoComplete.extraPaths" setting or create a .env file and set your PYTHONPATH there.您可以添加"python.autoComplete.extraPaths"设置或创建.env文件并在那里设置PYTHONPATH

I could be wrong here, but on vs code there is a python button on the bottom left that allows me to change the python path.我在这里可能是错的,但是在 vs 代码上,左下角有一个 python 按钮,允许我更改 python 路径。 The button says python3.8.1... I only code python, so you may have to have a.py file open or something like that.按钮显示 python3.8.1... 我只编码 python,所以你可能必须打开一个 .py 文件或类似的东西。

VS Code only search on python path that is in status bar (PYTHON_PATH/lib/*) and directory of file that you are working not anywhere else (I couldn't find any setting to change this path). VS Code仅搜索状态栏 (PYTHON_PATH/lib/*) 中的 python 路径和您不在其他任何地方工作的文件目录(我找不到任何设置来更改此路径)。 VS Code Statusbar VS 代码状态栏

(So I recommend to put your package in PYTHON_PATH/lib) (所以我建议把你的 package 放在 PYTHON_PATH/lib 中)

Also this thing that you're saying (folder1.folder2/mypackage) is only possible if you put __init__.py in each of directories此外,您所说的(folder1.folder2/mypackage)只有在您将__init__.py放在每个目录中时才有可能

if you put your folder in directory of your file, tree of your working directory should be something like this:如果您将文件夹放在文件目录中,则工作目录的树应该是这样的:

cwd
|____ __init__.py
|____ folder1
      |____ __init__.py
      |____ subfolder2
            |____ __init__.py
            |____ program1.py

In this situation I think I would do this:在这种情况下,我想我会这样做:

You can copy C:/folder1/folder2/mymodule.py to your file_path, then try to import it with import mymodule .您可以将C:/folder1/folder2/mymodule.py复制到您的 file_path,然后尝试使用import mymodule导入它。 See code below:请参见下面的代码:

import rx7
rx7.files.copy('C:/folder1/folder2/mymodule.py', 'mymodule.py')
import mymodule

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

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