Pygithub: ImportError:没有名为github的模块

创建于 2018-08-07  ·  43评论  ·  资料来源: PyGithub/PyGithub

我正在尝试使用PyGitHub,并且收到“ ImportError:没有名为github的模块”。

设置规格:
macOS v10.13.6
Python v3.7.0
随pip v18.0一起安装

bug high priority

最有用的评论

这打破了python 3.7

我同意,刚刚测试过。 使用Python 3.7+时,出现以下错误:

Traceback (most recent call last):
  File "scripts/src/create_release.py", line 6, in <module>
    from github.Milestone import Milestone
ModuleNotFoundError: No module named 'github'

使用Python 3.6可以正常工作。

所有43条评论

更改您的当前目录,然后重试?

您的意思是只是将脚本移到另一个目录然后重试吗? 如果那是您的意思,我尝试了并且什么都没有改变。

您的脚本名称也是“ github”吗?

不,Python脚本未命名为“ github”。

由于我根本没有使用过该库,因此我只是尝试使用README.md提供的示例代码。

screen shot 2018-08-07 at 5 33 05 pm

@JosephTLyons在尝试使用该库之前,您是否运行过$ pip install pygithub

是的,我做到了。 如果我再次运行它,则会收到这些消息以进行确认。

Josephs-MBP-15:~ josephlyons$ pip install pygithub
Requirement already satisfied: pygithub in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (1.40)
Requirement already satisfied: requests>=2.14.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pygithub) (2.19.1)
Requirement already satisfied: pyjwt in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pygithub) (1.6.4)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests>=2.14.0->pygithub) (1.23)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests>=2.14.0->pygithub) (3.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests>=2.14.0->pygithub) (2018.4.16)
Requirement already satisfied: idna<2.8,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests>=2.14.0->pygithub) (2.7)

嗨, @JosephTLyons您是否在MBP中同时安装了py2和py3?
如果这样做,请确认您的IDE解释器配置与您的pip安装相同
就像下面的图片
image
我的IDE解释器配置是
image

@JosephTLyons尝试在终端上运行脚本。...如果运行成功,则说明您的IDE配置错误

我同时拥有python 2(Apple随其mac一起提供的版本)和python3(通过Homebrew安装)。

另外,我通过终端运行脚本,没有使用IDE。 我严格使用Atom进行编码,然后使用Terminal运行。

忍受我,虽然我不是一个新程序员,但我是Python的新手,这是我尝试使用的第一个Python模块之一,但对我而言并不成功。

screen shot 2018-08-12 at 4 54 04 am

我也有同样的问题。 我创建了一个虚拟环境,并尝试使用pip install PyGithub安装PyGithub。 我收到:要求已经满足(使用--upgrade进行升级):/myvenv/lib/python3.5/site-packages中的pygithub
需求已经满足(使用--upgrade升级):/myvenv/lib/python3.5/site-packages中的request> = 2.14.0(来自pygithub)
需求已满足(使用--upgrade进行升级):/myvenv/lib/python3.5/site-packages中的pyjwt(来自pygithub)
需求已满足(使用-upgrade升级):/ myvenv / lib / python3.5 / site-packages中的idna <2.8,> = 2.5(来自请求> = 2.14.0-> pygithub)
要求已经满足(使用-upgrade升级):/ myvenv / lib / python3.5 / site-packages中的chardet <3.1.0,> = 3.0.2(来自请求> = 2.14.0-> pygithub)
已满足要求(使用--upgrade升级):/myvenv/lib/python3.5/site-packages中的certifi> = 2017.4.17(来自请求> = 2.14.0-> pygithub)
要求已经满足(使用-upgrade升级):/myvenv/lib/python3.5/site-packages中的urllib3 <1.24,> = 1.21.1(来自请求> = 2.14.0-> pygithub)

我尝试通过终端使用python导入库,但是...没有成功
sem titulo
sem titulo2

Python 2 virtualenv是否可以解决问题?

在会使用它的地方,由于冲突而未使用python 2。

