How to use an unlisted Python Package with 'pyproject.toml' file in Google Colab

Question:

I am trying to use an Unofficial Twitter API using python library called RedGalaxy on Google Colab. This package is not yet available on Pypi platform.

I have tried to build the package in Google colab itself but wasn’t able to use it there.

!git clone https://github.com/Ristellise/RedGalaxy.git
!pip install -e ./RedGalaxy/.

Output:

Successfully built RedGalaxy
Successfully installed RedGalaxy-0.1.0 h11-0.14.0 httpcore-0.16.3 httpx-0.23.3 rfc3986-1.5.0

When I try to import library:

Code:

from RedGalaxy import TwitterUser

Output:

ImportError                               Traceback (most recent call last)
<ipython-input-3-2f6c640e0abc> in <cell line: 3>()
      1 import asyncio
      2 
----> 3 from RedGalaxy import TwitterUser
      4 
      5 

ImportError: cannot import name 'TwitterUser' from 'RedGalaxy' (unknown location)
Asked By: Saurabh-Daware

||

Answers:

It seems like there is an issue with the import statement. Here are a few things you can try:

  1. Ensure that the package is installed properly by running !pip freeze command in a separate cell on Colab. This will show all the installed packages along with their versions. Check if RedGalaxy package is listed there.

  2. Try importing the package using the following statement instead:

    from RedGalaxy.twitteruser import TwitterUser

This assumes that the TwitterUser module is present in the twitteruser sub-package.

  1. Check if there are any naming conflicts with other modules that you may have imported. Sometimes, there may be a name collision with another module, which causes the import statement to fail.

  2. Ensure that you are using the correct version of Python. RedGalaxy may be written for a specific version of Python. You can check the required Python version in the README file of the package repository.

If none of the above methods work, you may try to contact the package maintainer or developer for further assistance.

Answered By: Ali Ahammad

Using git clone and pip install -e ./RedGalaxy/. had caused the package to install with name:

-e git+https://github.com/Ristellise/RedGalaxy.git@4258602c48895e4b1b7e352479a44a3322c3722d#egg=RedGalaxy

as compared to name of packages installed using pip:

tensorflow==2.12.0
torch @ https://download.pytorch.org/whl/cu118/torch-2.0.0%2Bcu118-cp39-cp39-linux_x86_64.whl

Installing package using
!pip install git+https://github.com/Ristellise/RedGalaxy.git@main
will resolve package name to

RedGalaxy @ git+https://github.com/Ristellise/RedGalaxy.git@4258602c48895e4b1b7e352479a44a3322c3722d

which can be imported in code.

Answered By: Saurabh-Daware
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.