WSL VSCode call Windows Python Executable - python

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

Related

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

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".

Python is not installing on Windows 10

I tried to install Python to Windows 10, with the PATH installation included. However, when I click to enter in the Python application, it just appears the setup. I need to access the Python terminal.
Here is the setup
Make sure you are not accidentally opening the Python installation executable. Check again that the Python path is installed properly and try running the command "py" or "python" in the Command Prompt to see if it starts. If it doesn't start and Python is installed on your computer, manually add Python's install location to your PATH.

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.

How to control what version of Python is run when double clicking a file?

Is there a way to control what version of python is run when double clicking on a py file? From the command line and in environments such as eclipse I can control what version is run. But from double clicking I am not sure.
I have 2.6 and 2.7 installed. 2.6 is for some application specific stuff and I want to make 2.7 the default. I have added "C:\Python27" to the PATH environment variable and that works well at the command line. C:\path\to\some\file>python someFile.py will run the file in 2.7. But if I double click the same file from explorer it runs 2.6. How to get it to run 2.7?
On Windows, you have to modify the file associations, for example via Right Click → Open with ... → Choose default program or the Control Panel's Folder Settings. You can choose between multiple python installations by navigating to the python.exe you want via the Browse button:
Alternatively, you can change the association in a command shell by typing
ftype Python.File="C:\Python27\python.exe" "%1" %*
Note that this requires administrator rights. If UAC is enabled on your machine, right click cmd in the start menu and select Run as administrator.
On freedesktop.org-compatible desktops, you can configure the association with xdg-mime.
On debian-based distributions, you can change the default python with update-alternatives. On all systems, you can also symlink the python in your path to the correct implementation, like this:
$ sudo ln -sf python2.7 /usr/bin/python
If the file is marked executable, it can also be executed directly from the command line or GUI if it starts with #! and the name of the interpreter:
#!/usr/bin/env python
To choose a specific Python version just for your program, you can start your Python program with one of the following lines:
#!/usr/bin/env python2.7
#!/usr/bin/python2.7
OK I have found the Python Launcher, which does exactly what I am after. Download can be found here. Installing this gave me the option for "Python Launcher for Windows (GUI)" when changing the file association via the right click menu.
Adding the shebang line
#!/usr/bin/python2.7
forces the script to run in 2.7.
This works great as I can control what version of python is running and users never need to know. No need for bat files, or dragging onto shortcuts etc. Nice and clean, and most importantly, no room for user error.
You can use ASSOC and FTYPE
assoc .py=pyfile
ftype pyfile=c:\Python27\python.exe %1
You will find answer in answers to the following questions:
How to execute python scripts in windows?
Using older Python 2.x while having both Python 2.x and 3.x installed on Windows
Temporary file association for single cmd.exe session
How to write a batch file showing path to executable and version of Python handling Python scripts on Windows?

Categories