简体   繁体   English

如何在项目中使用Python模块作为git子模块

[英]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. 我刚刚完成了Python的基础学习,然后尝试构建自己的东西。

I am making use of PyGithub and this is my source code, picked from here : 我正在使用PyGithub ,这是我的源代码,从这里选择

#!/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. 有人可以简要解释一下Python解释器如何搜索文件以及如何解决此错误。 Or a good link will be appreciated. 否则,将不胜感激。 I referred this article on __init__.py and it helped me a bit. __init__.py提到了这篇文章 ,这对我有所帮助。

EDIT 1 Reference : How to fix "ImportError: No module named ..." error in Python? 编辑1参考: 如何解决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. 我知道它是如何工作的,但是IMO这不是一个合适的解决方案,因为我不会对我决定使用的任何模块的每个子目录都这样做。 They are supposed to be standalone and adding the parent to the Python path should be all. 它们应该是独立的,并且将父项添加到Python路径应该是全部。

EDIT 2 Also I found that its working for only Python 2 with above version. 编辑2我也发现它仅适用于以上版本的Python 2。 With Python 3 its giving the error ImportError: No module named 'httplib' . 使用Python 3,它会给出错误ImportError: No module named 'httplib' This is strange because the same library installed with pip3 does not give the error. 这很奇怪,因为与pip3一起安装的同一个库不会给出错误。 Unless the maintainers of that library have really messed up, the source code should be same on both the Github and Python packages repository. 除非该库的维护人员真的搞砸了,否则Github和Python软件包存储库中的源代码都应该相同。

OK. 好。 I did pip3 install again and went to the installed location and did a meld. 我再次pip3 installpip3 install ,然后转到安装位置并进行了融合。 The code actually differs. 代码实际上有所不同。 So this portion (EDIT2) is out of scope of this question. 因此,这部分(EDIT2)在此问题范围之外。

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. 不要说不安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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