4

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:

squigglies enter image description here

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.

My Versions

  • MacOS Mojave 10.14.5
  • VS Code 1.36.1
  • Python 3.7.3

I've Tried

  • 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

My Structure:

To illustrate the problem, here is a simple app:

Directory

folder structure

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

init.py

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

animals/bird.py

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

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!
halfer
  • 19,824
  • 17
  • 99
  • 186
bananabrann
  • 556
  • 7
  • 26

3 Answers3

4

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.

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",

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 :)

michjnich
  • 2,796
  • 3
  • 15
  • 31
  • Oh wow, never heard of an .env file or `python.envFile` in settings.json. I'm having trouble Googling with .env files. Should the the PYTHONPATH go to the directory that contains the main .py file, or should it reference the .py itself? _(e.g., `./src/` vs `./src/app.py`)_ What is the `.env` referencing to? Is that the environment in home or is that within the project directory? Thank you so much! I feel like I'm missing some chunk from what Python is supposed to be doing. – bananabrann Aug 05 '19 at 14:38
  • 1
    I'm using one in my django project, which is under /src/. So in the top dir I specify the python path as ./src/, so all imports become relative to that.Works nicely, but took me a while and a lot of googling to find that solution! – michjnich Aug 05 '19 at 14:41
  • I looked at VS Code docs on `python.envFile`. I didn't understand `${workspaceFolder}` and `\\.env` in your answer. I have it set to `"python.envFile": "${workspaceFolder}/src/.env"` now. Thanks! Sorry, but I'm still confused on the PYTHONPATH in `.env` file... For context: I'm not using Django or any frameworks and do not have a special environment anywhere. I also have `"python.pythonPath": "/usr/local/bin/python3"` in settings.json. Should I have this? What is the .env's `PYTHONPATH` supposed to be pointed at in the project root directory? – bananabrann Aug 05 '19 at 14:58
  • 1
    Here's some more info on the .env file for vs code: https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file – michjnich Aug 05 '19 at 15:15
  • Thank you! That .env file makes sense now. Unfortunately, now the program will not even run. Adding `from __init__ import *` back into app.py allows the app to run again, so I do not think my .env and settings.json adjustments are changing things. I'm going to back-up and look at the actual installations of Python. Do you have any ideas what could be happening? – bananabrann Aug 05 '19 at 15:34
  • Odd, I'd have to see your file structure, __init__.py file and the files that you're importing in to see what the problem there was though ... – michjnich Aug 05 '19 at 17:04
  • The file structure and contents are already in the post, and the __init__.py is empty.. – bananabrann Aug 05 '19 at 21:54
  • I have my models in seperate files too. In the `__init.py__` file (in the same dir as the model files themselves. So, eg. I have `model1` and `model2` in app_name. `__init.py__` says `from app_name.models.model1 import Model1` and `from app_name.models.model2 import Model2`. I can then use just a standard `from app_name.models import Model1, Model2` wherever I like then. – michjnich Aug 06 '19 at 07:52
  • Strange, doing it like that runs the program, and even got rid of red squigglies, but the yellow suigglies still exist. Would a problem in my Bash settings cause a linting issue in IDEs and VS Code? – bananabrann Aug 06 '19 at 21:24
  • Not sure what your yellow squigglies are indicating. I do still get red ones under the "from" in the `__init.py__` file. This is simply because I am importing things in that file that are then not referenced in the same file. I don't actually think there's anything to do about that tbh. I do configure vs code to only show things under the "problem" tab for open files though, and since `__init.py__` is basically never open once I've set it up this doesn't bother me. – michjnich Aug 07 '19 at 07:30
  • Just wondering if the issue is resolved and how? I am getting yellow squiggles line issue same as described in above. @michjnich: file name might be wrong in your case. Normally it looks like (`__init__.py`) instead of (`__init.py__`) where file extension is changed from .py to .py__ in your comment. – Khokhar Jun 28 '21 at 15:49
0

You might have created two venv at the root of your directory. For example in {Python_Projects} root folder you have created test1_venv and test2_venv. You need to delete one of them.

{Python_Projects} root folder you need to create .vscode folder and in which create settings.json. In this json file you need to declare path to your venv python binary. {"python.pythonPath": "test_env/bin/python"}

0

this is the solution that I found. in .vscode/settings.json

{
     "python.analysis.extraPaths": ["${workspaceRoot}/path/to/file"]
}

note that you could use wildcards like * to add all files in a certain directory like pkgname/utils/*.

Hyperx837
  • 773
  • 5
  • 13