Open In App

How to Deploy Python project on GitHub

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Deploying a project using GitHub and Git has become very important in today's software development, And this is backed by several reasons

  • Collaboration: GitHub helps developers collaborate among different team members working on the same project, no matter their location. Many developers can work on the same project simultaneously, building different features while maintaining the quality of the code and making bug fixes.
  • Code Backup: By deploying your project to GitHub, this could potentially help in case of local hardware malfunctions, and their will always be a backup for the project on the cloud, which can be accessed from any part of the world.
  • Integration with other tools: GitHub has a marketplace with a wide range of developer tools, including pipelines (GitHub Actions. Travis CI) and several project managers helped streamline the developer's workflow, making it fairly easy to auto-deploy and deploy monitoring.

GitHub is a web based platform used for collaborative project development, At it's heart, it uses Git to track changes in the project. Uploading your Python project to GitHub makes it shareable to other people as well.

For this tutorial, we will be using the terminal integrated with Visual Studio Code for pushing our code to GitHub. Because of the integrated terminal, it is easier to use a terminal within the IDE during development, making it faster to deploy Python projects.

Primary Terminologies

  • Repository (Repo): Your project's storage location, which includes all files, history, and metadata.
  • Commit: A commit is a snapshot of changes in the repository. Each commit has a unique identifier (SHA).
  • Branch: A branch is a repository version that exists in parallel. The default branch is typically main or master.
  • Push: Sending committed changes from a local repository to a remote one.
  • Remote: A remote repository is one that is hosted on the internet or another network.

Installation & Setup up Git

As I have mentioned earlier, Git is the core of GitHub. So first, we will need to install Git and download the latest version of it, Follow the installation instructions for your operating system.

Screen after Git installation is completed.
Screen after Git installation is completed.

Step 1: Initializing an Git repository in a Python project.

  • For your reference, you could fork my repository here and use the sample Python project for yourself.
Initializing an Git repository
Initializing an Git repository

This command will create a .git folder where the details of the repository like remote and username and email are stored.

Step 2: Setting your username and email to push the code.

Setting the name and email config
Setting the name and email config

This config with the Git will only keep this username and email for the currently repository you are working with, and it is not recognized outside the current Git repository. This information is required by Git, It uses a username to associate commits with an identity. To set username and email across all repository in the system :

Setting the name and email config globally
Setting the name and email config globally

This config will recognized by every repository in the system, It helps developers to set their username and email global so that they do not have to tell Git about their identity every time they create and push and repository.

Creating an GitHub account

Head onto to create a GitHub account and fill in your details and confirm you email address.

Verification for GitHub account.
Verification for GitHub account.

If you do not verify your email address, you will not be able to: Create or fork repositories. Create issues or pull requests. Comment on issues, pull requests, or commits.

Creating an Repository in GitHub

Create an repository
Create an repository
  • Fill in the repository details.
Filling in the details of the repository
Filling in the details of the repository
  • Repository name: Give your repository a name.
  • Description: Add an description for your project.
  • Public/Private: Choose whether the repository should be public or private. - Public repository is accessible to anyone of the planet connected to the Internet while private repository is private means no one else other than you can access it.
  • Click "Create repository".

Now a screen like this will appear

The empty remote origin
The empty remote origin

Now copy the remote origin of the GitHub repository, Then we will head back to our terminal and add the files that we want to upload on the repository on GitHub.

Here's an example:

git init
git add -A
git commit -m "Initial commit"
git remote add origin https://github.com/your-username/HelloWorld.git
git push -u origin master
XML
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 424 bytes | 424.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (0/0), done.

This will open up a pop up window where you will need to connect your IDE to GitHub and we it will be done, The code will be successfully uploaded.

Pop Up window to connect to GitHub
Pop Up window to connect to GitHub

After this the uploading process will begin and when the code is successfully uploaded, It will be reflected on the GitHub repository.

done
Files reflected on the repository.

Let's make some changes and learn to re-deploy the changed file onto GitHub, I added a output.txt that will be storing the output of the code when it is executed.

update
Change in Repository
git add -A
git commit -m "change.one"
git push origin master

Here we will add the files which you recently created and then commit the changes with a different name so that it will be easy for us to recognize which changes we have recently pushed.

Finally we will push toe code to the origin which is the GitHub repository. And the changes will be reflected on your repository.

changed
The updated repository

Access through GitHub Codespace

GitHub codespaces enables an instant development environment in the cloud, so there is no longer the need to spend hours setting up a development environment. It offers an open place where developers can create, edit, and run code directly in a browser or by using Visual Studio Code—no local development environment.

  • Cloud Based Development: Since these codespaces run in VMs which are in the cloud, which removes the need for local setup and help across a sustainable development environment across devices.
  • Integration with GitHub: They are closely integrated with GitHub which makes it more easier to collaborate on code, pull requests etc.
  • Secured Environment: Since these are hosted in the cloud, it helps in keeping a cloud copy in case of machine failure.

Setting up codespaces

In order to setup GitHub codespaces, we only need to navigate to the repository for which we want to create a codespace, Under the "Code â–¼" button, you will find a codespace options adjacent to it.

codespace
Codespace section

But if you are creating a codespace, you will be billed on the basis of how much often you use their service, Codespaces are for free each month for 60 hours, after that you will be billed. GitHub codespaces uses a file configuration called devcontainer.json to configure the development cloud you are working in. Your repository can have one or several configuration files to provide the needs for your development.

Dev container config:

The preinstalled container is loaded with the latest version of Python with pip and miniconda. Since we need to configure our own container and tools we need. To set up the repository, you need to create a devcontainer.json file

dev
Selecting the custom container configuration

Next open up the command palette in the Codespace and then type add dev and you will be listed with several options pick the "Codespaces : Add Dev Container Configuration Files.."

  • Click on create new configuration
  • Then, Click on show all definitions.
lang
Python 3 configuration

After selecting this option, select the version, which you want to use. It is recommended to use the latest version to prevent any runtime errors.

Then a button with "OK" will appear and click that button and a devcontainer.json file will be generated.

ok
Click on "OK"

Additionally you can also install others tools if you want for your development such as Coverage.py which is a code coverage tool , The container json file will look something like this:

// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/python
{
"name": "Python 3",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye"

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

The devcotainer.json file would be in a .devcontainer folder where all your configurations can be saved. The updated folder structure will look like this

updated
updated file structure

Application Deployment

In order to deploy your app to GitHub codespaces, for this we will deploy a simple Python app to GitHub Codespaces.

Step 1: Create a requirements.txt file: And add the following you can add other modules if you want to use them.

streamlit==1.32.0
pandas==1.3.3
flask==3.0.3

Then install the modules present in the requirements.txt file

pip install -r requirements.txt

Step 2: Create a root file: We will now create a simple root file with the name "app.py" which is the main file for our web application. For keep this simple we will use Flask and create a simple web application with the following code.

Python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'This is the homepage.'

# main driver function
if __name__ == '__main__':
    app.run()

The in the terminal run the python program with

python3 app.py

This will expose an port of the codespace where your project is visible online.

deployement
The webpage where the application is working.

And this is how you deploy your application to codespaces, where you can host your development server and even share it with others.

Conclusion

Deploying a Python project to GitHub is an essential skill for current developers. It not only enables version control and collaboration among developers but it also helps collaborators, and the open-source community. In conclusion, releasing your Python projects on GitHub not only improves your coding methods but also connects you to the larger developer community, encouraging cooperation and progressive learning. By understanding these tools and methods, you can improve the quality of your projects.


Next Article
Article Tags :

Similar Reads