简体   繁体   中英

How to use a Python module for your project as a git submodule

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM