Let's say you have a Python script that prompts the user for their name and then writes "Hi, {Name}"
Now in PowerShell this is super easy, I can create a custom function, throw it into my PowerShell $Profile, and set an alias (and call it something like Get-Name). Then, in VSCode I can simply type "Get-Name" without having to search for the program, open it, fiddle with it, etc. Meaning I can have any file open in VSCode and type Get-Name and it always works. As you can imagine, this is super fast and convenient.
How do I do the same thing with Python? Suppose the file is in c:/code/name.py, what would you do? Again the goal is so I don't have to constantly navigate through the folders, find the file, open it, click run, and then use it.
Videos
How do I switch from powershell to python
And how do I run my code using the Terminal because it just doesn't put anything
And why when I use the run command it doesn't print hello
[SOLVED!!!]
Every time a call the function in the "vs studio" terminal it always gives me this error.
PS C:\Users\Firstname\python> telephonebook() //the function i try to call is "telephonebook()" in python
At line:1 char:12
+ telephonebook()
+ ~
An expression was expected after '('.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedExpression
the thing is, i only get this error in the "vs studio" editor. If i call the function in IDLE python, it works just fine. where the way you navigate in my telephonebook is working as expected.
is there a specific way to call a function in "vs studio" terminal?
[SOLVED!!!]
There is nothing wrong with your setup. It looks you miss basic understanding of different ways to execute python code.
VS Code has integrated terminal. From there You can run your python script i.e. file with py extension same as you are on terminal/cmd/powershell. That is common way to write and execute code.
When you type python and hit enter you start python interactive shell. That is when you get >>> prompt. Your python interpreter evaluate and execute each line as you type in and hit enter. Same will happen if you type in python in cmd/powershell outside VS Code. Interactive mode is used more or less to experiment, test simple ideas, simple code examples, etc. but the code you type in is lost once you exit the interactive mode by >>>exit().
for further reference you may check
- VSCode from start
- How to execute python code
After reading the documentation above, I also had a similar problem after running scripts. I think that VSC is exiting out of python after running a script and requires starting up the python terminal again. I wonder if VSC can default to python after running a script instead of exiting out to powershell.
HI, How do I stop the Python interpreter from trying to run my Powershell scripts when I hit F5?
I am learning Powershell and Python.
To run my Powershell I was hitting F5.
Then I started learning Python. When I go back to a Powershell script, and hit F5, I see in the debug console that its trying to parse my Powershell script with Python.
I can run the Poweshell script by calling it on the command line, but I'd sure like to be able to F5 and step through it.
Thanks
Here's my settings.json:
{
"python.pythonPath": "C:\Users\HankC\AppData\Local\Programs\Python\Python38-32\python.exe",
"terminal.integrated.shell.windows": "C:\Program Files\PowerShell\7\pwsh.exe",
"workbench.activityBar.visible": true,
"debug.openDebug": "neverOpen",
"editor.tabCompletion": "on",
"powershell.integratedConsole.focusConsoleOnExecute": false,
"files.defaultLanguage": "powershell",
"editor.wordSeparators": "`~!@#%&*()-=+[{]}\|;:'",.<>/?",
"powershell.buttons.showPanelMovementButtons": true,
"files.associations": {
"*.ps1": "powershell"
},
"editor.fontSize": 16,
"git.autofetch": true,
"git.confirmSync": false,
"window.zoomLevel": 0,
"workbench.colorTheme": "Default Dark+",
"python.terminal.executeInFileDir": true,
"editor.minimap.enabled": false,
}
ok i just installed vs code like 10 minutes ago and i wanna try to do something with my mouse but when i was watching a tutorial they used py .\control.py in terminal, but when i try i get an error. how do i use it?
(edit, heres the error)
py : The term 'py' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ py .\control.py
+ ~~
+ CategoryInfo : ObjectNotFound: (py:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I'm trying to debug using the built in pdb vscode debugger, but the command the debugger uses to start debugging is for cmd, when my default integrated terminal in vscode is WSL. How can I configure it so it runs in the WSL terminal?
Here is a screenshot of what I mean. After I press the run button, it gives the cmd command to run python and not the bash command.
Yeah, it's an issue of VSCode In version v1.60.0. You can refer to GitHub issue #132150 for more details.
There are a lot of people report this kind of problem recently.
And the only workaround to this problem is to roll back the version to v1.59 until the release of v1.60.1 for now.
There might be an issue in your select default profile settings. However, you can always choose a default shell by pressing ctrl + ~ (~ tilda key) You can open up a shell in vscode, as seen below. By default this might open up powershell, from there you can select your default shell.


Note: Some of the options seen in the image require WSL2 (Windows Subsystem for Linux) to be installed and activated on any Windows system. Clicking any of those options will open a new tab of that shell, but if you click on the '+' plus button or restart vscode later, it will go back to the default shell (powershell) in our case.

Now click the option on the bottom that says select default profile And you should see a window like this:
From there you can select the default shell in this window. And these changes will be saved, so anytime you click the '+' plus button to open a new shell instance, or relaunch vscode it will open your selected shell choice by default.

And that's it! From here you can select cmd since you want that to be your default shell. And you should be good to go. You can change these settings at anytime, by following these steps again.
Since you're using Python. I suggest using powershell for one reason. You can use a tool known as chocolatey which is like a package manager for windows that can download, install/update, and most importantly set environment variable paths in windows automatically. Which might be a lot easier than doing it manually on cmd.
To run the python script you can do the following:
Copy$ python /path/to/file.py