VS Code
We will use VS Code as our main code editor throughout this course. Python will be the main programming language used in this course. In previous section, we have connected to Vanda cluster and launched VS Code via OnDemand.Please follow the instructions below to set up your programming environment.
Create a course directory¶
It’s a good practice to create a dedicated directory for your course work. By default, you will be in your home directory when you log in. You can create a new directory by clicking the new folder icon in the file explorer on the left side of VS Code and then name it to something like MLE4217_5219.
Open a directory¶
Then you should open this directory in VS Code by clicking on File > Open Folder... and selecting the folder you just created.
Install Python and Jupyter extensions in VS Code¶
To work with Python and Jupyter notebooks in VS Code, you need to install the following extensions:
Open the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window.
In the search bar, type
Pythonand install the extension namedPythonby Microsoft.Similarly, search for
Jupyterand install the extension namedJupyterby Microsoft.
Creating Python Virtual Environments¶
A virtual environment is a self-contained directory that contains a Python installation for a particular version of Python, plus a number of additional packages. This allows you to work on a specific project without affecting other projects or your system Python installation.
To create a virtual environment for this course, press Ctrl/Command+Shift+P to open the command palette in VS Code, you will see a prompt like this at the top of the window:
> Then type Python: Create Environment and select it. Then choose Venv as the environment type, and select the base Python interpreter. Then it will create a folder .venv in your current directory. When you create a new Python file or Jupyter notebook, VS Code will automatically detect and use this virtual environment.
Select Interpreter¶
If you’re editing any Python code (file ends with .py), you should select your Python interpreter for your project in VS Code. Make sure you installed the Python extension in VS Code. Here’s how you can do it:
Open VS Code and press
Ctrl/Command+Shift+Pto open the command palette.Type
Python: Select Interpreterin the command palette. As you type, you will see a list of options appear. SelectPython: Select Interpreterfrom the list. The prompt will look like this:
> Python: Select InterpreterAfter selecting
Python: Select Interpreter, you will see a list of available Python interpreters. Look for the interpreter you just created. It should be named something likePython 3.9.18 ('./.venv/bin/python').Select the appropriate interpreter.
Once selected, you should see the interpreter path at the bottom left corner of VS Code, indicating that it is now using the specified Python environment for your project.
By following these steps, you ensure that VS Code uses the correct Python interpreter, which is essential for running your code and managing dependencies correctly.
Install Required Python Packages¶
Python packages are libraries that provide additional functionality for Python. In this course we will use pip to manage Python packages. When installing packages, make sure your virtual environment is activated. You can check this by looking at the terminal prompt, which should show the name of your virtual environment, e.g., (.venv).
The command to install packages is:
pip install package_nameYou need to install some Python packages that we will use in this course. You can do this by opening a terminal in VS Code (Terminal > New Terminal) and running the following commands:
pip install numpy matplotlib pandas jupyter pymatgen ase dscribe mace-torch asap3 atomate2 mp-api line_profilerUpload and Download Files via VS Code¶
You can easily upload and download files between your local machine and the Vanda cluster using the built-in file explorer in VS Code. For download, just right-click on the folder or file you want to download and select the appropriate option. For uploading files from your local machine to Vanda, you can also simply drag and drop files into the file explorer in VS Code.
*Setting Up GitHub Copilot in VS Code¶
GitHub Copilot is an AI-powered code completion tool that helps you write code faster and with fewer errors. Here’s how you can set it up in Visual Studio Code:
Set Up GitHub Copilot¶
Install the GitHub Copilot Extension:
Open Visual Studio Code.
Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing
Ctrl/Command+Shift+X.In the search bar, type
GitHub Copilot.Click on the
GitHub Copilotextension from the list and then click theInstallbutton.
Sign In to GitHub:
After installing the extension, you will need to sign in to your GitHub account.
Click on the
Sign in to GitHubbutton that appears in the notification or go to the Accounts view and sign in from there.Follow the prompts to authorize Visual Studio Code to access your GitHub account.
Enable GitHub Copilot:
Once signed in, GitHub Copilot should be enabled automatically.
You can check if it is enabled by looking for the Copilot icon in the status bar at the bottom of the VS Code window.
Using GitHub Copilot¶
Code Suggestions:
As you type, GitHub Copilot will provide code suggestions. These suggestions appear as grayed-out text.
To accept a suggestion, press
Tab. To see more suggestions, pressCtrl/Command+Space.
Inline Code Completions:
GitHub Copilot can complete entire lines or blocks of code based on the context of your current file.
Start typing a comment or a function, and Copilot will suggest the rest of the code.
Documentation and Examples:
You can ask GitHub Copilot to provide documentation or examples by typing a comment describing what you need.
For example, typing
// example of a function to calculate factorialwill prompt Copilot to generate a relevant code snippet.