Skip to main content

Add Self Developed Packages from Github

·456 words·3 mins

Add Self Developed Packages from Github #

Created: November 11, 2023
Modified: November 11, 2023

Introduction #

In the evolving landscape of Python development, integrating self-developed packages into projects is a common necessity. This blog post focuses on leveraging GitHub and poetry, a Python dependency management tool, to streamline this process. Whether you’re working on a personal project or collaborating with a team, understanding how to efficiently add custom packages can significantly enhance your workflow.

Prerequisites #

Before diving into the specifics, ensure that you have:

  • A GitHub account.
  • A repository with your self-developed package.
  • Poetry installed on your machine. (Visit Poetry’s official website for installation instructions.)

Installation Using SSH #

If you’ve already configured SSH keys between your computer and GitHub, installing a package is straightforward. Run the following command in your project directory:

poetry add git+ssh://git@github.com:[YourUsername]/[YourRepoName].git

This command tells poetry to fetch the package from your GitHub repository using SSH.

Setting Up SSH (Optional) #

If you haven’t set up SSH keys, follow these steps:

  1. Generate a new SSH key on your machine. (Refer to GitHub’s SSH documentation for guidance.)
  2. Add the SSH key to your GitHub account.

Alternative Method Without SSH #

If you prefer not to use SSH, you can use a personal access token from GitHub.

Creating a Personal Access Token #

  1. Go to your GitHub account settings.
  2. Navigate to Developer settings → Personal access tokens.
  3. Generate a new token with the necessary permissions (at least ‘repo’ scope for private repositories).

Configuring Git and Poetry #

After obtaining your token, configure git to use it:

git config --global url."https://[Username]:[PersonalToken]@github.com/".insteadOf "https://github.com/"

Now, you can add your package using:

poetry add git+https://github.com/[YourUsername]/[YourRepoName].git

Branch and Tag Specification #

To specify a particular branch or tag of your package, modify the dependency line in your pyproject.toml file like this:

[YourPackageName] = { git = "https://github.com/[YourUsername]/[YourRepoName].git", branch = "main" }

Replace branch with tag if you want to specify a tag.

Troubleshooting Common Issues #

Encountering issues? Here are a few common ones:

  • SSH Key Not Recognized: Ensure your SSH key is added to the SSH agent and linked with your GitHub account.
  • Invalid Personal Access Token: Double-check your token’s permissions and expiration date.
  • Dependency Resolution Failure: Ensure the package’s setup.py or pyproject.toml is correctly configured.

Best Practices #

  • Regularly update your personal access tokens and SSH keys for security.
  • Use version tags for better control over package updates.
  • Keep your repository’s dependency information up to date.

Conclusion #

Integrating self-developed packages from GitHub into your Python projects using poetry is a powerful approach to managing dependencies. By following the steps outlined in this post, you can streamline your development process, whether you prefer SSH or personal access tokens for authentication.

References and Further Reading #