Setting up a new Python project, versioning it with Git, and hosting it on GitHub is a standard workflow for any developer. In this guide, we’ll walk through the process of creating a new Python project in Visual Studio Code (VS Code), pushing it to a new GitHub repository, and handling common SSH authentication errors you may encounter.
Step 1: Create a New Python Project in VS Code
- Open VS Code:
Start by launching Visual Studio Code on your system.
- Create a New Folder:
Create a new folder on your computer for the project. In VS Code, open the folder usingFile > Open Folder...
.
- Set Up a Python Environment:
It’s good practice to isolate your project’s dependencies. To do this, create a virtual environment by running
python -m venv venv
Then activate it:
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
- Windows:
- Install Dependencies:
If you need any Python packages for your project, you can install them usingpip
pip install package-name
To keep track of the dependencies, save them torequirements.txt
pip freeze > requirements.txt
- Initialize Git:
Inside your project folder, initialize a Git repository
git init
- Create
.gitignore
:
It’s important to exclude unnecessary files from your Git repository. In your project folder, create a.gitignore
file and add
venv/
*.pyc
__pycache__/
- Make the First Commit:
Stage and commit your changes
git add .
git commit -m "Initial commit with Python environment"
Step 2: Create a New Repository on GitHub and Push Code
- Create a New Repository on GitHub:
Go to GitHub and create a new repository. Name it appropriately and set it to either public or private, then click “Create repository.”
- Connect Local Repo to GitHub:
After creating your repository, link your local Git repository to GitHub using:
git remote add origin https://github.com/your-username/your-repository.git
- Push the Code to GitHub:
Push your local code to GitHub:
git push -u origin master
Step 3: Handling SSH Authentication Issues
If you encounter an SSH error when pushing to GitHub, like:
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
It typically means your SSH key isn’t set up correctly. Here’s how to resolve it:
Generate an SSH Key (if you don’t have one)
- Open your terminal and run:
ssh-keygen -t ed25519 -C "your_email@example.com"
Press Enter to accept the default file location.
- Add your SSH private key to the SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
- Copy the public key to your clipboard:
cat ~/.ssh/id_ed25519.pub
Add the SSH Key to GitHub
- Log in to GitHub and go to Settings > SSH and GPG keys.
- Click New SSH key, paste the copied key, and save it.
Test the SSH Connection
To verify everything is working, run:
ssh -T git@github.com
You should see a message like:
Hi your-username! You've successfully authenticated.
Once this is set up, try pushing your code again:git push -u origin master
Step 4: Pull Changes from GitHub
To update your local project with changes made on GitHub (whether by you or collaborators), use:git pull origin master
If there are conflicts, Git will alert you, and you can resolve them directly in VS Code.
Step 5: Amend and Commit Changes
- Make Changes to Your Code:
Edit or add files to your project. - Stage and Commit the Changes:
After making changes, stage and commit them:git add .
git commit -m "Description of changes" - Push the Changes:
Finally, push your changes back to GitHub:git push origin master
Conclusion
By following these steps, you’ll have a Python project set up in VS Code, pushed to GitHub, and be able to manage changes efficiently with Git. SSH key setup can sometimes be tricky, but with the steps above, you’ll be able to authenticate successfully and push your code securely. Happy coding!