这似乎是一个令人讨厌的已知问题,让我调查并尝试解决。

可能是画中画的问题吗?

我记得在Linux Mate上有这个问题(和其他模块一样)。

如果仍然存在问题,请从最新的master分支进行pip安装,我刚刚在de6b713b0中修复了导入问题。

#314中的评论也值得一试。

同样的问题。

尝试使用以下命令安装pygithub: python -m pip install pygithub

这可以确保您使用与运行脚本的python解释器相关的pip。

我做到了:
python3 -m venv myvenv
我激活了环境,然后:
python -m pip install pygithub

Collecting pygithub
  Downloading https://files.pythonhosted.org/packages/91/9e/50af4bb0ab316fedc13ae87347a718c19698ba8a19e64a692dc30c5fc9db/PyGithub-1.43.2.tar.gz (2.7MB)
    100% |████████████████████████████████| 2.7MB 647kB/s 
Collecting requests>=2.14.0 (from pygithub)
  Using cached https://files.pythonhosted.org/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl
Collecting pyjwt (from pygithub)
  Using cached https://files.pythonhosted.org/packages/93/d1/3378cc8184a6524dc92993090ee8b4c03847c567e298305d6cf86987e005/PyJWT-1.6.4-py2.py3-none-any.whl
Collecting Deprecated (from pygithub)
  Downloading https://files.pythonhosted.org/packages/83/2d/7a5a117eccdef0cdf4cf015830945a7d8fdbbc9ec866d7cead758dc4229c/Deprecated-1.2.3-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests>=2.14.0->pygithub)
  Downloading https://files.pythonhosted.org/packages/df/f7/04fee6ac349e915b82171f8e23cee63644d83663b34c539f7a09aed18f9e/certifi-2018.8.24-py2.py3-none-any.whl (147kB)
    100% |████████████████████████████████| 153kB 3.3MB/s 
Collecting urllib3<1.24,>=1.21.1 (from requests>=2.14.0->pygithub)
  Using cached https://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests>=2.14.0->pygithub)
  Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
Collecting idna<2.8,>=2.5 (from requests>=2.14.0->pygithub)
  Using cached https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl
Collecting wrapt<2,>=1 (from Deprecated->pygithub)
  Downloading https://files.pythonhosted.org/packages/a0/47/66897906448185fcb77fc3c2b1bc20ed0ecca81a0f2f88eda3fc5a34fc3d/wrapt-1.10.11.tar.gz
Building wheels for collected packages: pygithub, wrapt
  Running setup.py bdist_wheel for pygithub ... error
  Complete output from command /home/leonardo/phub/myvenv/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-yww1918p/pygithub/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /tmp/tmpx2l6hyczpip-wheel- --python-tag cp35:
  /usr/lib/python3.5/distutils/dist.py:261: UserWarning: Unknown distribution option: 'python_requires'
    warnings.warn(msg)
  usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: -c --help [cmd1 cmd2 ...]
     or: -c --help-commands
     or: -c cmd --help

  error: invalid command 'bdist_wheel'

  ----------------------------------------
  Failed building wheel for pygithub
  Running setup.py clean for pygithub
  Running setup.py bdist_wheel for wrapt ... error
  Complete output from command /home/leonardo/phub/myvenv/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-yww1918p/wrapt/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /tmp/tmp4kz97gl4pip-wheel- --python-tag cp35:
  usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: -c --help [cmd1 cmd2 ...]
     or: -c --help-commands
     or: -c cmd --help

  error: invalid command 'bdist_wheel'

  ----------------------------------------
  Failed building wheel for wrapt
  Running setup.py clean for wrapt
Failed to build pygithub wrapt
Installing collected packages: certifi, urllib3, chardet, idna, requests, pyjwt, wrapt, Deprecated, pygithub
  Running setup.py install for wrapt ... done
  Running setup.py install for pygithub ... done
Successfully installed Deprecated-1.2.3 certifi-2018.8.24 chardet-3.0.4 idna-2.7 pygithub-1.43.2 pyjwt-1.6.4 requests-2.19.1 urllib3-1.23 wrapt-1.10.11
You are using pip version 8.1.1, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

