How to set the Ubuntu Python3 interpreter path in VS Code? - python

I'm using Python3 on Ubuntu 20.04 inside WSL2, running print(sys.executable) outputs /usr/bin/python3
Within VS Code, pressing "Select Interpreter" then "Enter Interpreter Path" I have tried both /usr/bin/python3 and the full windows path \\wsl$\Ubuntu/\usr\bin/python3 (which is reachable inside file explorer).
Both paths returns the error An Invalid Python interpreter is selected, please try changing it to enable features such as IntelliSense, linting, and debugging.
It seems to only accept an .exe file, how can I select the Linux path as the interpreter? Thanks.

It sounds like either:
You are starting VSCode from Windows, rather than from inside WSL
Or you don't have the WSL extension installed
Or both
There are several options for doing WSL development in VSCode, but all do require that you start by installing (if you haven't already) either:
The "Remote - WSL" extension
Or the "Remote Development" extension pack, which includes the former
Then, typically, you'll start VSCode by navigating to your project directory in WSL and:
code .
You'll see that VSCode has switched from "Windows" mode to "WSL" mode in the lower left-hand corner of the status-bar, where you should see "WSL: [distroname]".
With that in place, you should be able to select the Linux Python interpreter.
Alternatively, you can start VSCode through Windows, then switch to WSL mode with the stacked "><" icon in that same location on the status-bar. Or just enter the command-palette and "Remote-WSL: New WSL Window".

Related

VSCode always runs python interpreter in WSL

I selected the windows python interpreter in the top-left section of vscode:
Nonetheless, if I press the run-button on the top-right, a new WSL-Terminal is spawned trying to execute the windows interpreter.
I have the WSL for VSCode Extension installed, but it is not active (as I do not want to run the code using WSL):
Watching the terminal, one can see that the windows interpreter path I preiviously set is tried to be executed by WSL (as this last screenshot shows a bash-prompt) - how can I set this to run with Windows?
Perhaps this is the case because the setting terminal.integrated.shell.windows in the users settings.json was refering to WSL - I was not aware the setting existed before, so lesson learned, I guess.
fix:
"terminal.integrated.shell.windows": "cmd.exe"

WSL VSCode call Windows Python Executable

Context: Running python in VSCode on Windows
Default Terminal is Bash (via WSL)
Using WSL - Debian
Python 3 installed on Windows, not on WSL
I want Bash to call my Windows Python Executable when I run my python files. I am only using WSL so I can replace cmd/powershell with bash. I do not want to install anything on WSL, I want to use existing programs on Windows (in this case Python).
However when I try to run my Python file (Clicking 'play' button) I get the error:
-bash: C:/Users/Connor/AppData/Local/Programs/Python/Python310/python.exe: No such file or directory
I believe this can be solved by replacing C: with \mnt\c
How can I achieve this?
I had a same problem as yours. I solved this by following steps.
Open the VSCode
On your left-down side, you can see the the icon that I pointed enter image description here (the name in here is "Open a Remote Window")
Next you can see above the "Reopen Folder in WSL" enter image description here and clicked
Then you can run python with wsl and no more directory errors

My Visual Studio Code is not using the Python interpreter that I have set it to

I am currently working on setting up pytorch to be used on Visual Studio Code on my Windows 10 machine. Currently I can use it when I am running it on Anaconda prompt. When running on VS Code, I changed the python interpreter to match the one I am using on Anaconda, but when I try to run it is still using the original, and does not recognize "import torch". I am fairly new to using VS Code and running python on Windows, as I usually code in Linux, any help would be much appreciated!
I have attached a photo showing the current environment as well as the result when typing in "python --version" in the command line on VS Code. Screenshot
The reason is that the VSCode terminal is not refreshed and it is still in the previous state.
Solution:
We can use the shortcut key Ctrl+Shift+` (or you can also click "Terminal", "New Terminal") to open a new VSCode terminal, and it will automatically enter the currently selected Python interpreter environment.
In addition, if you need to import and use the module "torch", please install it in the currently selected environment.
Update:
Please check whether Python is available:
Please enter "where python" in the cmd window:
Please check whether there is a python path in the python environment variable:
Generally, for the python global environment, when we enter "python --version" in the terminal, the system will find the first python in the environment variables by default, so it is recommended that you put the commonly used python at the top of the environment variables and restart VSCode.
For conda environment and virtual environment, VSCode terminal will automatically enter the currently selected environment.
Reference: Environment in VScode.
You can click in Python 3.8.. Conda and then vscode shows a list of python interpreters, select your interprete and vscode creates a settings.json in the .vscode folder with the path of your python interpreter in the current folder open in vscode and everytime you open that folder vscode automatically detects your python interpreter.

How to launch PyCharm from terminal/command prompt

How can I launch PyCharm from terminal / command prompt?
I tried charm, charm., pycharm., pycharm.
None of these commands work.
Is it possible to open/launch PyCharm from a command prompt / terminal ?
This works on macOS (should work with other OSes too):
Open Application Pycharm
Find tools in menu bar
Click Create Command-line Launcher
Checking the launcher executable file which has been created in /usr/local/bin/charm
Open project or file just type $ charm YOUR_FOLDER_OR_FILE
Answer credit
From examining the shortcut on my Ubuntu desktop (on which I have the community version of Pycharm installed) the command it runs is "pycharm-community" So this works when run in a terminal, for example:
pycharm-community &
If you are using Windows, you can use the same trick to find out where Pycharm is installed (examining the shortcut on your start menu or desktop). For me, it is:
"C:\Program Files (x86)\JetBrains\PyCharm 2016.3.1\bin\pycharm64.exe"
Running that from a git bash prompt launches Pycharm for me.
You probably want to examine your own shortcut to find the correct location on your computer.
You can find the shortcut file from the Start Menu by right-clicking on the Pycharm entry and selecting Open file location. Or you can just look at the shortcut on your desktop if you have one there.
When you have located the shortcut file, right-click it, click Properties, select the Shortcut tab, and look in the field called Target: to see the location of the Pycharm executable.
If you are using ubuntu, go to pycharm/bin folder and run pycharm.sh file using below command
./pycharm.sh
Add path of PyCharm to your environment variables.
As user #Rob Brincheno metioned
When you have located the shortcut file, right-click it, click Properties, select the Shortcut tab, and look in the field called Target: to see the location of the Pycharm executable.
For the example, let's say the path is:
"C:\Program Files (x86)\JetBrains\PyCharm 2016.3.1\bin\pycharm64.exe"
Copy all to last part, don't copy pycharm64.exe
Read this for how to add PyCharm to your environment variables.
After you add it to path, from your cmd you can execute it as last part you didn't add, just type:
pycharm64
and it will start.
Redirect to your codebase directory and type pycharm-community . in the terminal
Works with Ubuntu, macOS, and Windows
This worked for me on my 2017 imac macOS Mojave (Version 10.14.3).
Open your ~/.bash_profile:
nano ~/.bash_profile
Append the alias:
alias pycharm="open /Applications/PyCharm\ CE.app"
Update terminal:
source ~/.bash_profile
Assert that it works:
pycharm
Something similar to what #timxor suggested, but without all of the output and leaving the terminal free to keep using...
Paste this in your .bsahrc file
pycharm () {
# opens pycharm with the correct param and detachs it from terminal
# change <pycharm.sh> with the location for pycharm.sh
# ~/.local/share/JetBrains/Toolbox/apps/PyCharm-C/ch-0/223.8214.51/bin/pycharm.sh in my case
nohup sh <pycharm.sh> $1 > /dev/null 2>&1&
}
(Mac solution)
If you just want to open pycharm from the terminal, you can put this command: open -na "PyCharm.app" in yours .bashrc as a alias.

Idle and Anaconda

I use to have idle. Then I downloaded Anaconda and opened idle through there. I have not used idle for a while but just recently went to go open it up and use it again. However, it seems I no longer have idle on my computer, to my understanding I can still get to idle through Anaconda but I forgot how. Is there a way to open idle through Anaconda without re-downloading idle? If so what command can I use to pull Idle up?
Search for this folder "idlelib" in the Lib folder of the Anaconda version you have installed and you will find idle (check its file type: "compiled python file").
Pin it to your taskbar and run.
You are looking for C:\... <\> ...Anaconda3\Lib\idlelib\idle.bat
There is also C:\ ... <\> ...Anaconda3\Scripts\idle.exe which seems to run a little smoother, but there has to be a Command Prompt open in the background to run. ... ?
Navigate to your Anaconda3 installation folder. Yours could be anywhere, mine is at "C:\ProgramData\Anaconda3", but I installed for all users and you could have selected a random location on your drive. If you managed to add it to the environment variables you could easily find it by running the Command Prompt (not Powershell - has to be Command Prompt) and typing where conda or where anaconda and pressing enter. You should find a folder named Anaconda3 or something of that nature.
If you navigate to this folder, and within it find the "Anaconda3\Lib\idlelib\" folder, within it there is a file called "idle.bat". If you find this "Anaconda3\Lib\idlelib\idle.bat" file you can double click it to run IDLE, although it may be a little messy/outdated/glitchy/strange since you are updating all kinds of folders within Anaconda and some of them may have helped idle.bat run. Mine has a the spyder icon associated with it in the task bar and a blotted out IDLE icon in the top left corner for some reason. You could of course run the full path in command prompt as well if you already know where it is, for instance I would run...
C:\Users\Thomas>C:\ProgramData\Anaconda3\Lib\idlelib\idle.bat
~or~
C:\Users\Thomas>C:\ProgramData\Anaconda3\Scripts\idle.exe
--> It may be easier to just download a new version of Python side by side with Anaconda to run IDLE. I have been attempting to set a shortcut in the Start Menu that runs from anaconda properly for days and have failed. This way, there should be a right click>'run with IDLE' option for python files.
I believe the tilde slash (~/) was meant to imply your home directory since that is what it signifies on a linux machine, but seeing as you could have put it anywhere on the machine, even directly on the C:\ drive, maybe he meant 'unknown file path'.
If you are running MacOs (currently I am on Catalina - MacOs10.15) it is quite simple to run IDLE from Anaconda environment:
Launch Anaconda
Go to MacOs Terminal.
Type IDLE3
warning: if you type IDLE, the Python 2 IDLE will be opened ... but if you are using Python3 installed with Anaconda ... it doesn't work properly.

Categories