Configure VS code to use Python, R and C/C++
Configure Visual Studio code to use familiar softwares
- such as
Python
,R
andC/C++
- More details can be found https://code.visualstudio.com/
Configure VS code to use Python
-
Prerequisites
- VS Code
- VS Code Python extension
- Python 3
-
Install the Python Extension
- install the
Python extension
for VS Code from the Visual Studio Marketplace. The Python extension is namedPython
and it’s published by Microsoft.
- install the
-
Install a Python interpreter
- Suppose you already had installed
Python 3
- Suppose you already had installed
-
Start VS Code in a project (workspace) folder
- Using a command prompt or terminal, create an empty folder called “hello”, navigate into it, and open VS Code (
code
) in that folder (.
) by entering the following commands:mkdir hello cd hello code .
- Using a command prompt or terminal, create an empty folder called “hello”, navigate into it, and open VS Code (
-
Create a Python Hello World source code file
- From the File Explorer toolbar, select the
New File
button on thehello
folder - Name the file
hello.py
, and it automatically opens in the editor - Now that you have a code file in your Workspace, enter the following source code in
hello.py
:
data = [('SUFE','Shanghai University of Finance and Economics','8000'),('MSU','Michigan State University','30000'),('NWU','Northwestern University','9000')] tmp1 = [] tmp2 = [] for i in range(3): tmp1.append(data[i][0]) tmp2.append(data[i][2]) d = dict(zip(tmp1,tmp2)) print(d)
- From the File Explorer toolbar, select the
-
Run the codes
- It’s simple to run
hello.py
with Python. Just click theRun Python File
inTerminal
play button in the top-right side of the editor. - There are three other ways you can run Python code within VS Code:
- Right-click anywhere in the editor window and select
Run Python File in Terminal
(which saves the file automatically) - Select one or more lines, then press
Shift+Enter
or right-click and selectRun Selection/Line in Python Terminal
. This command is convenient for testing just a part of a file. - From the Command Palette (
Ctrl+Shift+P
), select thePython: Start REPL
command to open a REPL terminal for the currently selected Python interpreter. In theREPL
, you can then enter and run lines of code one at a time.
- Right-click anywhere in the editor window and select
- It’s simple to run
-
More details can be found HERE
Configure VS code to use C/C++
-
Prerequisites
- VS code installed the
C/C++
extension. Mingw-w64
, and add the path to yourMingw-w64
bin folder to the Windows PATH environment variable
- VS code installed the
-
Create first project
- create an empty folder called projects where you can place all your VS Code projects.
- open VS Code in that folder by entering the following commands:
mkdir projects code .
-The “code .” command opens VS Code in the current working folder, which becomes your “workspace”.
-
Add a source code file
- select the
New File
button and name the filehelloworld.c
. - Copy and paste in this source code:
// Collatz Problem #include<stdio.h> int chainLen(int n){ int len=1; while(n>1){ if(n%2) n=3*n+1; else n=n/2; len++; } return len; } int main(){ int n; printf("Input a natural number n = "); scanf("%d",&n); printf("The length of chain is %d\n",chainLen(n)); return 0; }
- select the
-
Build
helloworld.c
- Next, you will create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the
g++
compiler to create an executable file based on the source code. - From the main menu, choose Terminal > Configure Default Build Task.
- Choose g++.exe build active file, which will build the file that is currently displayed (active) in the editor.
- Next, you will create a tasks.json file to tell VS Code how to build (compile) the program. This task will invoke the
-
Running the build
-
Go back to
helloworld.c
. Your task builds the active file and you want to buildhelloworld.c
. -
To run the build task defined in
tasks.json
, pressCtrl+Shift+B
or from the Terminal main menu chooseTasks: Run Build Task
. -
Create a new terminal using the + button and you’ll have a new terminal (running PowerShell) with the
helloworld
folder as the working directory. Rundir
and you should now see the executablehelloworld.exe
. -
You can run
helloworld
in the terminal by typing.\helloworld.exe
.
-
-
Start a debugging session
- Go back to
helloworld.c
so that it is the active file. - Press
F5
or from the main menu chooseRun > Start Debugging
.
- Go back to
-
More details can be found HERE.
Configure VS code to use R
- Using a command prompt or terminal, create an empty folder called “hello”, navigate into it, and open VS Code (
code
) in that folder (.
) by entering the following commands:mkdir hello cd hello code .
- Create a
R
“Hello World” source code file.- From the File Explorer toolbar, select the
New File
button on thehello
folder. - Name the file
hello.r
, and it automatically opens in the editor.
msg = "Hello world!" print(msg)
- It’s simple to run
hello.r
withR
. Just click theRun Source
in Terminal play button in the top-right side of the editor, or pressCtrl+Shift+S
.
- From the File Explorer toolbar, select the