How to launch PyCharm from WSL terminal? - python

Is there a way to run Pycharm from wsl terminal by typing the command like charm <file_name> or pycharm <file_name>, just like it is with vscode where you type code <file_name> ?

From this article on How to run Windows 10 programs in a WSL Linux shell, you can already run Windows programs from WSL ex. notepad.exe. This is because the program in the WSL path seems to be in sync with Window's path. You can add the path to the PyCharm executable to the path variable, C:\Users\trakw\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\221.5921.27\bin, restart WSL and run pycharm64.exe which start PyCharm.
If you're using JetBrains Toolbox to install PyCharm, C:\Users\trakw\jetbrains\bin, containing the batch file pycharm.cmd to run PyCharm, is already in the path variable.
Running pycharm.cmd in WSL will give errors because the .cmd file is written in batch which WSL doesn't recognize.
You need to run it using cmd.exe /c pycharm.
You can add the modified code below to ~/.bashrc file in WSL, so you can run cmd pycharm ..
# Usage : cmd pycharm .
cmd() {
CMD=$1
OPEN=$2
WIN_PWD=`wslpath -w "$(pwd)"`
WIN_OPEN=`wslpath -w ${OPEN}`
pushd /mnt/c;
cmd.exe /c "${CMD} ${WIN_OPEN}"
popd;
}
# Disable pushd echoing
pushd () {
command pushd "$#" > /dev/null
}
# Disable popd echoing
popd () {
command popd "$#" > /dev/null
}
There is still problem. This cmd pycharm . in WSL works with projects in \\wsl$ created in PyCharm. For non-project folder in \\wsl$, the error below will show up. However, pycharm . in cmd, works with any directory.

Yes. It's possible, if you using Windows 11:
https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Related

Activate python virtualenv and runserver in bat file with windows terminal

I'm trying to create a bat file to open windows terminal, activate an environment and run a server. After some attemps the best I've got is this:
wt.exe cmd -NoExit -c "c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1;cd C:\Users\me\Desktop\myProyect\;python manage.py runserver"
But windows terminal activate the environment in a tab and in other tab throw this error:
[error 0x80070002 when launching `"C:\Users\me\Desktop\myproyect\;python manage.py runserver"']
I think that maybe the command after the environment activation is not properly 'passed' to the environment... but really I don't know how to solve it.
-NoExit -c both look like powershell flags, not cmd ones. That might be a place to start.
I'd recommend just taking the entire script you've got there:
c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1;cd C:\Users\me\Desktop\myProyect\;python manage.py runserver"
and putting it into a .bat file like:
powershell -f c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1
cd C:\Users\me\Desktop\myProyect\
python manage.py runserver"
then running that with wt.exe foo.bat

How to fix this 'cmd' not recognized error to carry out the debugging in VSCode?

While debugging cmd is not recognized is displayed and program is not debugged.
What can be the problem?
I have already checked the path and pythonpath variables and those seem to be just fine
bash
C:\Users\rahul\Desktop\vscode\.vscode>cd c:\Users\rahul\Desktop\vscode\.vscode &&
cmd /C "set "PYTHONIOENCODING=UTF-8" &&
set "PYTHONUNBUFFERED=1" &&
C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\python.exe c:\Users\rahul\.vscode\extensions\ms-python.python-2019.6.22090\pythonFiles\ptvsd_launcher.py --default --client --host localhost
--port 50265 c:\Users\rahul\Desktop\vscode\.vscode\s.py "
'cmd' is not recognized as an internal or external command,
operable program or batch file.
TL;DR: cmd is not in your Windows Environment Path.
add %SystemRoot%\system32 to your System Variables and restart VSCode.
Visual Studio Code has actually brought native support for selecting your terminal, so including cmd into your path is nolonger necessary.
Press CTRL + SHIFT + P -> Terminal: Select default shell -> select your terminal.
It will add this line to your settings.json:
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe"
should have appeared.
Or if you chose Powershell, it will look like this:
"terminal.integrated.shell.windows": "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
To view your settings.json file, simply:
Ctrl + , scroll down to Files: Associations and click Edit in settings.json.
It means that cmd is not in your path. Either:
Add the path to the system or user variables in the control panel
Use the full path to cmd instead (typically C:\Windows\System32\cmd.exe), meaning something like:
cd c:\Users\rahul\Desktop\vscode\.vscode && C:\Windows\System32\cmd.exe /C "set "PYTHONIOENCODING=UTF-8" && set "PYTHONUNBUFFERED=1" && C:\Users\rahul\AppData\Local\Programs\Python\Python37-32\python.exe c:\Users\rahul\.vscode\extensions\ms-python.python-2019.6.22090\pythonFiles\ptvsd_launcher.py --default --client --host localhost
--port 50265 c:\Users\rahul\Desktop\vscode\.vscode\s.py "
If cmd is in your Windows Environment Path, that means that, probably, your default integrated shell is set to wsl bash.
Change it and set
"terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe"
in your settings json
You may need to restart VSCode for this to take effect.

How to prevent command prompt ( windows ) from opening file when typing command?

I download source code of my app from google appengine using this command in cmd:
appcfg.py download_app -A <your_app_id> -V <your_app_version> <output-dir>
But, instead of running the command, it opens the file "appcfg.py". So I don't know what to do now.
Sounds like python files are associated with an editor instead of with the python interpreter.
If so you'll have to change the associations for .py files (found in folder options), or call the python interpreter:
C:\path\to\python appcfg.py download_app -A -V

Bash script that runs during terminal startup not working

This is a snippet of code inside by .profile which is run during startup. I am not too sure why it is not source activate py33dev (which switches from default python 2.7 to 3.3) and cd to desktop does not work, even though the first cd and echo works.
cd /Users/anaconda/envs/py33dev/bin
echo 'source activate'
source activate py33dev
cd ~/Desktop/
.....other bash script commands that work....

Running "IDLE3.2 -s" from the "Finder" in OS X 10.6

I want to run IDLE3.2 with the argument "-s" so it can read ".pythonstartup" and export relevant modules, change the working directory and etc. Here is what I have tried:
Created a shell script:
/usr/local/bin/idle3.2 -s
this works allright, however running the script from the Finder opens up the Terminal, which is not the desired behavior.
Created an applescript:
do shell script "/bin/bash; cd /usr/local/bin/; ./idle3.2 -s"
this get rids of the terminal however fails to pass "-s" argument to idle3.2 so the configuration file is not loaded.
any suggestions?
EDIT: turns out environment variables are not properly set even though /bin/bash is called. so the following solves the problem:
do shell script "/bin/bash; source ~/.profile; /usr/local/bin/idle3.2 -s"
I think your do shell script "/bin/bash; cd /usr/local/bin; ./idle3.2 -s" is doing extra work, and can probably be done more simply. Try:
do shell script "/usr/local/bin/idle3.2 -s"
thanks to #lain the following applescript solves the problem:
do shell script "source ~/.profile; idle3.2 -s"
where ~/.profile points the shell (in this case /bin/sh) the path for .PYTHONSTARTUP and the path for idle3.2

Categories

Resources