VS code 1.63.2 installed on Fedora 35 via MS repo (not flatpak).
Python3.10 installed via Fedora 35 repo.
python3-autopep8,black and python3-flake8 are installed via repo.
VS Code is using Python3.10, the Python and Pylance extensions are installed.
My settings.json has:
{
"python.defaultInterpreterPath": "/bin/python",
"python.linting.flake8Enabled": true,
"python.linting.enabled": true,
"python.formatting.autopep8Path": "/usr/bin/autopep8-3",
"python.linting.flake8Path": "/usr/bin/flake8-3.10",
"python.formatting.blackPath": "/usr/bin/black"
}
I have selected to use the flake8 as the linter via crtl+shift+p. When i run the linter though, nothing happens, no errors. Thats when run on a file like the below (which should fail)
#!/usr/bin/env python3
msg = 'hello'
print msg
What am I doing wrong pls?
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 try running code in Visual Studio code but I keep getting a ModuleNotFoundError. When I run the code in my terminal or in debug mode in VS with the activated conda environment it works fine.
System: Mac M1 12.3
Conda Environment, selected in Visual Studio Code.
I have added this
import os
print('cwd is %s' %(os.getcwd()))
import sys
print('executable is %s' %(sys.executable))
print('path is %s' %(sys.path))
and running in terminal gives:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
running in VS via Run Python File (upper right corner button) gives:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/lib/python39.zip', '/Users/USERNAME/miniforge3/lib/python3.9', '/Users/USERNAME/miniforge3/lib/python3.9/lib-dynload', '/Users/USERNAME/miniforge3/lib/python3.9/site-packages']
running in VS via Debug Python File (upper right corner button) gives:
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
I am confused - How can I get this running in VS Code?
---- Update March 23 2022:
I have three options
When I add
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName"
}
to
settings.json
(see [https://www.wiseowl.co.uk/blog/s2930/module-not-found-error.htm] 2 from #Kyouya Sato)
and run Run Code it works and I also get
cwd is /Users/USERNAME/xyz/CodeFolder
executable is /Users/USERNAME/miniforge3/envs/conda_envNAME/bin/python
path is ['/Users/USERNAME/xyz/CodeFolder', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python38.zip', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/lib-dynload', '/Users/USERNAME/miniforge3/envs/conda_envNAME/lib/python3.8/site-packages']
without changing
settings.json
it does also not work using Run Code.
Run Python File is not working at all.
Workaround:
Set "python.terminal.activateEnvironment" to false in the settings.json.
Downgrade to the previous version of the extension which works fine(avoid conda run).
Try out the following VSIX which has the potential fix: https://github.com/microsoft/vscode-python/suites/5578467772/artifacts/180581906, Use Extension: Install from VSIX command to install the VSIX.
Reason:
Conda has some problems:
conda run -n MY-ENV python FILE.py uses the base interpreter
instead of environment interpreter.
Running using conda run inside already activated environment should
be the same as running it outside
conda run does not remove base environment components from
$PATH
I'm using PythonKit in my Swift project for MacOS. At the moment I'm using Python 2.7 but from MacOs 12.3 it isn't no more supported so I'm trying to migrate to Python 3 but it doesn't work.
func applicationDidFinishLaunching(_ notification: Notification) {
if #available(OSX 12, *) {
PythonLibrary.useVersion(3)
}
else {
PythonLibrary.useVersion(2)
}
let sys = Python.import("sys")
print("Python \(sys.version_info.major).\(sys.version_info.minor)")
print("Python Version: \(sys.version)")
print("Python Encoding: \(sys.getdefaultencoding().upper())")
sys.path.append(Bundle.main.resourceURL!.absoluteURL.path)
let checker = Python.import("checkLibrary")
_ = Array(checker.check())
}
This is the error message:
PythonKit/PythonLibrary.swift:46: Fatal error: Python library not found. Set the PYTHON_LIBRARY environment variable with the path to a Python library.
The code fail on MacOs 12 on line 9th line (let sys = Python.import("sys")), so I can't interact so sys in any way.
I've already tried to disable sandbox and Hardened Runtime but is seems useless.
I was having the same issue.
where python3
which python3
type -a python3
I could clearly see that Python3 was present using any of the above commands from terminal. Python3 wasnt something that I directly installed, (probably something I added during an install of XCode) but I could see it located at "/usr/bin/python3"
I had already removed the sandbox and disabled the hardened runtime.
Adding the environment variable to XCode did not work and Google ultimately told me to do what had already been done.
Finally, I decided to just perform a fresh install, following the blog as guidance.
https://www.dataquest.io/blog/installing-python-on-mac/#installing-python-mac
https://www.python.org/downloads/macos/ (direct URL for Python download)
After installing, everything worked as expected.
PythonLibrary.useVersion(3)
PythonLibrary.useLibrary(at: "/usr/local/bin/python3")
I could use either of the above methods, without adding the environment variable to the XCode scheme.
Hopefully that helps
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