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
Related
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",
},
I have installed Python extention on VS code. The version that I use on VS code is 3.9. when I try to install openpyxl package on VS code console using code pip install openpyxl I get an error:
bash: /Library/Frameworks/Python.framework/Versions/3.9/bin/pip: No such file or directory.
I have a pip.py file in the corresponding directory. What I am doing wrong?
Here is contents of my settings.json:
{
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"security.workspace.trust.untrustedFiles": "open",
"python.defaultInterpreterPath": "/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9"
}
Try running this each time you open a terminal window:
alias python=/usr/local/bin/python3
alias pip=/usr/local/bin/pip3
I opened the .zprofile file on my mac and added the line export PYTHONPATH="/Users/username/Library/Python/3.9/bin"
Save the file.
Close Terminal.app;
Start Terminal.app again, to read in the new settings, and type this:
echo $PYTHONPATH
It should show something like /Users/username/Library/Python/3.9/bin
https://bic-berkeley.github.io/psych-214-fall-2016/using_pythonpath.html
~
I have an issue with Pylint which reports the following (false-positive?) warning:
standard import "import re" should be placed before "from astroid
import MANAGER" pylint(wrong-import-order) [5,1]
and this in all my file at the same line (5).
I cannot find this line:
from astroid import MANAGER
anywhere in any of my files
My settings :
pylint 2.8.3
astroid 2.5.6
python 3.8.6
VS Code :
{
"python.pythonPath": "-------",
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_flask_sqlalchemy",
"pylint_flask",
"--max-line-length=80",
]
}
=> Is it a bug coming from pylint or does this line exist in one my my third party package like one of the flask extension ?
It look like you're analysing astroid, a pylint dependencies and not your own code. You should launch pylint on your own code and not the code installed in your virtualenv. Putting the correct path in "python.pythonPath": "-------",, for example "python.pythonPath": "src/my_code", should fix this.
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:
I have recently been asked to learn the AWS-CDK module in Python to handle deployments and following the examples on in the AWS-CDK documentation I am getting an error that indicates it cannot import 'core'
I have created a virtual environment that is using Python 3.6.6 and am running CDK version 1.16.2 (build 5893301) all running on a Windows 10 64-bit machine in VSCode
from aws_cdk import core
I would expect this to just load the constructor but it just returns the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'core'
I have searched and searched and I cannot seem to find what I am doing wrong or anything that contradicts the documentation on the CDK site. Any insight would be appreciated.
This worked for me. Might be related to conda / venv interactions.
pip3 install -r requirements.txt --user
Is aws_cdk.core installed in your environment?
I have this:
pip list|grep core
aws-cdk.core 1.68.0
I noticed that the cdk.json "app" value can also generate this error.
Example of the cdk.json file which used to generate the error:
{
"app": "python3 app.py",
"context": {
...
}
}
Changing the app value from python3 to python resolved the problem for me. Like this:
{
"app": "python app.py",
"context": {
...
}
}
I am not entirely sure why this happens as I am using python 3.8 and I can see a python.exe in the python path/environment variables. If I deactivate the virtual env, and I have cdk installed globally, this problem goes away regardless of whether I use python or python3 for the app value. Therefore, I suspect it's got to do something with environment paths.
aws-cdk-lib==2.34.0
constructs>=10.0.0,<11.0.0
aws-cdk.aws-kinesisfirehose-alpha
aws_cdk.aws-kinesisfirehose-destinations-alpha
aws_cdk.core
pip list|grep core
If you are unable to load this module that means it is not installed. It is possible that it is not installed in the python virtual environment you are using. When I search aws_cdk module in my working directory using find . -name aws_cdk , I found that I had two virtual environments setup. One was under venv and other one was .venv. And the module installed were installed in one of them but not in the other environment. Switching the environment and installing the module fixed the error.