Python Extensions cannot see installed packages in DevContainer IDE - python

I am trying to create a basic Development Container to use with VS Code.
I've been through a few iterations no of versions but keep coming up against the same issue, my VS Code extensions cannot seem to see what packages are installed in my venv.
Files in my workspace:
.devcontainer/devcontainer.json
{
"name": "Existing Dockerfile",
"context": "..",
"dockerFile": "../Dockerfile"
}
venv/ containing pip installed pandas
Dockerfile:
FROM python:3.9
WORKDIR .
COPY my_file.py .
my_file.py
import sys
import pandas
print(sys.path)
Output of sys.path incase relevant is ['/workspaces/yt', '/usr/local/lib/python39.zip', '/usr/local/lib/python3.9', '/usr/local/lib/python3.9/lib-dynload', '/workspaces/yt/venv/lib/python3.9/site-packages']
The code executes fine when ran but in VS Code the linting tools raise an error that pandas is not accessed.
Any help would be greatly appreciated.

The most likely reason is that the environment in which the pandas library was installed is not the same as the environment you are currently using.
Solution
Use Ctrl+Shift+P to open the command palette and search for Python:Select Interpreter to select a correct interpreter.
If the error is still not resolved, go ahead and try the following methods:
Add configuration in settings.json to point to the pandas library.
// Just an example, please modify it to your own path
"python.analysis.extraPaths": [
// Path to pandas library
"C:\\WorkSpace\\PyTest0802\\.venv\\Lib\\site-packages"
],
The most simple and rude method -- cancel this type of error (use with caution, this method will cause the prompt message that there is such an error to not be displayed)
Add the following configuration to the settings.json file
"python.analysis.diagnosticSeverityOverrides": {
"reportMissingModuleSource": "none",
},

Related

Numpy module not found when working with Azure Functions in VS Code and virtualenv

