I wonder why python with homebrew doensn't work in VScode - python

I have installed python3 with homebrew, but it keeps making errors when i run.
i think problem is that i'm not working at homebrew file, so i've searched but couldn't get solutions.
which python3
give me
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
and i'm using Python 3.9.9 64-bit interpreter in /opt/homebrew/bin/python3
How can i make my VScode work with homebrew python3enter image description here?

If you focus on the terminal you can probably press arrow up to see which command Visual Studio code used to run your program. It'll probably reveal the path of the python interpreter.
I used the answer from https://stackoverflow.com/a/51488832/467650 to make Visual Studio Code run with the desired python interpreter (which also for me was the homebrew one:
I pressed ⇧⌘P, found Python: Select Interpreter and entered the full path of the python executable. After that Visual Studio Code run just fine.

Related

How to run Python commands in VS Code Terminal

I have installed latest Python Latest Python 3 (python-3.11.0-amd64) and latest VS Code (VSCodeUserSetup-x64-1.73.1). I also installed the Python Extension for Visual Studio Code.
I have selected the interpreter as:
But I am not able to run any Python Command in the terminal even as an administrator. No error and no complain but just empty line:
Why is this happening?
Has Python been added to your path? There's a checkbox for this in the dialogue when you install it, but if you didn't check that box, then its possible that Python hasn't been added to your path.
system properties
edit path
Have you checked python path?
system properties--->environment variables--->system variables--->path

Visual Studio Code wont find my interpreter

I have used python 3.8.5 before on this computer and one day it just can't find the interpreter. I've tried reinstalling python, reinstalling VS code, and I've tried repairing corrupt files.
In vs code
In top left, Click on view
Click on Command Palette
Click on Python: Select Interpreter
Click on your python version
If python directory does not appear, copy python.exe directory and paste it in the Python: Select Interpreter bar.
First, you need to check whether it's a problem related to 'Python' which installed on your computer. Open the cmd out of VSCode, type in 'python --version' to check the output. If you can't get the proper output, you need to check whether you have added the python path to the 'PATH' system variable.
Second, if everything works well out of VSCode. That means that's the problem of 'Python' extension. You'd better reinstall or downgrade the 'Python' extension.

Visual Code on Windows can't find module after installing it with PIP. Using wrong Python in Terminal window?

I am using Visual Code 1.47.0 on Windows 8.1 with the latest version of the Python extension installed too. I just installed a module using pip. I then created a new Python file to test it. The IDE highlights the import statement as an error and the drop-down info box says "Unable to import 'extpylib'. If I try to run/debug the test file, I get a traceback error saying:
C:\Users\User\AppData\Roaming\Python\Python38\Scripts
Exception has occurred: ModuleNotFoundError
No module named 'extpylib'
File "C:\Users\user\Documents\PythonProjects\test-extpylib.py", line 2, in <module>
import extpylib
If I run the pip install command again, even with the "--upgrade" command line argument, it says everything is installed and that I'm using the latest build. I am wondering if this is happening because the VSCODE Terminal window is still using Python 2.7, and not the version I have selected in the status bar, which is Python 3.8.3 64-bit? I am not using the Code Runner extension.
I assumeg that this mismatch of versions between the IDE and the VSCODE Terminal window is causing my issue? If so, how can I get the VSCODE Terminal window to use the same version as that which is selected in the IDE? If not, what is the real problem and how can I fix it?
You may have many python paths and VS code is using the wrong one. Choose the right interpreter path just clicking here:
I solved this by closing and re-opening the vs-code after installing new package.
It seems like an environment issue. In my own Visual Studio code integrated terminal, when I type which python and which pip, I get the correct path to both python and pip executables. So this:
I am wondering if this is happening because the VSCODE Terminal window is still using Python 2.7, and not the version I have selected in the status bar, which is Python 3.8.3 64-bit?
Definitely seems to be the issue.
If your VSCODE terminal still uses Python 2.7, you could follow the tips presented here:
However, launching VS Code from a shell in which a certain Python environment is activated does not automatically activate that environment in the default Integrated Terminal. Use the Terminal: Create New Integrated Terminal command after VS Code is running.
and
Note: conda environments cannot be automatically activated in the integrated terminal if PowerShell is set as the integrated shell. See Integrated terminal - Configuration for how to change the shell.
and
Changing interpreters with the Python: Select Interpreter command doesn't affect terminal panels that are already open.
Of course, the package you installed in an environment is independent of other environments.
As you said the VSCode Terminal using Python2.7 while the status bar shows the selected interpreter is Python3.8.3 64-bit. This meaning you are using the global python(3.8.3) to run the python file while you install the package to the Python(2.7) environment.
Solution:
After you selected the python interpreter in the status bar, you need to activate the environment in Terminal through shortcut 'Ctrl+Shift+`'. You can through these commands to check which environment you are using:
In the terminal:
By command "pip --version" to check which pip you are using.
By command "python" -> "import sys; sys.executable" to check which python you are using.
By command "python" -> "import sys; sys.path" to check which 'site-package' the interpreter searching for.
pip command that was used to install the package is based on a different version of python versus what VS Code is using.
Check the python version of your pip command, run pip --version. For example, on my system, this is the output of this command.
$ pip --version
pip 22.0.4 from /Library/Python/3.8/site-packages/pip (python 3.8)
Check the version of code that VS code is using. For VS code 1.61, the python version will be visible at the bottom left corner while a python file is open in the editor. Screen shot of the same is given below. You can also observe from the screen shot that lazy_streams package is not being recognised by VS code.
To solve the error in VS code, update the python version in VScode to be same as that of pip. Updating of the version can be done by simply clicking on the Python version displayed, which will further prompt you to select among all available python versions on your system.

