Accessing and running Developer PowerShell using a Python Script - python

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.

Related

Python in Visual Studio Code without PowerShell

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"

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)

Run Visual Python script from bash terminal

I am trying to run the example programs that come with Visual Python ("VPython") like bounce.py, orbit.py, etc through bash.
When I'm in the folder which contains these examples (C:\Python27\Lib\site-packages\visual\examples), I can right-click on any of the examples and choose "open with" and then select "python.exe" from the list of given programs. This method is able to run the programs just fine and it generates the visual representation of a ball bouncing in a room, orbiting balls, etc.
On the other hand, when I use bash to navigate to this folder and then try to run python bounce.py, I get the error message:
Traceback (most recent call last): File "bounce.py", line 1, in
from visual import * ImportError: No module named visual
I was under the impression that calling python bounce.py was effectively the same as opening the file with the python executable but the behavior is different. I'm not sure why the "open with python.exe" method would allow the example to find the visual module but opening the example through bash is unable to find the same module. Any advice or help would be appreciated.
(Side note, I am running Bash on Ubuntu on Windows through the new beta https://msdn.microsoft.com/en-us/commandline/wsl/about)
The Linux environment (Bash on Windows) includes a builtin python, which does not have the package visual.
If you double-click the file, right-click and then open with python.exe, or execute the script via cmd or powershell, it will use the Python installed on the Windows machine. If you execute the script in bash, it will use the Python in Linux subsystem. That's it.

Microsoft Visual Studio Properties

Recently cloned the spyder.py source from github searching for a python api; been going through the files in Powershell looking for a desktop python editor to edit code and run tests.
Most of the files run so far (using the '.\ -filename.file extension-' command) have opened windows briefly on screen which then exited.
Would anyone with experience on Spyder care to explain the method for best editor use (or the file to run to get access to the editor)?
Respectfully,
HeiwajimaSidekick22

Categories

Resources