I'm new to working with azure functions and tried to work out a small example locally, using VS Code with the Azure Functions extension.
Example:
# First party libraries
import logging
# Third party libraries
import numpy as np
from azure.functions import HttpResponse, HttpRequest
def main(req: HttpRequest) -> HttpResponse:
seed = req.params.get('seed')
if not seed:
try:
body = req.get_json()
except ValueError:
pass
else:
seed = body.get('seed')
if seed:
np.random.seed(seed=int(seed))
r_int = np.random.randint(0, 100)
logging.info(r_int)
return HttpResponse(
"Random Number: " f"{str(r_int)}", status_code=200
)
else:
return HttpResponse(
"Insert seed to generate a number",
status_code=200
)
When numpy is installed globally this code works fine. If I install it only in the virtual environment, however, I get the following error:
*Worker failed to function id 1739ddcd-d6ad-421d-9470-327681ca1e69.
[15-Jul-20 1:31:39 PM] Result: Failure
Exception: ModuleNotFoundError: No module named 'numpy'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound*
I checked multiple times that numpy is installed in the virtual environment, and the environment is also specified in the .vscode/settings.json file.
pip freeze of the virtualenv "worker_venv":
$ pip freeze
azure-functions==1.3.0
flake8==3.8.3
importlib-metadata==1.7.0
mccabe==0.6.1
numpy==1.19.0
pycodestyle==2.6.0
pyflakes==2.2.0
zipp==3.1.0
.vscode/settings.json file:
{
"azureFunctions.deploySubpath": ".",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.pythonVenv": "worker_venv",
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~2",
"debug.internalConsoleOptions": "neverOpen"
}
I tried to find something in the documentation, but found nothing specific regarding the virtual environment. I don't know if I'm missing something?
EDIT: I'm on a Windows 10 machine btw
EDIT: I included the folder structure of my project in the image below
EDIT: Added the content of the virtual environment Lib folder in the image below
EDIT: Added a screenshot of the terminal using the pip install numpy command below
EDIT: Created a new project with a new virtual env and reinstalled numpy, screenshot below, problem still persists.
EDIT: Added the launch.json code below
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
SOLVED
So the problem was neither with python, nor with VS Code. The problem was that the execution policy on my machine (new laptop) was set to restricted and therefore the .venv\Scripts\Activate.ps1 script could not be run.
To resolve this problem, just open powershell with admin rights and and run set-executionpolicy remotesigned. Restart VS Code and all should work fine
I didn't saw the error, due to the many logging in the terminal that happens
when you start azure. I'll mark the answer of #HuryShen as correct, because the comments got me to the solution. Thank all of you guys
For this problem, I'm not clear if you met the error when run it locally or on azure cloud. So provide both suggestions for these two situation.
1. If the error shows when you run the function on azure, you may not have installed the modules success. When deploy the function from local to azure, you need to add the module to requirements.txt(as Anatoli mentioned in comment). You can generate the requirements.txt automatically by the command below:
pip freeze > requirements.txt
After that, we can find the numpy==1.19.0 exist in requirements.txt.
Now, deploy the function from local to azure by the command below, it will install the modules success on azure and work fine on azure.
func azure functionapp publish <your function app name> --build remote
2. If the error shows when you run the function locally. Since you provided the modules installed in worker_venv, it seems you have installed numpy module success. I also test it in my side locally, install numpy and it works fine. So I think you can check if your virtual environment(worker_venv) exist in the correct location. Below is my function structure in local VS code, please check if your virtual environment locates in the same location with mine.
-----Update------
Run the command to to set execution policy and then activate the virtual environment:
set-executionpolicy remotesigned
.venv\Scripts\Activate.ps1
I could solve my issue uninstalling python3 (see here for a guide https://stackoverflow.com/a/60318668/11986067).
After starting the app functions via F5 or func start, the following output was shown:
This version was incorrect. I have chosen python 3.7.0 when creating the project in the Azure extension. After deleting this python3 version, the correct version was shown and the Import issue was solved:

Instance of 'SQLAlchemy' has no 'Column' member (no-member)

I'm currently trying to implement steam login into website. But I'm unable to get pass this error within the code. I've created the database object but it keeps showing the error I mentioned earlier. I'm not sure whether SQLAlchemy has changed or what since I used it.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
The message emitted by pylint is
E1101: Instance of 'SQLAlchemy' has no 'Column' member (no-member)
EDIT: After read and try np8's answer my previous answer is wrong there is a package you have to install, which is pylint_flask_sqlalchemy
so the answer will be
on your project directory find folder .vscode (if you dont have it, just create it) then create file settings.json and add this line
{
# You have to put it in this order to make it works
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_flask_sqlalchemy",
"pylint_flask", # This package is optional
]
}
You also need to have pylint-flask-sqlalchemy and if you want to use pylint-flask install on your current python environment:
pip install pylint-flask-sqlalchemy
pip install pylint-flask
pip install pylint-flask
In case of Visual Studio Code: Open File > Preferences > Settings > Edit in settings.json as below:
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask"]
Summary of working and non-working configurations
It seems that this can be configured so many ways incorrectly, that I decided to write the different options down. I am assuming VS Code, but for command line or other editor, arguments and their order is the same. These were tested using the latest versions of all the packages (list in the bottom of this post)
Working versions
# What you'll need is pylint_flask_sqlalchemy
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask_sqlalchemy"]
# You can add pylint_flask but only if it is *AFTER* pylint_flask_sqlalchemy
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask_sqlalchemy", "pylint_flask"]
Non-working versions
# pylint_flask does not help, but can break it (see below)
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask"]
# You can NOT add pylint_flask *BEFORE* pylint_flask_sqlalchemy
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask", "pylint_flask_sqlalchemy"]
# CAUTION: These will disable pylint silently altogether!
# Do not use dash (-) but underscore (_)!
"python.linting.pylintArgs": ["--load-plugins", "pylint-flask-sqlalchemy", "pylint-flask"]
"python.linting.pylintArgs": ["--load-plugins", "pylint-flask"]
"python.linting.pylintArgs": ["--load-plugins", "pylint-flask-sqlalchemy"]
Details for those who are interested
pylint_flask_sqlalchemy
The pylint-flask-sqlalchemy1 was created specifically to fix this2. You can enable it by adding it to --load-plugins of pylint. In command line this would be
python -m pylint --load-plugins pylint_flash_sqlalchemy <mymodule.py>
and in VS Code (Settings (JSON)):
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask_sqlalchemy"]
1Also mirrored to GitHub: https://github.com/anybox/pylint_flask_sqlalchemy
2See this comment on pylint Issue tracker.
pylint_flask
pylint-flask is pylint plugin for Flask. It has nothing to do with Flask-SQLAlchemy and it does not even try to solve the false positive issues pylint has with Flask-SQLAlchemy3. The only possibility to use pylint-flask to "make the errors disappear" is to load it with erroneusly with dashes, which makes the whole pylint to be disabled.
It seems that pylint-flask must be loaded after pylint-flas-sqlalchemy; I tested with my setup, and for some reason
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask", "pylint_flask_sqlalchemy"]
will not work, but
"python.linting.pylintArgs": ["--load-plugins", "pylint_flask_sqlalchemy", "pylint_flask"]
will. So the order of loading plugins matters.
3 See the code for yourself: pylint-flask source & pylint-flask-sqlaclhemy source
Caution: Do not remove your linting accidentally
As the documentation of pylint-flask and pylint-flask-sqlalchemy says, the names in the argument --load-plugins should be written with underscores; If you use
"python.linting.pylintArgs": ["--load-plugins", "pylint-flask", "pylint-flask-sqlalchemy"]
in VS Code, the errors will be gone, but so will be all of your linting as the pylint crashes silently in the background.
Installing pylint-flask-sqlalchemy
pip install pylint-flask-sqlalchemy
Used versions
Flask 1.1.2
Flask-SQLAlchemy 2.4.4
pylint 2.5.3
pylint-flask 0.6
pylint-flask-sqlalchemy 0.2.0
See also
Discussion on pylint GitHub issue: "Problem with Flask-SQLAlchemy, cannot find valid and existing property in SQLAlchemy object."
Open the Command Palette (Command+Shift+P on macOS and Ctrl+Shift+P on Windows/Linux) and type in one of the following commands:
Python: Select Linter
Switch from PyLint to flake8 or other supported linters.
I've just encountered this issue. Neither of the suggested solutions worked for me, but the following does.
First, install these modules:
pip install pylint-flask
pip install pylint-flask-sqlalchemy
Then, in Visual Studio Code, you need to add the following to your settings.json file:
"python.linting.pylintArgs": ["--load-plugins", "pylint-flask", "pylint-flask-sqlalchemy"]
For Windows, this works: In case of Visual Studio Code: Open File > Preferences > Settings > Edit in settings.json ->
and add this code after comma below existing settings:
"python.linting.pylintArgs": [
"--load-plugins",
"pylint-flask"
]

