Home > Software design >  Python works in vs code but not in vs code terminal
Python works in vs code but not in vs code terminal

Time:01-11

I can run my python files with the run python files button. When I open the terminal in vs code, I see this showing up:

source /Users/xxxxx/opt/anaconda3/bin/activate
conda activate base

But if I type where python in the terminal is get:

/usr/bin/python

Furthermore, if I run the python file I get errors like No module named 'whatever' etc, or there's a syntax error, allthough the file runs in vs code.

What can I do to run the files also from the terminal ?

CodePudding user response:

so what is happening is that conda is actually sourcing and using it's own packages when you use VSCode.
You have to do the same if you want to use the same packages, so I'd say just do:

source /Users/xxxxx/opt/anaconda3/bin/activate
conda activate base

For the 2nd problem, you might want to give python it's PYTHONPATH when running it. It essentially tells it where to find other modules (like an src directory).
To do that, just prefix: PYTHONPATH=/folder/path/ python filename.py

CodePudding user response:

You have selected the different environments in VSCode and terminal.

You have selected conda base environment in the VSCode, and your python file runs correctly. But when you are out of VSCode, you are using the global python environment(/usr/bin/python).

And this python version is lower then 3.6. f-string is introduced in 3.6, so you will get the syntax error.

So, you can activate the conda base environment in the terminal before you execute your python file:

source /Users/xxxxx/opt/anaconda3/bin/activate
conda activate base
  •  Tags:  
  • Related