Seemingly out of nowhere my python scripts in VS Code are no longer running correctly. None of the python packages I've installed are importing correctly anymore, getting a ModuleImport error. When I printed sys.path, and sys.version, I found that not only were the directories in path not correct, but the version of Python VS Code was trying to run was not the same as the version of Python the interpreter was set to use. The interpreter I had selected was using Python 3.9.7, but sys.version was printing Python 3.8. Even more strangely, when I ran the same code with the debugger, the directories printed by sys.path were magically correct, and the version of Python being run was suddenly correct again as well. In the end I completely uninstalled and reinstalled Anaconda and all versions of Python on my system, but I'm still running into the same problem after creating a new environment, the only difference now is that the version of python being run is correct, but the path directories are not.
In both cases the correct interpreter is selected, the only difference is using Run or Debug.
The only thing I can think of that may relate to the error is that I recently installed Linux on an external hard drive to dual boot from, and I installed Python on that as well. I'm not sure how that would cause this though.
CodePudding user response:
You could try these:
- Go to
VSCode
settings and go toextensions
, and then findPython
. Then findDefault Interpreter Path
. If the path it says ispython
, change it to your installation path. - Check your
PATH
and see ifPython
is in it. - Reinstalling
VSCode
orPython
.
CodePudding user response:
It's an issue with the Python Extension update.
Use conda run for conda environments for running python files and installing modules. (#18479)
changelog(28 February 2022).
But it has some problems, I had submitted an issue on GitHub.
The debugger can take the correct python interpreter which you have selected in the VSCoe, while the command conda run xxx
can not. It will persist in the base instead of the NeoNatal environment. It can not select the sub environment under Anaconda3 envs.
Execute sys.executable
you will find it. If execute the python script directly in the terminal likes python pythonFileName.py
instead of conda run xxx
it will be working again.