VS Code - pylinter cannot find module

I started using VS Code for Python development on a Mac and cannot make pylint find a module.
This is my project folder structure:
project_root/
.env
.vscode/
settings.json
lib/
# lib containing necessary modules
sample/
client/
EDAMTest.py
# many more files
I use a virtualenv in which I have installed pylint. The virtual env is activated in the terminal. I started code from within project_root folder via code . in my terminal.
VS Code says it is using the correct interpreter. I can see on the bottom left that it says Python 3.6.1 (virtualenv)
If I want to test the project_root/sample/client/EDAMTest.py code within terminal I can do it via export PYTHONPATH=../../lib; python EDAMTest.py while being in folder project_root/sample/client/.
Now if I am in VS Code, open the file EDAMTest.py, pylint is telling me that it cannot import modules from lib.
Now my question:
How can I add lib to PYTHONPATH in VS Code?
I found several possible ways to do so:
Create a .env file (see [1] below).
Specify PYTHONPATH in .vscode/launch.json file (see [2])
None of the possible solutions I found seem to work.
What am I missing?
[1] Environment variable definitions file
This tells me how to define global (env) vars. So I specified this:
PYTHONPATH="~/.virtualenvs/evernote/bin/python;lib"
But it won't work. Still libs path is not found by pylint
[2] So I did create a launch.json file like so:
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config.python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceRoot}",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
],
"env": {
"PYTHONPATH": "~/.virtualenvs/evernote/bin/python:lib"
}
}
---
EDIT
Here is a link that tries to address this problem:
Troubleshooting linting
That link tries to address several possible problems, one is this:
... unable to import
The suggested solution is:
Ensure that the pythonPath setting points to a valid Python installation where Pylint is installed.
=> Yes, I did.
Alternately, set the python.linting.pylintPath to an appropriate version of Pylint for the Python interpreter being used.
=> I did, still no success:
My .vscode/settings.json:
{
"python.pythonPath": "~/.virtualenvs/evernote/bin/python",
"python.linting.pylintPath": "~/.virtualenvs/evernote/bin/pylint"
}
It seems that I had to use a colon instead of a semicolon in .env file like so: PYTHONPATH="~/.virtualenvs/evernote/bin/python:lib". That seems to solve the problem.

Using psycopg2 or other python module within Visual Code

I am using Visual Code and wanted to script some Python code that connects to a database. Psycopg2 seems to be the perfect library for just that. So I had in my settings.json file:
{
"python.linting.pylintEnabled": true,
"python.autoComplete.extraPaths": [
"c:/OSGeo4W64/apps/python27",
"C:/OSGeo4W64/apps/Python27/Lib/site-packages/psycopg2"
],
"python.pythonPath": "C:/OSGeo4W64/bin/python.exe"
}
I still get the error
'no module named psycopg2'
on the first line in my code: import psycopg2.
Or psycopg2 is install but not in the right place you can check where it is with this method :
How do I find the location of Python module sources?
or as have said bernie you don't have psycopg2you can check this way:
https://askubuntu.com/questions/588390/how-do-i-check-whether-a-module-is-installed-or-not-in-python
in this case in your terminal do : pip install psycopg2or if you use anaconda conda install -c anaconda psycopg2=2.7.1

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).
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.
I know that in settings.json, I can specify a python.pythonPath setting, which I have done and is pointing to a virtual environment.
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.
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.
"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). 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:
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.
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
"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.
This file should have an absotute path per line, for each module or package to be included in the PYTHONPATH.
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. I'm using Linux. For Windows, use 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.
Finally, I added the PyLance extension per https://stackoverflow.com/a/64103291/11262633.
I also reloaded my workspace.
These two changes allowed me to run Python programs using the debugger and the Run menu. AutoComplete is aware of the added path, and my VSCode linter (was the default linter pylint, now ``pylance```) now works.
I made it work through adding "python.analysis.extraPaths" when using Pylance and IntelliCode.
In 2022, the configuration is as file .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); need to install code command in PATH (vsc shell command: install...)
#!/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'.
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
eg: "python.venvPath": "~/.virtualenvs"
This allows you to find several (eg: virtualenvs) as mentioned:
To select a specific environment, use the Python: Select Interpreter
command from the Command Palette

Categories

Resources