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
~
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 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
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?
I created my own python packages that contains some script. It can be installed with
python setup.py install
Under Linux I can then run the script when the correct conda environment is loaded with:
my_script.py
Under Windows the script is not found although I specify it twice in the setup.py file:
config = {
...
'version': '0.0.25',
'scripts': ['scripts/my_script.py', 'scripts/my_script.bat'],
'packages': ['my_package'],
'name': 'ms_package',
'data_files': [('scripts', ['scripts/my_script.py', 'scripts/my_script.bat']),
('static', ['static/my_data.csv'])]
}
The script starts with a shebang:
#!/usr/env python
The file is found, but when I run it I get the message that this file has not app associated. How to set this up correctly under Windows? Do I need a second file that specifies an app?
I added the my_script.bat that loads the anaconda environment, but then fails with the same error. And when I put
python my_script.py
in there, it does not find my_script. So, I would have to do something equivalent to
python `which my_script.py`
Is there a functionality like this on Windows (Windows 10)?
I would like to have an icon for the user to click on. For people that don't know how to use the command line.
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