visual studio code (run Python file in terminal does not work)

visual studio code, pressing run does not work on mac. Let say I want to print the word "Hello" and run the code print("Hello"), it will not be executed. I tried reinstalling python and visual studio but I could't resolve the problem.
When this happens to me, the Python interpreter borked and I need to press ctrl + c (abort) in the terminal a couple of times. It's still in the Python process and vscode is trying to input the run command into a Python process that's not actually a terminal.
I ran into the same issue. Figured out that I forgot to select the Add Python 3.8 to PATH while installing. You can either choose to uninstall and reinstall Python and make sure to select the above option OR you can manually update the user environment variable for PATH and add the path to scripts and python like the following.
C:\Users\<addyourusernamehere>\AppData\Local\Programs\Python\Python38-32\Scripts\
C:\Users\<addyourusernamehere>\AppData\Local\Programs\Python\Python38-32\
For me, downgrading VS Code extension called Python helped.
This shows where the option to install another version is in VS Code Extensions
As for today, I had to go back to version 2021.11.1422169775
M1 MacBook, macOS Monterey 12.2.1

How to change interpreter in Visual Studio Code?

I have installed on my system several Python interpreters, 2.x and 3.x versions. I am trying to prepare my work environment to allow easily switch between code written in both Python version. It is really important to have as much flexible setting in Visual Studio Code (VSC).
The problem is that I have no idea how to set VSC terminal to run code in Python 2.x. Terminal output is needed because it allows to provide user input easily.
I've tried instructions provided on VSC page, like manual interpreter's path indication in folder or workspace setting. I reinstalled Python 2.x to ensure PATH variable has been updated.
When I run code with CodeRunner extension, it always run it in Python 3.x.
Does anyone have similar issue and found how to change Python environment used by this integrated terminal?
All you have to do is press ctrl+shift+p Then will get a search bar kinda thing on top of the screen.
Then type the following command:
> python: select interpreter
You will be provided with options. Select the one you want to use.
At the bottom of the MS Code screen is an info bar that lets you know what line, col, text encoding, etc... It also shows the python interpreter you are accessing.
If you click on the text for the version of python that is running, it will open a list of available interpreters on your system. If 2.7 is in your path, you can select it.
Download and install the python version that you want
https://www.python.org/downloads/
Open the visual studio code
Click on the Bottom left for the version
Select the version that you want the code to be complied
Answers above won't change interpreter in the visual-studio's terminal as explained. You can see for yourself. (I'm on linux)
which python3
# /usr/bin/python3
You can't export a new PATH because it'll ruin your other commands. And neither you can directly change the 'python3' file via root privileges. It doesn't work that way.
Your best option is to reshape your system interpreter according to your needs.

Categories