Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
跨Python脚本使用输入变量_Python_Python 3.x - Fatal编程技术网

跨Python脚本使用输入变量

跨Python脚本使用输入变量,python,python-3.x,Python,Python 3.x,在另一个Python脚本中执行操作后,我试图访问函数中包含的变量,但我不想运行函数的操作,我只需要访问变量gh和user的返回值 我尝试了不同的方法来实现这一点,例如在主脚本中将变量的初始值设置为“无”,但遇到的问题是,当我将script1导入script2时,它会在脚本中运行,并再次将相关变量重置为“无”。此外,我还试着把它们放在一个没有运气的课堂上 此外,我还尝试使用if uuuuu name_uuuu='uuuu main_uuuuu':来运行函数,但我似乎不知道如何将函数中的值获取到sc

在另一个Python脚本中执行操作后,我试图访问函数中包含的变量,但我不想运行函数的操作,我只需要访问变量
gh
user
的返回值

我尝试了不同的方法来实现这一点,例如在主脚本中将变量的初始值设置为“无”,但遇到的问题是,当我将script1导入script2时,它会在脚本中运行,并再次将相关变量重置为“无”。此外,我还试着把它们放在一个没有运气的课堂上

此外,我还尝试使用
if uuuuu name_uuuu='uuuu main_uuuuu':
来运行函数,但我似乎不知道如何将函数中的值获取到script2中用作全局变量

我在这里看到了一些可能有效的答案,例如将函数的值返回给另一个函数以供使用???,但我不能完全确定语法,因为函数似乎不包含变量的值

如果我问错了这个问题,请让我知道如何改进它,因为我试图问“好”的问题,所以我不会被禁止。我仍然在学习,我在这里问了很多问题,但我从中学到了很多

脚本1.py:

#! /usr/bin/python

import github3
from github3 import login, GitHub, authorize
from getpass import getuser, getpass
import requests

import configparser

def getCreds():
    try:

        user = input('GitHub username: ')
    except KeyboardInterrupt:
        user = getuser()

    password = getpass('GitHub token for {0}: '.format(user))

    gh = login(user, password)


if __name__ == '__main__':

    getCreds()
    exec(open("next_file.py").read())
脚本2.py

import os
import github3
from github3 import login, GitHub, authorize
from getpass import getuser, getpass
import requests
import csv
import configparser
import sys
import script1
import codecs

gh = script1.gh
user = script1.user

def one_commit_one_file_change_pr():

    #open csv file and create header rows
    with open('c:\\commit_filechange.csv', 'w+') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['Login', 'Title', 'Commits', 'Changed Files','Deletions', 'Additions'])

    for pr in result:
        data = pr.as_dict()
        changes = (gh.repository(user, repo).pull_request(data['number'])).as_dict()    

        if changes['commits'] == 1 and changes['changed_files'] == 1:
        #keep print to console statement for testing purposes
        #print changes['user']['login']


            with open('c:\\commit_filechange.csv', 'a+') as f:
                csv_writer = csv.writer(f)

                csv_writer.writerow([changes['user']['login'], changes['title'], changes['commits'], changes['changed_files']])

one_commit_one_file_change_pr()

我认为这是一个常见的解决办法。设置一个名为global_var.py的文件(或类似文件),并将全局变量存储在那里。()以下是代码的简化版本:

脚本1.py


这个答案纯粹是基于我的假设,即您只想获得一次用户凭据,并且希望在以后的其他脚本中重复使用它“n”次。 我会这样做的


更新1

您的问题还与您希望如何组织和运行脚本有关,如果您已经将脚本捆绑到python包中并运行它们,下面的示例将起作用

因为。乙二醇

但是,如果您计划单独运行各个脚本,那么除了为每个脚本调用登录提示之外,您别无选择,除非您计划完全删除请求用户凭据并使用文件中的预配置数据

脚本1

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]
在其他脚本中

from script1 import do_login

# the first time do_login() is called it asks for user credentials
print do_login()
# the next times it just returns the previously collected one
print do_login()
例2

脚本3

from script1 import do_login
creds = do_login()  
脚本4

from script3 import creds
from script1 import do_login
print creds
print do_login() # No input prompt would be provided

@天行者…谢谢你的回复,但我遇到的问题是(我已经尝试过了),它再次通过提示用户输入用户名和密码来运行,而我只需要访问返回给变量的值,而无需再次遍历函数的其余部分。那么,只需在script1的底部声明变量,而不是在if name=main部分中。看见edit@skywalker...I按照你的建议进行了编辑,但我得到了相同的结果。从script2中的script1执行导入时,它会遍历代码,找到
gh,user=getCreds()
,然后整体运行该函数,包括提示输入用户名和密码,而我只需要返回给
gh
user
的值。我希望这是有意义的。我现在看到了问题所在,根据您的应用程序,您最好将其设置为环境变量。但我不知道如何解决这个问题。你的目的是什么?可能有更好的方法来获取用户名,而不是使用
input()
。我正在尝试获取用户名和pswd,以便使用github3库对github进行身份验证。我还使用用户名传递到github3库的repository对象。我找到了这篇文章,它讨论了如何将变量值返回给其他函数。这看起来是可行的,但我无法正确理解语法。我得到
TypeError:other_function()缺少1个必需的位置参数:“parameter”
@Sanju…您的操作是正确的,但我想在script1中运行一次
do_login
,以收集凭据,然后将
gh
user
中的值保存到其他脚本中,而不会再次得到提示。如果我在其他脚本中运行
do\u login()
,我会得到位于
getCredentials()
中的提示。我认为最好的方法是收集这些信条,并将它们存储在某种性质的加密文件中,然后从那里读出。令牌是安全敏感的部分。我现在明白了,您可能会如何使用它,如果您单独运行脚本,是的,每次都会显示提示。我添加了另一个可能的用法示例,如在script3中,您可以调用do_login()一次,然后在需要登录凭据的所有其他位置导入cred,这样,您实际上可以避免出现多个提示。但更好的方法是将凭据安全地存储在文件中并访问它。@Sanju…我看到了将脚本捆绑在python包中的更新。脚本不会单独运行。其想法是让主脚本循环并调用位于单独目录中的所有验证脚本
exec(open(“next_file.py”).read()
将变成一个循环来读取单独目录中的每个文件。我没有使用python软件包,所以我需要研究如何使用它们,然后尝试您的建议。
from script1 import do_login
creds = do_login()  
from script3 import creds
from script1 import do_login
print creds
print do_login() # No input prompt would be provided