Setting up VS code with Github and Python

Setting up VS code with Github and Python

This will cover setting up the Visual Studio Code for running and debugging Python code and with GitHub repositories.

Installations

  • Download VSCode

    • For the installation make sure to select these boxes:

image-20250131-195900.png
  • Download Git

    • For the installation make sure Git’s default editor is set to Visual Studio Code

    • If done correctly, you should be able to enter git in the command prompt

image-20250131-200426.png
  • Download Python

    • For the installation make sure that Python is added to your system’s environment variables

    • Optional: Change the default path to a location that is easy to manage

    • If done correctly, you should be able to open a command prompt and type py to start python

image-20250131-200958.png

Add your username and email to Git

  • Open up a command prompt and type these commands to add your username and email to the Git configuration

    • git config --global user.name your_github_username git config --global user.email your_email

Clone GitHub repository to your computer and add your Git credentials

This can be done several different ways, I will show you my preferred method. You will need the repository’s link, for example:

image-20250131-202324.png
Link to a repository

 

  • Make a folder somewhere, I usually use C:\Users\username\Documents\Github

    • This is where your list of cloned repositories will go

  • Open the command prompt at this directory by typing cmd in the directory address bar and hitting enter

cmd_dir.gif
  • Type the following command to clone the repository to your local folder:

    • git clone https://link-to-repository.git
    • This will likely prompt a login screen. Click on Sign in with your browser and sign in to your GitHub account

image-20250131-203640.png
  • This will clone the repository to a local folder on your computer while being linked to the remote repo. This allows you to sync changes with the repo (as long as you have those privileges for that repository)

Create a Python virtual environment

This step is somewhat optional, but HIGHLY recommended. Virtual environments can be thought of as containers that hold an instance of your Python installation. This is helpful because it allows you to install and manage modules/libraries for specific projects in containers rather than your global Python installation. So if something is weird with a certain module, you can just delete the virtual environment without uninstalling all of Python etc.

  • I usually keep my environments in a C:\ folder, but if you have multiple users on your computer, you may want a different location.

    • Create a folder to store your python environments (mine will be called PythonVenv_3.12). In this folder make another folder for the virtual environment (mine will be called venvTest)

image-20250131-204537.png
  • In your environment folder (venvTest) open a command prompt

  • In the command prompt enter the following command:

    • py -m venv ./
    • This should make the following folders in your environment folder (venvTest)

image-20250131-205101.png
  • A few notes:

    • In the Scripts folder there will be a python.exe which is a wrapper for your python installation.

    • The Lib folder will contain your libraries

  • Now in the same command prompt as before type:

    • cd Scripts activate
  • This will activate your virtual environment in this command prompt. You should see the name of your virtual environment in parenthesis:

image-20250131-205620.png
  • Now if you use python commands such py or pip it will refer to your virtual environment in this command prompt window

Setting up VS Code (combining everything)

This is how I usually set up my VS Code. You can experiment with other settings etc, but this is what has worked best for me. For this setup, I will be using BlackBox_RCWA as my repository.

  • In your repository folder, right click in the empty space, click Show more options and then click Open with Code. This opens this folder in VS Code

open_vscde.gif
  • Since this is a git repository, you should see this icon which opens up a tab. This tab shows you your local changes, remote changes etc.

image-20250131-210509.png
  • Navigate to the extensions icon

image-20250131-210646.png
  • Install the Python extension by Microsoft

    • You can also install Jupyter which allows you to make notebooks, but this won’t be covered in this article

  • Open or make a .py file

  • At the bottom right, there should say something like Python. Next to it is a button that allows you change your Python environment.

image-20250131-211138.png
  • Now you can Enter interpreter path.. and navigate to the Python interpreter in your virtual environment.

    • This is located in: path_to_your_environment\Scripts\python.exe

  • Now when you run code it will use this interpreter and the modules in your virtual environment

Other notes

  • You can open a terminal within VS Code. If you select a python virtual environment, opening a terminal will automatically activate your virtual environment

    • This is helpful when you want to install modules in your environment etc.

image-20250131-211744.png