从 Github 导入 Python 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19943022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import a Python library from Github
提问by Nyxynyx
I'm new to Python so this may sound silly.
我是 Python 新手,所以这听起来很傻。
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.
我想使用我在 Github 上找到的 Python 库,比如说 on https://github.com/praw-dev/praw
,我希望git pull
将来能够提取最新的提交。
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
?
问题:我应该git clone <git url>
在项目目录中删除除praw
目录之外的所有内容,然后在我的 python 脚本中执行import praw
?
In iPython,
在 iPython 中,
import praw
gives the error ImportError: No module named praw
给出错误 ImportError: No module named praw
Directory Structure
目录结构
~\myProject\
praw\
myNotebook.ipynb
回答by pandita
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
代替 git 来安装和升级包(除非您有使用 git 的紧迫理由)。
pip install praw
pip install praw
to update the package you can do
更新你可以做的包
pip install --upgrade praw
pip install --upgrade praw
Also have a look herefor further information on how to use pip.
另请查看此处以获取有关如何使用 pip 的更多信息。
回答by agconti
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.
只需克隆 python 路径上任何目录中的文件,然后通常python setup.py install
从命令行构建 lib 。
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.
我通常在我的site_libraries
文件夹(包含所有 pip 安装包的文件夹)中克隆一个库形式 git 。从那里你可以像任何其他 git repo 一样从 git 拉取然后构建库。将文件放在那里很好,因为您的所有库都在您的 python 路径上。
回答by pkowalczyk
Actually, if given package is not on PyPI (or you want a specific branch) you can still install it through pip from GitHub with:
实际上,如果给定的包不在 PyPI 上(或者你想要一个特定的分支),你仍然可以通过 GitHub 上的 pip 安装它:
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):
对于您的问题,它将是(尽管@pandita 的答案对于正常使用情况是正确的):
pip install git+https://github.com/praw-dev/praw.git
For more information check thisanswer.
有关更多信息,请查看此答案。
回答by Mohammad Ali
Experimental Python module finder/loader from github, like in golang.
来自 github 的实验性 Python 模块查找器/加载器,就像在 golang 中一样。
So, in golang we can import like:
所以,在 golang 中,我们可以像这样导入:
import "github.com/parnurzeal/gorequest"
But in python we should install package by our hands:
但是在python中,我们应该手动安装包:
pip install requests
And import it like:
并像这样导入:
import requests
But with this magic package and power of PEP-0302 we can do it automatically:
但是有了这个神奇的包和 PEP-0302 的强大功能,我们可以自动完成:
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:
你应该有 git、Python 3.2+ 和 pip:
pip install import_from_github_com