How to Create a New Python Project in VS Code

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

  1. Open VS Code:
    Start by launching Visual Studio Code on your system.
  2. Create a New Folder:
    Create a new folder on your computer for the project. In VS Code, open the folder using File > Open Folder....
  3. 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
  4. Install Dependencies:
    If you need any Python packages for your project, you can install them using pip

    pip install package-name

    To keep track of the dependencies, save them to requirements.txt

    pip freeze > requirements.txt
  5. Initialize Git:
    Inside your project folder, initialize a Git repository

    git init
  6. 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__/

  7. 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

  1. 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.”
  2. 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
  3. 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)

  1. Open your terminal and run:

    ssh-keygen -t ed25519 -C "your_email@example.com"

    Press Enter to accept the default file location.
  2. Add your SSH private key to the SSH agent:

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519

  3. Copy the public key to your clipboard:

    cat ~/.ssh/id_ed25519.pub

Add the SSH Key to GitHub

  1. Log in to GitHub and go to Settings > SSH and GPG keys.
  2. 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

  1. Make Changes to Your Code:
    Edit or add files to your project.
  2. Stage and Commit the Changes:
    After making changes, stage and commit them:

    git add .
    git commit -m "Description of changes"
  3. 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!

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.