How to install and run Python code from GitHub

3 minute read see also comments

To install a Python package from a GitHub repository, that is not available via conda or pip, we have several options at hand:

  1. Downloading the repository as a ZIP file and extracting it.
  2. Clone the repository using the GitHub Desktop app, and keep the cloned version synchronized with updates in the repository.
  3. Like option 2, but using the git command line tool.

img

In this post we briefly go through these three options, which we apply to this test repository.

Download a repository as a ZIP file and run the included Python scripts

We can manually download a repository as a ZIP file and run the included Python scripts by following these steps:

  1. On the repository’s main website, click on the green Code button and choose Download ZIP: img
  2. Extract the Zip file and move the unpacked folder to the desired location on your hard drive.
  3. Follow the installation instructions provided on the repository’s website or documentation website.

    For the example repository, we have to create a virtual environment and install the requirements from the “requirements.txt” file:

    img
      conda create -n Tkinter_tests -y python=3.9 pip
      conda activate Tkinter_tests
      pip install -r requirements.txt
    
  4. To run a script from the package, simply type:
      python calculator.py
    

    img

Using the GitHub Desktop app

As an alternative, we can also clone the repository to our hard drive. The advantage of cloning is, that we can update our local copy with any updates in the repository. To clone a repository, we can use the GitHub Desktop app, which comes along with a GUI and makes the repository management quite handy and easy to use:

  1. In the GitHub Desktop app, click on the Add button and choose Clone Repository... img
  2. In the dialog prompt:
    1. Enter the corresponding repository URL or USERNAME/REPOSITORY. For our example repository from above, simply enter:
        FabrizioMusacchio/Tkinter_GUI_examples
      
    2. Then choose the local path of the folder, in which you’d like to clone the repository. This can be, e.g., the folder of the project, for which you’d like to use the scripts in the repository. The repository will be cloned to a new folder inside that local path: img
  3. Follow steps 3 and 4 from option 1.

Whenever the repository is updated by the repository contributors, the GitHub Desktop app detects these updates and offers the option to Pull origin, which will synchronize your local copy with the repository:

img

But caution: A synchronization via the pull operation is always bidirectional (in the next session we learn further details on this). If you add your own files to the cloned local repository folder, the GitHub Desktop app pushes them to the repository on GitHub, and they become available to the public – if the repository holders accept them. In case you have modified the downloaded repository content, the app also pushes these modifications back to the remote repository, and the repository holders can decide whether to accept your changes or not. Both scenarios are maybe not what you want. To avoid them, never commit your local changes to the repository:

img

Alternatively, never modify or add anything to the cloned folder, and just work in the parent project folder – except, of course, you’d like to actively contribute to the repository.

Using the git command line tool

If we have installed git on our OS, we can carry out the same steps with the command line as with the GitHub Desktop app:

  1. Copy the URL of the repository: img The URL for our example repository is:
      https://github.com/FabrizioMusacchio/Tkinter_GUI_examples.git
    
  2. Open a terminal and change the current working directory (cd) to the location where you want the cloned directory.
  3. Type:

      git clone https://github.com/FabrizioMusacchio/Tkinter_GUI_examples.git
    
  4. Follow steps 3 and 4 from option 1.

To update your locally cloned copy with potential changes in repository on GitHub, follow these steps:

  1. cd into the cloned repository folder on your hard drive.
  2. Type:

      git fetch
    

fetch only pulls updates from GitHub without merging them with our local changes. To merge our local changes with the repository, we would have to use the merge command:

git merge

The pull command combines both, fetching and merging, in one command:

git pull

The pull command only merges committed local changes with the remote repository. Since git is an open-source version control system, it is meant to store and manage different versions of our code stored on our local hard drive or in a remote repository (like GitHub). To create such versions with git, we have to commit them (i.e., the changes in our files) via

git commit -m "some explanatory commit comment"

With

git status

we can get a quick summary of the current status of our repository (Is everything up to date? Are any modified files waiting to be committed?). And with the log command,

git log

we can browse and inspect the history of the files in the repository.

comments