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.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 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 configThis 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 globallyThis 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. 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 - Fill in the repository details.
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 originNow 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 GitHubAfter this the uploading process will begin and when the code is successfully uploaded, It will be reflected on the GitHub repository.
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.
Change in Repositorygit 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.
The updated repositoryAccess 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 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
Selecting the custom container configurationNext 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.
Python 3 configurationAfter 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.
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 file structureApplication 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.
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.
Similar Reads
Top Python Projects on GitHub
Python has established itself as one of the most versatile and user-friendly programming languages in recent years. With a strong community and extensive libraries, it supports everything from web development to data science and machine learning. GitHub, a treasure trove for developers, offers numer
8 min read
How to edit an existing GitHub project in PyCharm?
PyCharm, a renowned integrated development environment (IDE), has earned its place as a favorite among Python developers. Developed by JetBrains, it is tailored to meet the specific needs of Python programmers. One of the many features that make PyCharm indispensable for Python developers is its sea
5 min read
How to Upload a Project through GitHub Desktop on GitHub
GitHub Desktop is a user-friendly application that simplifies the process of managing your GitHub repositories on your local machine. If youâre looking to upload a project to GitHub using GitHub Desktop, this guide will walk you through the steps, making the process straightforward and accessible ev
3 min read
How to Export a Git Project?
Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific ap
3 min read
How to Deploy React App on Netlify Using Github?
A React App is a web or mobile application that is built using the React library of JavaScript. It helps users create interactive, single-page, and dynamic applications. These applications are built using HTML, CSS, and JavaScript. Deployment of a React app can be done via GitHub, Netlify, or any ot
6 min read
How to Use GitHub For Personal Development Projects?
GitHub is more than just a platform for professional developers and large teams; itâs also a fantastic tool for individuals working on personal development projects. Whether youâre a beginner learning to code or an experienced developer building a portfolio, GitHub offers a suite of tools that can h
7 min read
How to Add Python Poetry to an Existing Project
Poetry is a powerful dependency management tool for Python that simplifies the process of managing the project dependencies, packaging, publishing, etc.. . It simplifies dependency management, virtual environments, and packaging, thus making development more efficient and less prone to errors. The p
3 min read
How to Create a Project in GitLab?
A popular web-based tool for the DevOps lifecycle, GitLab offers a Git repository manager. It integrates CI/CD pipelines, version control, and collaboration tools, making it a powerful tool for developers and companies. Creating a project is one of the first things you do while using GitLab. This ar
3 min read
How To Rename A Repository on GitHub?
Renaming a repository on GitHub is a simple task that might be necessary for several reasons: action may include project renaming, consolidation of repositories, improper naming conventions or, in general, redundancy and clarity. In this detailed guide, each of the steps involved in renaming a repos
6 min read
How to Import a Flutter Project from GitHub?
Importing a project from GitHub is a very common practice of developers. When we want to get any project from an online source like GitHub then we can easily import or download it to our system. Sometimes, developers need to get the small module of application and if that module is available on GitH
2 min read