1

I am on Ubuntu 20.04 and have both Python2 and Python3 installed natively. I have also installed Python through miniforge, a variant of miniconda. In VSCode I have both the MS Python extension and Pylance installed.

I use the miniforge python for my coding. This works perfectly fine in PyCharm.

However in VSCode, when I try to execute the same file I get errors. After investigating it seems that VSCode is picking native Python2 - even though I have the miniforge Python selected. In this picture it can be seen that the status bar at the bottom states Python interpreter selected is Python3. But the output window shows that the python interpreter is Python2.

A more confusing thing is when I use VSCode for Jupyter notebook files then it picks up the interpreter correctly and I have no issues.

I have checked both User and Workspace settings, and they all point to Python3. How can I fix this for standard .py files?

I prefer VSCode to PyCharm, but will need to use PyCharm till this is resolved.

demokritos
  • 1,416
  • 2
  • 12
  • 34
gautam
  • 23
  • 1
  • 3

2 Answers2

1

It seems that your system console cannot see python3. You need to put the python3 in the PATH variable, before python2. Like:

PATH=path/to/python3:path/to/python2:$PATH

Also, make sure that the environment containing python3 is activated before command prompt appears. It can be done in bash_profile by adding a line like

conda activate my_env_with_python3

Try changing the settings "Python:Python path", "Python:default interpreter path" and "Python:conda path" also.

puio
  • 1,208
  • 6
  • 19
  • On system using the python command gets me Python3. To get Python2 I have to run python2. Also, VSCode does pick up the miniforge/conda environment - when I run the python terminal by itself or work with a jupyter notebook, all of it executes within the conda environment. Its only when running a .py file by itself that I get this error. – gautam Oct 15 '20 at 12:52
  • I have updated all these 3 paths, but it didn't change anything.I also activated Conda and ran VSCode from the commandline, but it is still picking up Python2. – gautam Oct 15 '20 at 17:27
  • 1
    Is there a .vscode folder near this file which might be overriding the settings ? – puio Oct 16 '20 at 11:23
  • I've run out of ideas.. please file a bug report https://github.com/Microsoft/vscode – puio Oct 16 '20 at 11:24
  • I ultimately nuked the problem by removing Python2 from my system. Now VSCode picks the interpreter as per the settings. It uses the python in the Conda install, not the system python3. Now am finding and fixing/reinstalling apps that needed python2. Thanks for the feedback. I will try to file a bug report, but might be difficult since I can no longer reproduce the issuel. – gautam Oct 19 '20 at 08:43
0

I have just bumped into something similar. The Run code option resulted in the file being run with the default interpreter instead of the venv-based one with necessary packages installed.

The fix was simply to use "Run python file" instead:

enter image description here

The run-code behavior must be customizable, something is mentioned e.g. here: Run Code vs Run Python File in Terminal for VSCODE but I didn't bother.

Sergey Grechin
  • 876
  • 11
  • 14