I'm trying to use PyGitHub and I'm getting "ImportError: No module named github".
Setup specs:
macOS v10.13.6
Python v3.7.0
Installed with pip v18.0
Change your current directory and try again?
Do you mean simply move the script to another directory and try again? If that's what you meant, I tried and nothing changed.
Is your script name also "github"?
No, the Python script is not named "github".
Since I haven't used the library at all, I was just attempting to use the sample code provided in the README.md
@JosephTLyons Did you run $ pip install pygithub
before trying to use the library?
Yes, I did. And if I run it again, I get these messages to confirm it.
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)
Hi, @JosephTLyons did you install py2 and py3 together in your MBP?
If you do, confirm your IDE interpreter config is same as your pip install was
like the picture below
and my IDE interpreter config is
@JosephTLyons Try to run the script on a terminal.... If this run successfully your IDE is bad configured
I do have both python 2 (the version Apple ships with their mac) and python3 (installed through Homebrew).
Also, I am running the script through Terminal, I'm not using an IDE. I'm coding strictly with Atom and then running with Terminal.
Bear with me, while I'm not a new programmer, I am new to Python and this is one of the first Python modules I've tried to use that wasn't successful for me.
I have the same problem. I created a virtual environment and tried to install PyGithub using pip install PyGithub. I received: Requirement already satisfied (use --upgrade to upgrade): pygithub in /myvenv/lib/python3.5/site-packages
Requirement already satisfied (use --upgrade to upgrade): requests> = 2.14.0 in /myvenv/lib/python3.5/site-packages (from pygithub)
Requirement already satisfied (use --upgrade to upgrade): pyjwt in /myvenv/lib/python3.5/site-packages (from pygithub)
Requirement already satisfied (use -upgrade to upgrade): idna <2.8,> = 2.5 in /myvenv/lib/python3.5/site-packages (from requests> = 2.14.0-> pygithub)
Requirement already satisfied (use -upgrade to upgrade): chardet <3.1.0,> = 3.0.2 in /myvenv/lib/python3.5/site-packages (from requests> = 2.14.0-> pygithub)
Requirement already satisfied (use --upgrade to upgrade): certifi> = 2017.4.17 in /myvenv/lib/python3.5/site-packages (from requests> = 2.14.0-> pygithub)
Requirement already satisfied (use -upgrade to upgrade): urllib3 <1.24,> = 1.21.1 in /myvenv/lib/python3.5/site-packages (from requests> = 2.14.0-> pygithub)
I tried importing the library using python via terminal, but... No sucess
Does a Python 2 virtualenv solve the problem?
Where it will be used, python 2 is not used because of conflict.
This seems to be one annoying known issue, let me investigate and try to fix.
Can be a problem of PIP?
I remember had this problem (with other module) on Linux Mate.
If this is still an issue, pip install from the latest master branch, where I just fixed an import issue in de6b713b0.
Also comments in #314 are worth checking out.
Same issue.
Try installing pygithub with this command: python -m pip install pygithub
This ensures you're using the pip related to python interpreter that you're running the script with.
i did it:
python3 -m venv myvenv
I activated the environment and then:
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.
I tried running a script using pygithub and it did not return any error.
it is good to try to run the script first from the command line of the virtual environment.
These may be caused by the relative imports. Python 3 requires absolute imports. This should be fixed by #967
sudo pip3 install pygithub
worked for me.
this breaks in python 3.7+ but works in 3.6.5 FYI
though not sure if it's a pip issue as 3.7.2 uses pip 18 but 3.6.5 uses 9.x
this breaks in python 3.7
I agree, have just tested it. When using Python 3.7+, getting following error:
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'
Using Python 3.6 works fine.
Does this still not work on Python 3.7 ??? Im still getting error when importing?
@conoroshea1996 they have refused to fix this in the past despite multiple PRs to fix it.
I have the same issue when I use Pycharm on Mac OS, but I do not have this issue when I use Jupyter on Mac OS. And, I do not have this issue when use Pycharm or Jupyter on Windows.
+1
Python 3.7.4 I get the same error.
@p3nda This is almost certainly not the same issue as previously reported, can you show the full error?
Hi @s-t-e-v-e-n-k ,
Sure thing:
python3 --version
Python 3.7.4
I get:
ImportError: cannot import name 'Github' from 'github' (/usr/local/lib/python3.7/site-packages/github/__init__.py)
I figured this error is slightly different, so after troubleshooting for a while unsuccessfully I spun up a docker container and installed python3.6 and tried to use it:
ImportError: cannot import name 'Github'
python3 --version
Python 3.6.9
@p3nda On which platform?
@s-t-e-v-e-n-k For the first error on python3.7.4 i'm using macos mojave 10.14.6. In the docker container with python 3.6.9 i'm using Ubuntu 18.04.3
So it's odd, what does:
>>> 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'
give you?
And from github.MainClass import Github
sorry
In Ubuntu 18.04 Docker container:
import github
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.6/dist-packages/github/__init__.py", line 44, in
from github.MainClass import Github, GithubIntegration
File "/usr/local/lib/python3.6/dist-packages/github/MainClass.py", line 58, in
import requests
File "/usr/local/lib/python3.6/dist-packages/requests/__init__.py", line 112, in
from . import utils
File "/usr/local/lib/python3.6/dist-packages/requests/utils.py", line 26, in
from ._internal_utils import to_native_string
File "/usr/local/lib/python3.6/dist-packages/requests/_internal_utils.py", line 11, in
from .compat import is_py2, builtin_str, str
File "/usr/local/lib/python3.6/dist-packages/requests/compat.py", line 59, in
from http import cookiejar as cookielib
File "/usr/lib/python3.6/http/cookiejar.py", line 31, in
import copy
File "/copy.py", line 2, in
from github import Github
ImportError: cannot import name 'Github'
from github.MainClass import Github
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.6/dist-packages/github/__init__.py", line 44, in
from github.MainClass import Github, GithubIntegration
File "/usr/local/lib/python3.6/dist-packages/github/MainClass.py", line 58, in
import requests
File "/usr/local/lib/python3.6/dist-packages/requests/__init__.py", line 112, in
from . import utils
File "/usr/local/lib/python3.6/dist-packages/requests/utils.py", line 26, in
from ._internal_utils import to_native_string
File "/usr/local/lib/python3.6/dist-packages/requests/_internal_utils.py", line 11, in
from .compat import is_py2, builtin_str, str
File "/usr/local/lib/python3.6/dist-packages/requests/compat.py", line 59, in
from http import cookiejar as cookielib
File "/usr/lib/python3.6/http/cookiejar.py", line 31, in
import copy
File "/copy.py", line 2, in
from github import Github
ImportError: cannot import name 'Github'
On my Mac:
import github
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.7/site-packages/github/__init__.py", line 44, in
from github.MainClass import Github, GithubIntegration
File "/usr/local/lib/python3.7/site-packages/github/MainClass.py", line 58, in
import requests
File "/Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/__init__.py", line 112, in
from . import utils
File "/Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/utils.py", line 26, in
from ._internal_utils import to_native_string
File "/Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/_internal_utils.py", line 11, in
from .compat import is_py2, builtin_str, str
File "/Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/compat.py", line 59, in
from http import cookiejar as cookielib
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
from github import Github
ImportError: cannot import name 'Github' from 'github' (/usr/local/lib/python3.7/site-packages/github/__init__.py)
from github.MainClass import Github
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.7/site-packages/github/__init__.py", line 44, in
from github.MainClass import Github, GithubIntegration
File "/usr/local/lib/python3.7/site-packages/github/MainClass.py", line 58, in
import requests
File "/Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/__init__.py", line 112, in
from . import utils
File "/Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/utils.py", line 26, in
from ._internal_utils import to_native_string
File "/Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/_internal_utils.py", line 11, in
from .compat import is_py2, builtin_str, str
File "/Users/amanda/Library/Python/3.7/lib/python/site-packages/requests/compat.py", line 59, in
from http import cookiejar as cookielib
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
from github import Github
ImportError: cannot import name 'Github' from 'github' (/usr/local/lib/python3.7/site-packages/github/__init__.py)
I should note that i'm on my work machine this morning doing the test from the macbook which is
MacOS Mojave 10.14.6 with python 3.7.5 installed. For the ubuntu container, it is the same one, same version of python.
@s-t-e-v-e-n-k
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
Where did that /copy.py or /Users/amanada/copy.py file come from? It's masking a real Python module /usr/lib/python3.6/copy.py and causing a circular import.
copy.py is a script I was working which uses PyGithub. When I ran those commands they were ran from the python interpreter itself after i tried running the script. I spun up a new container without running that script and just ran the import commands through python interpreter and works fine:
import github
from github import Github
exit()
I'm not sure why adding it in my script broke everything, it's not even fully formed yet:
import shutil
from github import Github
ACCESS_TOKEN = '123456789'
g = Github(ACCESS_TOKEN)
print(g.get_user().get_repos())
@s-t-e-v-e-n-k
It broke everything because of the name -- it masks a Python module called 'copy' which is in the module import chain when you run from github import Github
. Call your script anything else and it should work fine.
Ah okay, thanks for the clarity @s-t-e-v-e-n-k i appreciate your time!
@p3nda I'm delighted to help! I'm going to close this issue, if you have any other issues please feel free to open a new issue.
Can someone help me with this error
Traceback (most recent call last):
File "terra.py", line 1, in
from python_terraform import *
ImportError: No module named python_terraform
In my case, I got this error due to issues with my Python imports path.
I'm using Python 3.7.3:
$ python3 --version
Python 3.7.3
And PyPI 20.2.3:
$ pip3 --version
pip 20.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
I have installed 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)
But, it fails to import, nevertheless:
$ python3 <<< "from github import Github"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'github'
In the pip output, we saw that the package was installed to /usr/local/lib/python3.7/site-packages
. However, this directory was __not__ in my Python imports path:
$ 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']
PYTHONPATH
One way to solve this is to add the missing directory to my PYTHONPATH
, in ~/.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
And source the file:
$ . ~/.bashrc
/Users/jhwill/Library/Python/3.7/lib/python/site-packages
is already in my system path. So, we can target this directory for installation while using the pip3
command:
$ 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
Most helpful comment
I agree, have just tested it. When using Python 3.7+, getting following error:
Using Python 3.6 works fine.