<p>这个答案完全基于我的假设,即您只需要获取用户凭据一次,然后在其他脚本中重复使用它“n”次。
这是我该怎么做的</p>
<hr/>
<p>更新1</p>
<p>您的问题还与如何组织和运行脚本有关,如果您将脚本捆绑到python包中并运行它们,下面的示例就可以工作了</p>
<p>因为。例如</p>
<p><a href="https://i.stack.imgur.com/hnnqE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hnnqE.png" alt="enter image description here"/></a></p>
<p>但是,如果您计划单独运行单个脚本,那么您除了调用每个脚本的登录提示之外别无选择,除非您计划完全删除请求用户凭据并使用文件中预先配置的数据。在</p>
<p>脚本1</p>
<pre><code>from github3 import login
USER_CREDS = None # Store raw input credentials here ,not recomended
GIT_USERS = {} # global dict to store multiple users with username as key
def getCredentials():
global USER_CREDS
if USER_CREDS:
print "returning user credentials"
return USER_CREDS
print "gettting user credentials"
user = raw_input("Enter Username")
pwd = raw_input("Enter Password")
USER_CREDS = (user, pwd)
return USER_CREDS
def do_login():
global GIT_USERS
user, pwd = getCredentials()
if not GIT_USERS.get(user, None):
gh = login(user, password=pwd)
GIT_USERS[user] = gh
return GIT_USERS[user]
</code></pre>
<p>在其他脚本中</p>
^{pr2}$
<p>例2</p>
<p>脚本3</p>
<pre><code>from script1 import do_login
creds = do_login()
</code></pre>
<p>脚本4</p>
<pre><code>from script3 import creds
from script1 import do_login
print creds
print do_login() # No input prompt would be provided
</code></pre>