0

There are two Python versions on my Mac. One in Anaconda, the other in VS Code. When I try to install libraries for the Python in VS Code by terminal, it always goes to Anaconda. What shall I do in this case?

I tried to install directly from VS Code, but that always resulted in a syntax error. Then, I Googled the issue and was told to install the libraries by terminal. But when I tried to install from terminal, the libraries were always installed in the Anaconda, not in VS Code.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 1
    `pip install` installs to the normal python, `conda install` installs to anaconda. – code May 13 '23 at 02:36
  • "*When I try to install*" How _exactly_ are you installing packages? What commands are you using? Where are you running those commands? Also, VS Code does not have its own Python version, it uses whatever is available on your system. See https://code.visualstudio.com/docs/python/environments#_python-environments. In this case, macOS comes with its own Python, so VS Code is just using that. You probably just need to tell VS Code to use the correct Python version. See: https://code.visualstudio.com/docs/python/environments#_working-with-python-interpreters – Gino Mempin May 13 '23 at 03:26
  • See https://stackoverflow.com/q/48135624/2745495 OR https://stackoverflow.com/q/74110362/2745495 – Gino Mempin May 13 '23 at 03:29

2 Answers2

0

Assuming that the Python environment that comes with VS Code is located in the /usr/bin directory, you can use the following command to install a package using the correct version of pip:

/usr/bin/python3 -m pip install

This command specifies the full path to the pip executable that's associated with the Python environment that comes with VS Code, and then uses the -m flag to run pip as a module of that Python environment.

By using this command, you can make sure that the package is installed in the correct Python environment, and not in the Anaconda environment.

Dheeraj
  • 9
  • 2
  • You can tell which Python is being used. If a workspace is setup, check the lower right portion of VS Code, and click on the displayed Python interpreter, it should show a modal at the top with the path. OR, from the terminal, do `which python` or `which python3` which would give the full path. The Python in /usr/bin might be Python 2, which definitely should not be used and should not be where to install packages. – Gino Mempin May 13 '23 at 03:47
0

When you open a python file, the python version of the current workspace will be displayed in the lower right corner.

enter image description here

Of course, this is your choice. You can also click it and select other python interpreters.

enter image description here

Generally, when you select an interpreter and create a new terminal, the terminal will automatically activate the environment, which is controlled by the following settings:

"python.terminal.activateEnvironment": false

enter image description here

Using the pip command directly on the above terminal to install the package will be directly installed in the venv virtual environment.

In addition, it should be noted that the command for the conda installation package is conda inatall ..., and the command for the general python installation package is pip install ....

In addition, you can manage the conda environment and its packages within the Anaconda Navigator.

enter image description here

About the pyhton environment in vscode.

JialeDu
  • 6,021
  • 2
  • 5
  • 24