How do I install a package from GitHub gist?

Question:

I don’t understand this…

I want to install this https://gist.github.com/sixtenbe/1178136.

It is a peak detection script for python.

Everywhere I look I am told to use pip with the .git extension.

All I see is how to download the .zip, but from there I am lost.

How can I install this?

Thanks.

Asked By: sci-guy

||

Answers:

You can get the individual files in the Gist (or download the Gist as an ZIP and extract) and put them in your source code folder.

Then you will be able to import them as modules in your own scripts:

import analytic_wfm as AW
AW.ACV_A6( ... )

import peakdetect as PK
PK.peakdetect_parabola( ... )
Answered By: musically_ut

Let’s give it another look.
By "installing a package" we might mean that the package should be available via import.
For that the package directory should reside either in the current directory or in one of the other directories in the import search path.
One such directory is the "user-specific site-packages directory, USER_SITE":

python -c "import site; print(site.getusersitepackages())"

Git URL

First we might need a Git URL. Going to https://gist.github.com/sixtenbe/1178136 we can click on the Embed pop-up and switch it to Clone via HTTPS:

enter image description here

in order to obtain the GIT URL: https://gist.github.com/1178136.git.

git and bash

Having the Git URL and the Unix shell (bash) we can install the package manually into the USER_SITE.

Let’s go into the USER_SITE first:

cd $(python -c "import site; print(site.getusersitepackages())")
pwd

Now that we are in the USER_SITE, let’s download the Gist:

git clone https://gist.github.com/1178136.git analytic_wfm

Finally, let’s verify that the package is now available:

cd && python -c "import analytic_wfm.analytic_wfm; print(analytic_wfm.analytic_wfm.__all__)"

If numpy is installed, it prints

['ACV_A1', 'ACV_A2', 'ACV_A3', 'ACV_A4', 'ACV_A5', 'ACV_A6', 'ACV_A7', 'ACV_A8']

pip

Let’s try to install a Gist package with pip.
For pip install we should prefix the Git URL with git+:

pip install --user git+https://gist.github.com/1178136.git

This gives us the error:

ERROR: git+https://gist.github.com/1178136.git does not appear to be a
Python project: neither ‘setup.py’ nor ‘pyproject.toml’ found.

Looks like the package we’ve picked is missing the necessary pip configuration!

Let’s try another one:

pip install --user git+https://gist.github.com/bf91613a021a536c7ce16cdba9168604.git

Installs NP:

Successfully built llog
Installing collected packages: llog
Successfully installed llog-1.0

Particularly because it has the setup.py.

Note also that Gist does not support subfolders, and pip seems to depend on them in handling the packages argument, but the code in setup.py can workaround this by creating the package subfolder on the fly and copying the Python files there!

Hence if you want to import that Gist, https://gist.github.com/sixtenbe/1178136, with the rest of the requirements.txt dependencies, – you can fork it and add setup.py to the effect.

pypi

Given that the analytic-wfm can also be found at the Python Package Index, https://pypi.org/project/analytic-wfm/, you can install it with

pip install analytic-wfm
Answered By: ArtemGr

Here’s a 4-liner that I sometimes use:

import urllib.request
url = 'https://gist.github...'
with open('package_name.py', 'w') as file: file.write(urllib.request.urlopen(url).read().decode())
import package_name

It’s basically the same as @musically_ut’s approach, except in the script you’re running

Answered By: Matt
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.