我尝试使用pygithub运行脚本,但未返回任何错误。

最好先从虚拟环境的命令行运行脚本。

这些可能是由于相对进口造成的。 Python 3需要绝对导入。 这应该由#967解决

sudo pip3 install pygithub为我工作。

这会在python 3.7+中中断,但可以在3.6.5 FYI中使用
尽管不确定是否是点子问题,因为3.7.2使用点子18,而3.6.5使用9.x

这打破了python 3.7

我同意,刚刚测试过。 使用Python 3.7+时,出现以下错误:

Traceback (most recent call last):
  File "scripts/src/create_release.py", line 6, in <module>
    from github.Milestone import Milestone
ModuleNotFoundError: No module named 'github'

使用Python 3.6可以正常工作。

这仍然不适用于Python 3.7吗??? 导入时我仍然收到错误消息?

@ conoroshea1996他们过去拒绝修复此问题,尽管有多个PR对其进行了修复。

在Mac OS上使用Pycharm时,我遇到相同的问题,但在Mac OS上使用Jupyter时,我没有此问题。 而且,在Windows上使用Pycharm或Jupyter时,我没有此问题。

+1
Python 3.7.4我遇到了同样的错误。

@ p3nda这几乎可以肯定与以前报告的问题不同,您可以显示完整的错误吗?

嗨@stevenk,
当然可以:

python3-版本
的Python 3.7.4

我得到:

ImportError:无法从'github'(/usr/local/lib/python3.7/site-packages/github/__init__.py)导入名称'Github'

我发现此错误略有不同,因此在进行了一段时间的故障排除后,我旋转了一个Docker容器并安装了python3.6并尝试使用它:

ImportError:无法导入名称“ Github”
python3-版本
的Python 3.6.9

@ p3nda在哪个平台上?

@stevenk对于python3.7.4上的第一个错误,我正在使用macos mojave 10.14.6。 在带有python 3.6.9的docker容器中,我正在使用Ubuntu 18.04.3

因此,奇怪的是:

>>> import github
>>> github.__file__
'/home/steven/PyGithub/github/__init__.py'
>>> dir(github.MainClass)
['AuthenticatedUser', 'Consts', 'DEFAULT_BASE_URL', 'DEFAULT_PER_PAGE', 'DEFAULT_STATUS_URL', 'DEFAULT_TIMEOUT', 'Github', 'GithubException', 'GithubIntegration', 'GitignoreTemplate', 'HookDescription', 'Installation', 'InstallationAuthorization', 'RateLimit', 'Repository', 'Requester', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'datetime', 'github', 'jwt', 'pickle', 'requests', 'time', 'urllib3']
>>> github.MainClass.__file__
'/home/steven/PyGithub/github/MainClass.py'

给你?

from github.MainClass import Github对不起

在Ubuntu 18.04 Docker容器中:

导入github
追溯(最近一次通话):
文件 ”“,第1行,在
在第44行的“ /usr/local/lib/python3.6/dist-packages/github/__init__.py”文件中
从github.MainClass导入Github,GithubIntegration
在第58行的“ /usr/local/lib/python3.6/dist-packages/github/MainClass.py”文件中
汇入要求
在第112行的“ /usr/local/lib/python3.6/dist-packages/requests/__init__.py”文件中
从 。 导入工具
在第26行的“ /usr/local/lib/python3.6/dist-packages/requests/utils.py”文件中
从._internal_utils导入到_native_string
在第11行的“ /usr/local/lib/python3.6/dist-packages/requests/_internal_utils.py”文件中
从.compat导入is_py2,builtin_str,str
在第59行中的文件“ /usr/local/lib/python3.6/dist-packages/requests/compat.py”
从http导入cookiejar作为cookielib
在第31行的文件“ /usr/lib/python3.6/http/cookiejar.py”
导入副本
文件“ /copy.py”,第2行,在
从github导入Github
ImportError:无法导入名称“ Github”

