简体   繁体   English

在python中导入另一个脚本

[英]Importing another script in python

Let's say I have a this script called script.py: 假设我有一个名为script.py的脚本:

def num():
    ...
    return num1, num2

def quiz()
   ...
   ...
   num1, num2 = num()   
   name = input('name: ')
   class_num = input('class: ')

if __name__ = '__main__':
    quiz()

In another script I want to use this script and the script.py's defined variables: 在另一个脚本中,我想使用此脚本和script.py的已定义变量:

import script

script.quiz()

def path(class_num):
    ... 
    # do stuff

This keeps on returning 这一直在继续

'NameError: name 'class_num' is not defined' 'NameError:名称'class_num'未定义'

How can I use all the variable defined in the script.py in my second script? 如何在第二个脚本中使用script.py中定义的所有变量?

you defined a function in the script that returns nothing, all the variables that are being defined by user input need to be returned 您在脚本中定义了不返回任何内容的函数,需要返回由用户输入定义的所有变量

def quiz():
    stuff
    name = input('...')
    class_num = input('...')
    return [stuff, name, class_num]

the other scripts needs to be changed with 其他脚本需要更改

results = script.quiz()

then you parse results they will contain the quiz responses 然后您解析results ,其中将包含测验答案

You are trying to make a local variable act as a global one. 您正在尝试使局部变量充当全局变量。 class_num has a scope within the function quiz , but not outside of it. class_num在函数quiz具有范围,但不在其范围之内。

In Python, variables that are only referenced inside a function are implicitly global. 在Python中,仅在函数内部引用的变量是隐式全局的。 If a variable is assigned a value anywhere within the function's body, it's assumed to be a local unless explicitly declared as global. 如果在函数体内任何位置为变量分配了值,除非明确声明为全局变量,否则将假定该变量为局部变量。

The error 错误

'NameError: name 'class_num' is not defined' 'NameError:名称'class_num'未定义'

Is telling you exactly that, this variable doesn't exist in the scope its called. 正是在告诉您,此变量在其调用的范围中不存在。

To fix the problem simply change 要解决问题,只需更改

def quiz()
   ...
   ...
   num1, num2 = num()   
   name = input('name: ')
   class_num = input('class: ')
   return class_num

and in your other script catch the return variable 并在其他脚本中捕获返回变量

class_num = script.quiz()

def path(class_num):

You can use something like this In your script.py return the class_num variable 您可以使用类似这样的东西在您的script.py中返回class_num变量

def num():
    ...
    return num1, num2

def quiz()
   ...
   ...
   num1, num2 = num()   
   name = input('name: ')
   class_num = input('class: ')
   return class_num


if __name__ = '__main__':
    quiz()

and use it like this 像这样使用

import script

class_num = script.quiz()

def path(class_var = class_num):
    ... 
    # do stuff

in your code you are trying to use a local variable by importing the script.py which is not correct. 在您的代码中,您尝试通过导入不正确的script.py来使用局部变量。

hope it may help ! 希望对您有所帮助!

Python doesn't know what the variable class_num is because it's trapped within your quiz method. Python不知道变量class_num是什么,因为它被困在您的quiz方法中。

import script

script.quiz()
    ...
    class_num = input('class: ')
    # quiz function exits, making class_num go out of scope

# since class_num doesn't exist, this NameError
def path(class_num):
    ... 

If you want the class name to be usable outside the function, return it 如果希望类名在函数外部可用,请返回它

def quiz()
    ...
    ...
    num1, num2 = num()   
    name = input('name: ')
    class_num = input('class: ')
    return class_num  # <=======

And in the other script 在另一个脚本中

class_num = script.quiz()  # <=======

def path(class_num):
    ... 

If you want the other variables from quiz to be available, you have several options: 如果您希望quiz的其他变量可用,则有以下几种选择:

  • Multiple returns, as you did for your num method 多次返回,就像对num方法所做的那样
  • Create something to that holds all your data which counts as a single return. 创建一个东西来保存您的所有数据,这些数据都计为一次收益。 An object would be perfect, other options would be a namedtuple or a dictionary 一个对象将是完美的,其他选项将是一个namedtuple或一个字典
  • Use globals (please don't do this) 使用全局变量(请不要这样做)

I modified the script to show both: a) return value as mentioned by others, b) missing execution of path, with the returned value as parameter value. 我修改了脚本以显示这两个脚本:a)其他人提到的返回值,b)缺少路径执行,返回值作为参数值。 Here we have script.py 这里我们有script.py

def num():
    num1 = 1
    num2 = 2
    return num1, num2

def quiz():
   num1, num2 = num()
   name = "Andreas"
   class_num = 9
   return class_num

if __name__ == '__main__':
    quiz()

And here something.py 这是something.py

import script

def path(class_num):
    pass
    # do stuff

class_n = script.quiz()
path(class_n)

I hope this can show how it could work. 我希望这可以显示它如何工作。 You possibly used the variable class_num somewhere later outside the function path. 您可能稍后在函数路径之外的某处使用了class_num变量。 That gives you a NameError. 那给你一个NameError。

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

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