【问题标题】:Import a Python library from Github从 Github 导入 Python 库
【发布时间】:2013-11-13 00:40:33
【问题描述】:

我是 Python 新手,所以这听起来很傻。

我想使用我在 Github 上找到的 Python 库,比如说 https://github.com/praw-dev/praw,我希望将来能够使用 git pull 来拉取最新的提交。

问题:我是否应该在项目目录中git clone <git url> 并删除除praw 目录之外的所有内容,然后在我的python 脚本中执行import praw

在 iPython 中,

import praw

给出错误ImportError: No module named praw

目录结构

~\myProject\
    praw\
    myNotebook.ipynb

【问题讨论】:

    标签: python git python-2.7 github ipython


    【解决方案1】:

    实际上,如果给定的包不在 PyPI 上(或者你想要一个特定的分支),你仍然可以通过 GitHub 上的 pip 安装它:

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

    对于您的问题,它会是(尽管@pandita 的回答对于正常使用情况是正确的):

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

    更多信息请查看this答案。

    【讨论】:

    • 对我不起作用。我有一个名为 pinyin_utils 的 github 存储库。执行命令-用我的github用户名替换[repo owner],用'pinyin_utils'或'pinyin_utils.git'替换[repo],用'master'替换[branch name],或者将它和'@'关闭,我得到错误消息: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\{{User}}\\AppData\\Local\\Temp\\pip-req-build-yk2dkge1\\setup .py'(注意:我用 {{User}} 替换了本地用户名 - 文件夹存在直到并包括 'Temp')在具有管理员权限的 PowerShell 窗口中执行此操作。
    【解决方案2】:

    来自 github 的实验性 Python 模块查找器/加载器,例如在 golang 中。

    所以,在 golang 中我们可以像这样导入:

    import "github.com/parnurzeal/gorequest"
    

    但是在python中我们应该手动安装包:

    pip install requests
    

    然后像这样导入它:

    import requests
    

    但有了这个神奇的包和 PEP-0302 的强大功能,我们可以自动完成:

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

    安装

    你应该有 git、Python 3.2+ 和 pip:

    pip install import_from_github_com
    

    参考:https://github.com/nvbn/import_from_github_com

    【讨论】:

      【解决方案3】:

      只需克隆 python 路径上任何目录中的文件,然后通常使用命令行中的python setup.py install 构建库。

      我通常在我的 site_libraries 文件夹(包含所有 pip 安装包的文件夹)中克隆一个库形式的 git。从那里你可以像任何其他 git repo 一样从 git 拉取然后构建库。有这些文件很好,因为你所有的库都在你的 python 路径上。

      【讨论】:

        【解决方案4】:

        您可能需要考虑使用pip 而不是 git 来安装和升级软件包(除非您有迫切需要使用 git 的理由)。

        pip install praw

        要更新你可以做的包

        pip install --upgrade praw

        还可以查看here,了解有关如何使用 pip 的更多信息。

        【讨论】:

        • 问题想要它'来自 github'
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-09
        • 1970-01-01
        • 2018-07-22
        • 2018-04-04
        • 2020-01-21
        • 1970-01-01
        相关资源
        最近更新 更多