1

When I try to run any .py file on my vscode ; it always show this error!

zsh: command not found: python

% which python
python not found

% which python3
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3

while my terminal from other environment shows

(tensorflow) anshumansinha@lawn-128-61-45-180 deepOscillations_try-master % which python
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflow/bin/python

(tensorflow) anshumansinha@lawn-128-61-45-180 deepOscillations_try-master % which python3
/opt/homebrew/Caskroom/miniforge/base/envs/tensorflow/bin/python3

Have I installed my python multiple times? And it is not showing up on my vscode? How to correct this? Also does the honeybrew version only work for a given environment? Because when I go for a new terminal window and write the following, again it stops working

anshumansinha@lawn-128-61-44-2 ~ % which python
python not found
anshumansinha@lawn-128-61-44-2 ~ % which python3
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3
anshumansinha@lawn-128-61-44-2 ~ % 

Kindly help me understand what am I doing wrong?

Anshuman Sinha
  • 157
  • 1
  • 9

3 Answers3

2

To use python in place of python3 you should add this line to ~/.zshrc

alias python=python3

This way you will use your system Python /Library/Frameworks/Python.framework/Versions/3.10/bin/python3.

If you want to use the Python from the tensorflow virtual environment you should activate it also inside VSCode

$ . tensorflow/bin/activate
chc
  • 498
  • 1
  • 4
  • 18
  • What's `~/.zshrc` ? and how to add this line to that? additionaly the 2nd line does not work! `no such file or directory: tensorflow/bin/activate` – Anshuman Sinha Aug 29 '22 at 01:17
  • 1
    `~/.zshrc` it's a file. It's located on directory ` ~` and it's an hidden file, since its name starts with a dot. You can add the line by open the file and just type that line at the end. I personally use Vim `$ vim ~/.zshrc` but you can use your favorite text editor (I think you can also open it with `$ open ~/.zshrc`). – chc Aug 29 '22 at 07:20
  • I added the lines in my `~/.zshrc` file but it is still showing `anshumansinha@lawn-128-61-44-2 ~ % which python python not found` – Anshuman Sinha Aug 29 '22 at 14:26
  • Here's what I think you should do to be sure of using exactly what you need for your project. 1) Choose an installed version of Python you want to use. You should be able see them all with `$ ls /usr/local/bin/python*`. 2) Once you have chosen one (e.g. `/usr/local/bin/python3.9`) enter your project directory and type `$ /usr/local/bin/python3.9 -m venv venv`. 3) Now activate your virtual environment `$ . venv/bin/activate` Every time you work on your project, you must remember to activate the venv. 4) Pip install whatever package you need, e.g. `(venv) $ pip install tensorflow` – chc Aug 29 '22 at 16:09
  • 1
    I recommend you to uninstall all packages you have already installed with `pip` on your system Python (https://stackoverflow.com/questions/11248073/what-is-the-easiest-way-to-remove-all-packages-installed-by-pip) and install any new packages in the future within a virtual environment dedicated to a specific project – chc Aug 29 '22 at 16:12
  • Is there any step by step method following which I can install tensorflow working both vscode and terminal! I wish to uninstall all the previous ones! – Anshuman Sinha Aug 29 '22 at 23:53
  • 1
    VSCode it's just an IDE, it has no Python nor packages installed. It all dependes on which Python you areusing and which packages are installed on that Python. You should follow this tutorial (https://stackoverflow.com/questions/11248073/what-is-the-easiest-way-to-remove-all-packages-installed-by-pip) and remove everything you have installed in your basic (system) Python. Then you should learn how to work on virtual environments (https://realpython.com/python-virtual-environments-a-primer/) and install there what you need to use, for example tensorflow – chc Aug 30 '22 at 06:41
2

You have to install two things. Python itself, and the Python extension inside VSCode that allows it to make use of Python.

The VSCode extension must be installed from within VSCode by searching "Python" in the Extension tab on the left side of the screen and installing it.

Davezilla
  • 21
  • 1
2

In mac, python points to the pre-installed python2 by default. But since Monterey 12.3, python2 is no longer pre-installed.

So you need to create an alias for your machine using the following command to make python point to python3.

echo "alias python=/usr/bin/python3" >> ~/.zshrc

If you want to keep using the python 2 version on mac, check out this answer.

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • I wish to use my python and tensorflow which I installed from homebrew! ` environment location: /opt/homebrew/Caskroom/miniforge/base` and it shows up on one of my terminals as : `(tensorflow) anshumansinha@lawn-128-61-45-180 deepOscillations_try-master % which python3 /opt/homebrew/Caskroom/miniforge/base/envs/tensorflow/bin/python3` ; but other terminal don't show like this! The keyword `(tensorflow)` is missing in the initial statement! before `anshumansinha@lawn`. I am getting confused in all this! – Anshuman Sinha Aug 29 '22 at 14:36