1

I just finished by basic learning of Python and I was trying to build something of my own.

I am making use of PyGithub and this is my source code, picked from here:

#!/usr/bin/python

import getpass
import sys

sys.path.append("./PyGithub");

from github import Github
from github import GithubException

username = raw_input("Github Username:")
pw = getpass.getpass()
g = Github(username, pw)

for repo in g.get_user().get_repos():
    print (repo.name)

But this is giving me error :

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    from github import Github
  File "./PyGithub/github/__init__.py", line 37, in <module>
    from MainClass import Github, GithubIntegration
ModuleNotFoundError: No module named 'MainClass'

Can someone please explain in brief how Python interpreter is searching for the files and how can I resolve this error. Or a good link will be appreciated. I referred this article on __init__.py and it helped me a bit.

EDIT 1 Reference : How to fix "ImportError: No module named ..." error in Python?

After doing the following, my problem goes away....

~/Desktop/Dev/GithubMetrics/PyGithub/github $ export PYTHONPATH=`pwd`

I understand how it worked, but IMO this is not a proper solution, as I won't be doing this for every subdirectory for any module I decide to use. They are supposed to be standalone and adding the parent to the Python path should be all.

EDIT 2 Also I found that its working for only Python 2 with above version. With Python 3 its giving the error ImportError: No module named 'httplib'. This is strange because the same library installed with pip3 does not give the error. Unless the maintainers of that library have really messed up, the source code should be same on both the Github and Python packages repository.

OK. I did pip3 install again and went to the installed location and did a meld. The code actually differs. So this portion (EDIT2) is out of scope of this question.

Naveen
  • 7,944
  • 12
  • 78
  • 165
  • 1
    You shouldn't be using Python packages by making them submodules. [Install from PyPI like a normal person.](https://packaging.python.org/tutorials/installing-packages/) – jwodder Sep 21 '17 at 13:29
  • @jwodder : I know that way, but I chose this way as I might be modifying the source of submodule as per my needs, as I found lots of pending issues on PyGithub project and it seems to be abondoned – Naveen Sep 21 '17 at 13:30

1 Answers1

0

I have just tried your example and it works in my local machine. As a consequence, I assume this is a problem related to your folder structure.

Can you try following these instructions:

cd /tmp
mkdir gittest
cd gittest
virtualenv -p python3 env
source env/bin/activate
pip install -e git+https://github.com/PyGithub/PyGithub#egg=master
cp <your_test.py> ./test.py
python3 test.py

And please remove this line from your script:

sys.path.append("./PyGithub");

That is not an efficient piece of code; not to say unsecure.

  • Did it really work for you? Because i get the error `name 'raw_input' is not defined`. – Naveen Sep 21 '17 at 17:07
  • Try using 'input' instead of 'raw_input' – Martin Castro Alvarez Sep 21 '17 at 17:09
  • This exception is not raised if you follow my instructions and replace 'raw_input' 'ModuleNotFoundError: No module named 'MainClass'' – Martin Castro Alvarez Sep 21 '17 at 17:10
  • Yes it works. But it still is `pip install` which I didn't want to do. This was suggested by @jwodder as well. Can you please help me in understanding the better solution for my use case. Maybe I am not using the best practice, so please feel free to suggest me the correct approach. My intention was to have all dependency projects as submodules, with their source code. This would provide me flexibility to customize them as per my needs if needed. – Naveen Sep 21 '17 at 17:27
  • You can clone **https://github.com/PyGithub/PyGithub** and then import it from a relative path. However, you need to create a subdir (for example, *libs*) and clone all your submodules there. Then, create an **__init__.py** inside this directory so that you can do relative imports. After that, include **os.path.dirname(__file__)** in your **sys.path** list and finally: **from libs.github import Github** – Martin Castro Alvarez Sep 21 '17 at 19:03