Python debugger can't find module - python

I am building an application in Python. A while ago, I was able to debug it without any issues, but after accidently reseting my VS Code settings, I am not able anymore. Whenever I try to debug I get the following error on the first installed library I try to import:
Exception has occurred: ModuleNotFoundError No module named
'PySimpleGUI'
When I try to install the library using pip3, I get the message:
Requirement already satisfied: PySimpleGui in c:\users\adassa\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages
When I run directly from the console using python3 name_of_file.py, the file runs without any problems. Here is my launch.json for debugging:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"env": { "PYTHONPATH": "${workspaceRoot}"}
}
]
}
I read in other questions that this error has to do with different installations of Python, and the debugger trying to find the library on the wrong directory, but couldn't understand how to solve it.
I tried replacing PYTHONPATH with the path of python3 I get from the console. (I obtaineed that path using the command from this answer: https://stackoverflow.com/a/647798/14874778, but the error remains. How can I solve this problem?

Do you have multiple python installations? If so, try something that looks like this (made for python 3.7.9 & Windows):
py -3.7 -m pip install pysimplegui
This has always worked for me.
You can also try specifying which installation you want to run the script, which should look like this (also 3.7.9, also Windows):
py -3.7 helloworld.py

Related

vscode has correct python interpreter selected but it cannot see one of the installed modules

I installed sympy using the commands
source website-env/bin/activate
(website-env)pip install sympy
My Python script is
import sympy as sp
u1,u2,d1,d2=sp.symbols('u1,u2,d1,d2')
psip,psim,phip,phim=sp.symbols('psip,psim,phip,phim')
psi=u1*(phip-phim)*u2-u1*(psip-psim)*d2-d1*(psip+psim)*u2+d1*(phip+phim)*d2
sp.factor(psi,phip,phim,psip,psim)
It runs fine from the command line but when I select this Python interpreter and try to run it within VSCode, it give "module not found - sympy"
My launch.json is
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
What could be the problem?
It seems there was something wrong with my virtual environment. I used pip freeze to get a list of modules, then deleted the environment and recreated it. That fixed it.

python vscode ImportError: No module named simplejson

i have import simplejson in my code, and installed using pip install simplejson using python3
so as i checked, it perfectly runs in my terminal opening the file but when i try to build in VScode, it shows an error like,
see the error in this link as
`> Executing task: python /Users/Tony/Documents/python3.9/helloworld.py <
Traceback (most recent call last):
File "/Users/Tony/Documents/python3.9/helloworld.py", line 1, in
import simplejson as json
ImportError: No module named simplejson
The terminal process "/bin/bash '-c', 'python /Users/Tony/Documents/python3.9/helloworld.py'" failed to launch (exit code: 1).
but it still runs on my terminal. and if i check the pip list, simplejson is still there and i also tried to use different version of python too.
and I have activated the virtual environment enter image description here
and i am using the last one which i recently activated.
please help me to find the error. i don’t really know where to fix thanks
I've reproduced this process and hope the following steps could help you
1.Activate the venv and install simplejson;
2.Run the helloworld.py in Terminal to make sure the script no error;
3.Configure the tasks.json, pay attention to use the venv's pythonpath;
"version": "2.0.0",
"tasks": [
{
"label": "run python file",
"type": "shell",
"command": "/venv's pythonpath/ helloworld.py",
"group": {
"kind": "test",
"isDefault": true
},
"problemMatcher": [
"$tsc"
]
}
]
4.Tasks:Run Task;

Python in VS Code: Error when importing module from subfolder

I recently started exploring VS Code for developing Python code and I’m running into an issue when I try to import a module from a subfolder. The exact same code runs perfectly when I execute it in a Jupyter notebook (the subfolders contain the __init__.py files etc.) I believe I followed the instructions for setting up the VS Python extension correctly. Everything else except this one import command works well, but I haven’t been able to figure what exactly is going wrong.
The structure of the project is as follows: The root folder, which is set as the cwd contains two subfolders (src and bld). src contains the py-file that imports a module that is saved in foo.pyin the bld-folder using from bld.foo import foo_function
When running the file, I get the following error: ModuleNotFoundError: No module named ‘bld'. I have several Anaconda Python environments installed and get the same problem with each of them. When copying foo.py to the src directory and using from foo import foo_function everything works.
My launch.json file is as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {"PYTHONPATH": "${workspaceFolder}:${workspaceFolder}/bld"},
"console": "integratedTerminal"
}
]
}
Any ideas or help would be greatly appreciated!
Stefan‘s method worked for me.
Taking as example filesystem:
workspaceFolder/folder/subfolder1/subfolder2/bar.py
I wasn't able to import subfolders like:
from folder.subfolder1.subfolder2 import bar
It said: ModuleNotFoundError: No module named 'folder'
I added to .vscode/settings.json the following:
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}"
}
I also added at the beginning of my code:
import sys
#[... more imports ...]
sys.path.append(workspaceFolder)
# and then, the subfolder import:
from folder.subfolder1.subfolder2 import bar
Now, it works.
Note: all my folders and subfolders have an empty file named __init__.py. I still had to do the steps described above.
VSCode version: 1.52.0 (from 10-dec-2020)
I think I finally figured out the answer myself: The integrated terminal does not scan the PYTHONPATH from the .env-file. When running the file in an integrated window, the PYTHONPATH is correctly taken from .env, however. So in order to run my script in the terminal I had to add the terminal.integrated.env.* line in my settings.json as follows:
{
"python.pythonPath": "/anaconda3/envs/py36/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": false,
"python.envFile": "${workspaceFolder}/.env",
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}"
}
}

Why VSCode fails executing my main module in the new VSCode update?

**********New post edition with a simple example********************
After the suggestion of Martineau, I tried a simplified example python program in the following.
The background was that last week Vscode asked me to update to VSCodeUserSetup-x64-1.31.1 in Win10.
So I did.
Then sometimes VSCode failed to execute my main module.
I tried to reinstall and uninstall vscode. I also tried to modify the path variables here and there.
But the problem is still there.
Still I can execute the program OK purely in the console.
The simplified python program is the following.
print ("Here I am!")
The launch.json file is the following.
{
"version": "0.2.0",
"configurations": [
{
"name": "vsCode Test ",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceRoot}/src/pt.py",
"args": [
],
"cwd": "${workspaceRoot}",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
}
]
}
After I clicked the debugger button and then the right triangle button to execute the debugger, I always got the following screenshot which showed that nothing was executed.
I suspect that the new update has changed my settings in vscode.
But I don't know where to reconfig vscode.
Your help will be deeply appreciated!
Thanks
Farn
****************Old post with a more complicate example!****************
dear all:
Last week Vscode asked me to update to VSCodeUserSetup-x64-1.31.1.
So I did.
Then sometimes VSCode stopped to execute my main module.
The basic screen layout is the following.
VSCode basically stalled before the first statement and the first breakpoint.
But if I use Console to execute "python CnTaaD.py -L", the module (CnTaaD.py) executed correctly.
I tried several times and even reinstall, uninstall, used an older version of VSCode.
The stalling problem came on and off.
Then last night, VSCode always stalled before the first statement and the first breakpoint.
My friend helped me and guessed it is because the path to python interpreter is wrong now.
But we tried to set the path variables here and there.
Nothing good happened.
The code of CnTaaD.py is in the following for your reference.
It calls another module.
Can anyone help by telling me how to reconfigure maybe VSCode so that I can resume the development ?
Thanks
from __future__ import print_function # (at top of module)
if __name__ == '__main__':
import os
import sys
import CnUserManager
if sys.version_info[0] < 3: # Python 2 and 3:
print ("python 2.x")
# to execute the file, type in the following command in powershell:
# % python CnTaaDPackage.py build_ext --inplace
import future # pip install future
import builtins # pip install future
import past # pip install future
import six # pip install six
else:
print ("python 3.x")
root = os.path.realpath(__file__)
# print ("realpath of __file__ is ", root)
root = os.path.dirname(root)
root = os.path.dirname(root)
sys.argv.append(root)
# print ("sys.argv = ", sys.argv)
CnUserManager.CnUserManagerClass(sys.argv)
I kind of solved the problem.
Basically, I removed the "RedirectOutput" option from the "debugOptions" in launch.json. Somehow, the older version the redirection is to the console in the debug screen.
But now I am not sure where it is to.
In fact, there are three consoles in VSCode, internal, integrated, and external.
I will appreciate it if someone can enlighten me which is which ?
I checked some related pages. But vague explanations are found.

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.

Categories

Resources