从github.MainClass导入Github
追溯(最近一次通话):
文件 ”“,第1行,在
在第44行的“ /usr/local/lib/python3.6/dist-packages/github/__init__.py”文件中
从github.MainClass导入Github,GithubIntegration
在第58行的“ /usr/local/lib/python3.6/dist-packages/github/MainClass.py”文件中
汇入要求
在第112行的“ /usr/local/lib/python3.6/dist-packages/requests/__init__.py”文件中
从 。 导入工具
在第26行的“ /usr/local/lib/python3.6/dist-packages/requests/utils.py”文件中
从._internal_utils导入到_native_string
在第11行的“ /usr/local/lib/python3.6/dist-packages/requests/_internal_utils.py”文件中
从.compat导入is_py2,builtin_str,str
在第59行中的文件“ /usr/local/lib/python3.6/dist-packages/requests/compat.py”
从http导入cookiejar作为cookielib
在第31行的文件“ /usr/lib/python3.6/http/cookiejar.py”
导入副本
文件“ /copy.py”,第2行,在
从github导入Github
ImportError:无法导入名称“ Github”

在我的Mac上:

导入github
追溯(最近一次通话):
文件 ”“,第1行,在
在第44行的“ /usr/local/lib/python3.7/site-packages/github/__init__.py”文件中
从github.MainClass导入Github,GithubIntegration
在第58行的“ /usr/local/lib/python3.7/site-packages/github/MainClass.py”文件中
汇入要求
在第112行的“ /Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/__init__.py”文件中
从 。 导入工具
文件“ /Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/utils.py”,第26行,在
从._internal_utils导入到_native_string
在第11行的“ /Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/_internal_utils.py”文件中
从.compat导入is_py2,builtin_str,str
文件“ /Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/compat.py”,第59行,在
从http导入cookiejar作为cookielib
在第31行的文件“ /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/cookiejar.py”
导入副本
文件“ /Users/amanda/copy.py”,第2行,在
从github导入Github
ImportError:无法从'github'(/usr/local/lib/python3.7/site-packages/github/__init__.py)导入名称'Github'

从github.MainClass导入Github
追溯(最近一次通话):
文件 ”“,第1行,在
在第44行的“ /usr/local/lib/python3.7/site-packages/github/__init__.py”文件中
从github.MainClass导入Github,GithubIntegration
在第58行的“ /usr/local/lib/python3.7/site-packages/github/MainClass.py”文件中
汇入要求
在第112行的“ /Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/__init__.py”文件中
从 。 导入工具
文件“ /Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/utils.py”,第26行,在
从._internal_utils导入到_native_string
在第11行的“ /Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/_internal_utils.py”文件中
从.compat导入is_py2,builtin_str,str
文件“ /Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/compat.py”,第59行,在
从http导入cookiejar作为cookielib
在第31行的文件“ /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/cookiejar.py”
导入副本
文件“ /Users/amanda/copy.py”,第2行,在
从github导入Github
ImportError:无法从'github'(/usr/local/lib/python3.7/site-packages/github/__init__.py)导入名称'Github'

我应该注意,今天早上我在工作机上通过Macbook进行测试,这是
已安装python 3.7.5的MacOS Mojave 10.14.6。 对于ubuntu容器,它是相同版本的python。

@stevenk

File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/cookiejar.py", line 31, in
import copy
File "/Users/amanda/copy.py", line 2, in 

/copy.py或/Users/amanada/copy.py文件从何而来? 它掩盖了一个真实的Python模块/usr/lib/python3.6/copy.py并导致循环导入。

copy.py是我正在使用PyGithub的脚本。 当我运行这些命令时,它们是在我尝试运行脚本后从python解释器本身运行的。 我在不运行该脚本的情况下启动了一个新容器,只是通过python解释器运行了导入命令,并且运行良好:

导入github
从github导入Github
出口()

我不确定为什么在脚本中添加它会破坏所有内容,甚至还没有完全形成:

进口壁垒
从github导入Github

ACCESS_TOKEN ='123456789'

初始化github客户端

