0

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 ?

stanvooz
  • 522
  • 3
  • 19
  • Does this answer your question? [Mac using default Python despite Anaconda install](https://stackoverflow.com/questions/22773432/mac-using-default-python-despite-anaconda-install) – Random Davis Jan 10 '22 at 17:05
  • Maybe you could try to change the location of the python interpreter that VSCode uses – Corni Jan 10 '22 at 17:06
  • I have same problem, it work at out side terminal, vscode, BUT not work on Terminal in vscode. – Tommy Chang Feb 22 '22 at 10:01

3 Answers3

0

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

Berlm
  • 447
  • 3
  • 7
0

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
Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
  • Like mentioned above, I open my terminal (not in vs code) and activate the conda env - it works. I do the same process in the terminal inside vs code - it doesn't work – stanvooz Jan 11 '22 at 13:27
  • @stanvooz In the VSCode, the Python extension will activate the conda environment automatically as you mentioned in the question. You need not activate it manually. And it's not `/Users/xxx/opt/anaconda3/bin/activate && conda activate /Users/xxx/opt/anaconda3; `, it's the `source /Users/xxxxx/opt/anaconda3/bin/activate` and `conda activate base`. – Steven-MSFT Jan 12 '22 at 01:51
  • It tried is as well. nothing changes – stanvooz Jan 14 '22 at 15:55
  • @stanvooz Could you provide a screenshot of it? – Steven-MSFT Jan 17 '22 at 02:54
0

I have a similar issue on MacOS.

In the standard terminal:

$ python -V
Python 3.9.7

$ python3 -V
Python 3.9.7

$ /opt/anaconda3/bin/python -V
Python 3.9.7

All good.

However, in the VSCode terminal (in base conda env):

$ python -V
Python 2.7.10

$ python3 -V
Python 3.6.4

$ /opt/anaconda3/bin/python -V
Python 3.9.7    # this is the one I want by default

In VSCode I set the paths for Anaconda and Python interpreters in the settings (screenshot):

  • Python: Conda Path: /Users/username/opt/anaconda3/bin/conda
  • Python: Default Interpreter Path: /Users/username/opt/anaconda3/bin/python

My .bash_profile (notice I commented the paths for older python versions):

# Setting PATH for Python 3.5
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
#export PATH

# MacPorts Installer addition on 2017-05-11_at_15:09:46: adding an appropriate PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.


# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
#PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
#export PATH


# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/username/opt/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/username/opt/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/Users/username/opt/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/username/opt/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

Also, when I try conda list (in VSCode, conda base env) I get

python                    3.9.7                h88f2d9e_1

which is correct, but when running

python3 -c "import sys; print(sys.version)"

I get

3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 

which is not what I'm looking for.

JDC
  • 1
  • 4