48

I'm new to Python so this may sound silly.

I want to use a Python library I've found on Github, lets say on https://github.com/praw-dev/praw, and I want to be able to do git pull in the future to pull the latest commits.

Question: Should I git clone <git url> in the project directory and delete everything except the praw directory, then in my python script do a import praw?

In iPython,

import praw

gives the error ImportError: No module named praw

Directory Structure

~\myProject\
    praw\
    myNotebook.ipynb
agconti
  • 17,780
  • 15
  • 80
  • 114
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830

4 Answers4

50

Actually, if given package is not on PyPI (or you want a specific branch) you can still install it through pip from GitHub with:

pip install git+https://github.com/[repo owner]/[repo]@[branch name]

And for your problem it would be (although @pandita's answer is correct for normal usage case):

pip install git+https://github.com/praw-dev/praw.git

For more information check this answer.

Community
  • 1
  • 1
pkowalczyk
  • 16,763
  • 6
  • 27
  • 35
  • 2
    Didn't work for me. I have a github repo named pinyin_utils. Executing the command - replacing [repo owner] with my github user name and [repo] with 'pinyin_utils' or 'pinyin_utils.git', [branch name] with 'master', or leaving it and the '@' off, I get the error message: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\{{User}}\\AppData\\Local\\Temp\\pip-req-build-yk2dkge1\\setup.py' (NOTE: I replaced by local user name with {{User}} - the folders exist up to and including 'Temp') Doing this in a PowerShell window with admin privileges. – John Deighan Sep 30 '19 at 06:33
  • 1
    Note for anyone stumbling on this, this works as long as there is either a `setup.py` file or a `pyproject.toml` file in the directory. – Meow_ly Oct 25 '22 at 17:47
19

Experimental Python module finder/loader from github, like in golang.

So, in golang we can import like:

import "github.com/parnurzeal/gorequest"

But in python we should install package by our hands:

pip install requests

And import it like:

import requests

But with this magic package and power of PEP-0302 we can do it automatically:

from github_com.kennethreitz import requests

assert requests.get('https://github.com/nvbn/import_from_github_com').status_code == 200

Installation

You should have git, Python 3.2+ and pip:

pip install import_from_github_com

Reference: https://github.com/nvbn/import_from_github_com

Ali
  • 2,541
  • 2
  • 17
  • 31
10

Just clone the files in any dir on your python path and then build the lib typically with python setup.py install from the command line.

I typically clone a libray form git in my site_libraries folder ( the folder that holds all of your pip installed packages ). From there you can pull and then build the libraries from git just like any other git repo. Having the files there is nice because all of your libs are in once place on your python path.

agconti
  • 17,780
  • 15
  • 80
  • 114
0

You might want to consider using pip instead of git to install and upgrade the package (that is unless you have a pressing reason to use git).

pip install praw

to update the package you can do

pip install --upgrade praw

Also have a look here for further information on how to use pip.

pandita
  • 4,739
  • 6
  • 29
  • 51