Reputation: 421
I downloaded and installed Visual Studio along with Anaconda to get access to all of the packages that come pre installed with Anaconda. I am trying to figure out how to run code such that it runs in the interactive shell. Right now when I hit F5 an Anaconda 3 cmd line window comes up with the prompt "Press any key to continue..." comes up. My question is: how can I make it so that when I hit F5 my code is executed in the interactive Python shell much like it does on the basic IDLE that comes with Python.
This seems like a question that a simple Google Search could fix, but for some reason I cannot find the answer. I've done some google searching, and I watched the Visual Studio python official Microsoft series about it. One of the videos touched on using the interactive shell, but even in the video, when he clicked the Start (Run) button, the code ran in what looked like the command line.
I have used IDLE in the past, and now I think it is time to make the change to a bigger IDE. I love the code completion and templates of visual studio, and I can't wait to solve this (noob) question.
Thanks
Upvotes: 2
Views: 11328
Reputation: 1681
To make a same experience like in c# where you can use F5 to start debugging python in Visual studio 2017 , you need to
1) create a new visual studio project ( ctrl + shift + N)
2) Select python as project type
3) Now you can create new python file ( *.py) and start code python ( ctrl + N)
4) Now you can right click the py file that you just created and use "set as startup file" command
5) Hit F5 to start python IDE debug experience ( breakpoint , inspect value , data type etc) , see below screen shoot
Upvotes: 0
Reputation: 11221
I'm not sure if it's the best way to do that, but here is what I do to quickly run Python script in Interactive Shell in Visual Studio 2017:
#%%
sequence at the beginning of *.py file#%%
to end of file (or another #%%
) will appear on Interactive Window#%%
is basically beginning of the cell. Cell is a part of code you would want to run at once in Interactive Shell. Cell begins with #%%
and ends with another #%%
which initializes another cell.
For example: you have following code in Visual Studio:
#%% Cell 1
print("Hello world1")
print("Hello world2")
#%% Cell 2
print("Hello world3")
When you click/focus on third line and press Ctrl+Enter
you will run second and third line in Interactive Window.
Upvotes: 0
Reputation: 2559
Add
import os
os.chdir(r"C:\My\script\\path\")
to the top of your script.
Then Shift+Alt+F5 works as expected.
Upvotes: 1
Reputation: 4381
I am struggling with this as well. There is a Visual Studio Shell command execute file in Python interactive which is bound to Shift+Alt+F5 by default.
This works: if the focus is in a code window then the current file is executed. If the focus is in the Solution Explorer window, the file selected as "Startup item" is executed. There seems to be a glitch however: Some import statements from the specific file which work fine on the standard Ctrl+F5 will fail on Shift+Alt+F5. I need to figure out why this is the case and will report here.
EDIT: Once in the interactive windows, change the working directory to the folder containing the project: os.chdir
etc. Then import your-filename
works flawlessly. So I assume that there is some problem with selecting the working directory while executing Shift+Alt+F5.
Upvotes: 7