本文介绍了如何让 VS Code 读取 Python 导入而不显示黄色波浪线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经摆弄了一个问题,但一直无法找到解决方案.VS Code 不会识别模块导入,因此在函数下放了一条黄色的波浪线,如下所示:

I've fiddled with a problem, and have not been able to find a solution. VS Code will not recognize module imports, and thus put a yellow squiggly line under the functions, like this:

这些是在每个导入的函数上,但是当主文件被执行时,它呈现和执行得非常好.问题仅在于 VS Code 中代码的可视化.

These are on every function that imports, but it renders and executes perfectly fine when the main file is executed. The issue is solely in the visualization of the code within VS Code.

  • MacOS Mojave 10.14.5
  • VS 代码 1.36.1
  • Python 3.7.3
  • 将 VS Code 源设置为 /usr/bin/python
  • 将 VS Code 源设置为 /usr/local/bin/python3
  • 重新安装 Python
  • 重新安装 Python3
  • 谷歌搜索
  • "python.jediEnabled": 中的 false 更改为 truesettings.json
  • Setting VS Code source to /usr/bin/python
  • Setting VS Code source to /usr/local/bin/python3
  • Reinstalling Python
  • Reinstalling Python3
  • Googling
  • Changing false to true in "python.jediEnabled": in settings.json

为了说明问题,这里有一个简单的应用程序:

To illustrate the problem, here is a simple app:

目录

注意:与动物/目录中的 init.py 相同的错误.

Note: Same error with init.py in the animals/ directory.

from animals.bird import *
from animals.reptile import *

app.py

from __init__ import *
print_bird()
print_reptile()

animals/reptile.py

def print_reptile():
    print("I'm a reptile. ssssssssss!")

动物/鸟.py

def print_bird():
    print("I'm a bird. tweet!")

当运行 python3 app.pypython app.py 时,结果总是预期的文本:


And when running python3 app.py or python app.py the result is always the expected text of:

I'm a bird. tweet!
I'm a reptile. ssssssssss!

推荐答案

几件事情,你不需要 from __init__ 来导入,你可以直接使用 from Animal importbird,reptile 一旦你设置了 __init__.py 文件.

Couple of things, you don't need from __init__ to import, you can just use from animals import bird, reptile once you have the __init__.py file set up.

那么我假设你已经安装了 vs code python 扩展.在这种情况下,在您的项目根目录中设置一个包含 python 项目根目录的 .env 文件(不一定相同).例如.我的 python 代码在项目根目录的 src 下,所以我的 env 文件中有:

Then I'm assuming you have the vs code python extension installed. In which case, in your project root set a .env file containing the python project root (not necessarily the same thing). Eg. my python code is under src in the project root, so I have in my env file:

PYTHONPATH="./src/project/"

然后在您的设置中您可以设置:

Then in your settings you can set :

"python.envFile": "${workspaceFolder}\\<project>.env",

这应该告诉 vs 代码你的python 根"在哪里,所以你的所有路径都是正确的.

That should tell vs code where your "python root" is, so all your paths will be correct.

让我发疯,直到我把它整理好:)

Drove me nuts that until I got this sorted :)

这篇关于如何让 VS Code 读取 Python 导入而不显示黄色波浪线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:30