Python in Visual Studio Code without PowerShell - python

So I need to use Python for my work. Unfortunately, since recent hacks, the companies security policies are very strict and there is no way I'm getting admin rights. I managed to persuade our IT to install Python, Visual Studio Code, and the Python extension for it on my computer.
If I try to run python commands in the Python interpreter it works. But when I try to run a Python script in Visual Studio Code it hast to run Power Shell which is also blocked for security reasons.
I get the following error:
The terminal process command 'C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe' failed to launch (exit code: {2})
Is there some way around this? To use Python in Visual Studio Code despite all of the security restrictions?
I tried asking our IT department but they have no idea how to help me...
Thank you in advance.

You can change the VS Code settings and tell it to not use PowerShell as shell in the integrated terminal.
For example, I have put Windows Terminal in there, but you can also use CMD or any other shell such a git bash as long as it's available in your system.
Press ctrl+shift+p and type/select Open Settings (JSON). Then add some of the following to this configuration file, and save.
For Windows Terminal:
"terminal.external.windowsExec": "C:\\Users\\<your-username>\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"
For CMD:
"terminal.external.windowsExec": "C:\\Windows\\system32\\cmd.exe"

I'm used below code in setting file and it works.
"terminal.integrated.defaultProfile.windows": "Command Prompt"

Related

Unable to Run Python by Code Runner on Visual Studio Code on MacOS

Facing issues with Vsc Code Runner, unable to run python code using Code Runner. Works fine when clicked on Run Python Code button
I get an error
zsh: no such file or directory: /usr/local/bin/python3.9.2
I have identified the error, need to change the run command from
python -u
to
python3 -u
How do I do that?
First, make sure that you have installed the python extension. Secondly, this should be caused by the error of interpreter path recognition. You can modify it manually.
Type ctrl+shift+P and then choose “python:select Interpreter”.
Check that the Python interpreter has selected the correct one, and then run the file again.

Accessing and running Developer PowerShell using a Python Script

I am trying to create a python script to automatically run unit tests in Visual Studio. One path that I am exploring would involve using shells, in particular Developer Powershell. I am running into a roadblock with accessing this shell, though. Here's what I have so far:
First off, the Visual Studio solution has all of the necessary files; I just need to build and run, which I can do in Developer Powershell using the following lines
> MSBuild.exe .\project.sln
> VSTest.Console.exe .\x64\Debug\project.dll /Logger:trx
To get to Developer Powershell, I can type the following into a normal Powershell:
> C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -noe -c "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 64375397}"
And if I need to start from CMD for some reason, I can get to Developer Powershell by simply typing this:
> %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\Tools\VsDevCmd.bat"
My issue is trying to put all of this in a python script. I can open Developer Powershell using os.system, but then I can't use os.system again until I exit Developer Powershell. Is there a way to let the python know that I am in a new shell and to start interacting with that one?
Okay, I figured it out, but I'll just leave this all up in case anyone has a similar question:
MSBuild and VSTest.Console are both just .exe files, and as such just have their own locations that you can target in a shell (links on where to find the are below).
MSBuild
VSTest
If you just run > & "C:\<The_path>\MSBuild.exe", it'll work fine with the arguments above. No Developer Powershell needed.
You can even use this stack exchange question to generally find the .exe files on any Windows system.

Terminal showing path while debugging Python code in VS Code

I am just a newbie and whenever I debug my code. This shows up with my output. How can I hide these unnecessary paths.
PS C:\Users\apoor\Desktop\Python\oops> & C:/Users/apoor/AppData/Local/Programs/Python/Python39/python.exe c:/Users/apoor/Desktop/Python/oops/overridingMethods.py
Thanks!
Please set it in "launch.json" in Visual Studio Code:
"console": "internalConsole",
It will display the debugging results on the Visual Studio Code internal console and not display the path.
Update:
At present, there is no direct way to hide the running path of the internal terminal of VSCode, because it integrates the powershell or cmd terminal from the computer, we need to specify the Python path used and the path to execute the script for it.
In addition, you could also try to use VSCode's extension "Code Runner".
More reference: Console in Visual Studio Code.

Why doesn't Visual Studio Code recognize my WSL bash python?

I am running Visual Studio Code on Windows 10 with WSL bash as its terminal:
"terminal.integrated.shell.windows": "C:/Windows/sysnative/bash.exe"
In WSL bash I have both python (v2.7) and python3 (v3.6) installed.
I have installed the Python extension for VS Code. Unfortunately VS Code fails to recognize either version of Python in WSL bash.
For example when I try to discover tests, I get the following error in the Python Test Log output window:
Test Discovery failed:
Error: spawn python ENOENT
How can I configure VS Code to work with Python form WSL bash?
Note: I'm pretty sure I need to configure a path to Python in the "python.pythonPath" key in VS Code's user settings file. I am unsure of which path to put as the value though.
Support for WSL is done through the "WSL - Remote" extension. There is a WSL blog post which covers how to get started.

How to run Python3 function script in terminal Visual Studio Code

so far I have been ok with debugging and running in a python debug terminal. Now that I'm being forced into functions, i have to input the arguments when launching the script, for example:
./python.py BIRTHDAY AHMED
and have it spit out something along the lines of this:
have a happy BIRTHDAY enjoy the day AHMED
problem comes down to me not knowing how to setup a terminal for such use. i have a bash terminal, however it wont recognize my python interpreter (#!/usr/bin/python3).
I added "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe" to my user settings to gain the bash terminal as well as installed Git.
Any ideas on how i can get a work in terminal that i can input arguments and recieve an output would be most appreciated. (running on windows 10)
You are providing the path of Git Bash, which only provides enough bash-like functionality to use git. That is why running .sh (Bash Scripts) or .py files doesn't work by default.
If you add python to your PATH environment variable, that will make it so running python.exe filename.py [ARGS] will work on the Integrated Terminal.
Another solution is to install WSL if you are on Windows 10, which does provide full* bash functionality under a modified Ubuntu shell. Then just provide VSCode the path to the WSL bash.exe. (How to here)

Categories

Resources