g = Github(ACCESS_TOKEN)
打印(g.get_user()。get_repos())

@stevenk

它因名称而中断了一切-它屏蔽了一个名为'copy'的Python模块,当您运行from github import Github时,该模块位于模块导入链中。 调用脚本其他任何东西,它应该可以正常工作。

好的,感谢您的@stevenk的谢意,感谢您的宝贵时间!

@ p3nda很高兴为您提供帮助! 我将关闭此问题,如果您还有其他问题,请随时打开一个新的问题。

有人可以帮我解决这个错误

追溯(最近一次通话):
在第1行的文件“ terra.py”中
从python_terraform导入*
ImportError:没有名为python_terraform的模块

就我而言,由于我的Python导入路径存在问题,导致出现此错误。

我正在使用Python 3.7.3:

$ python3 --version
Python 3.7.3

和PyPI 20.2.3:

$ pip3 --version
pip 20.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

我已经安装了PyGithub:

$ pip3 install pygithub
Requirement already satisfied: pygithub in /usr/local/lib/python3.7/site-packages (1.43)
Requirement already satisfied: pyjwt in /usr/local/lib/python3.7/site-packages (from pygithub) (1.7.1)
Requirement already satisfied: requests>=2.14.0 in /usr/local/lib/python3.7/site-packages (from pygithub) (2.24.0)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests>=2.14.0->pygithub) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests>=2.14.0->pygithub) (1.25.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests>=2.14.0->pygithub) (2020.6.20)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests>=2.14.0->pygithub) (2.10)

但是,它仍然无法导入:

$ python3 <<< "from github import Github"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'github'

在pip输出中,我们看到该软件包已安装到/usr/local/lib/python3.7/site-packages 。 但是,此目录在我的Python导入路径中为__not__:

$ python3 <<< 'import sys; print(sys.path)'
['', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python37.zip', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Users/jhwill/Library/Python/3.7/lib/python/site-packages', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site-packages']

解决方案1:添加到PYTHONPATH

解决此问题的一种方法是将缺少的目录添加到PYTHONPATH中的~/.bashrc

# Add these lines
readonly local_site_packages="/usr/local/lib/python3.7/site-packages"
if [ ! -z "$PYTHONPATH" ]; then
    export PYTHONPATH="$local_site_packages:$PYTHONPATH"
else
    export PYTHONPATH="$local_site_packages"
fi

并获取文件:

$ . ~/.bashrc

解决方案2:安装到现有的导入路径

/Users/jhwill/Library/Python/3.7/lib/python/site-packages已经在我的系统路径中。 因此,我们可以在使用pip3命令的同时将此目标定位为安装目录:

$ pip install --target=/Users/jhwill/Library/Python/3.7/lib/python/site-packages PyGithub
Collecting PyGithub
  Using cached PyGithub-1.53-py3-none-any.whl (274 kB)
Collecting pyjwt
  Using cached PyJWT-1.7.1-py2.py3-none-any.whl (18 kB)
Collecting deprecated
  Using cached Deprecated-1.2.10-py2.py3-none-any.whl (8.7 kB)
Collecting requests>=2.14.0
  Using cached requests-2.24.0-py2.py3-none-any.whl (61 kB)
Processing ./Library/Caches/pip/wheels/62/76/4c/aa25851149f3f6d9785f6c869387ad82b3fd37582fa8147ac6/wrapt-1.12.1-cp37-cp37m-macosx_10_14_x86_64.whl
Collecting idna<3,>=2.5
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting chardet<4,>=3.0.2
  Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Downloading urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
     |████████████████████████████████| 127 kB 2.9 MB/s 
Installing collected packages: pyjwt, wrapt, deprecated, idna, chardet, certifi, urllib3, requests, PyGithub
Successfully installed PyGithub-1.53 certifi-2020.6.20 chardet-3.0.4 deprecated-1.2.10 idna-2.10 pyjwt-1.7.1 requests-2.24.0 urllib3-1.25.10 wrapt-1.12.1
此页面是否有帮助?
0 / 5 - 0 等级