我想知道为什么在调用函数时没有在python3中定义NameError:名称'file‘是没有定义的?
def func(file):
for file in os.listdir(cwd):
if file.endswith('.html'):
f = open(file, "r+")
....
f = open(file, "w")
f.write(text)
我有一个python脚本,它在Apache下在Jython中执行。它用@schemaFunction resp定义了一些函数。@outputSchemaFunction装饰师。
我想在Pig外部用一个独立的python主脚本测试这些函数,这个脚本就在“普通的本地Python解释器下”。然而,当我在本地Python系统下运行脚本时,解释器会抱怨:"NameError: name 'outputSchema' is not defined",我理解独立Python显然不支持这些装饰器。
如何在本地Python解释器中测试脚本?我是否可以将调用脚本中的装饰器定义为“空
我认为我很清楚在Python中变量和生成器是如何工作的。
然而,下面的代码让我感到困惑。
from __future__ import print_function
class A(object):
x = 4
gen = (x for _ in range(3))
a = A()
print(list(a.gen))
当运行代码(Python 2)时,它说:
追溯(最近一次调用):文件"Untitled 8.py",第10行,打印(列表(a.gen))文件"Untitled 8.py",第6行,in gen = (x for _ in r
如何将变量从外部类赋给内部?以下是我最好的尝试:
in [1]: class one:
...: x = 1
...: class two:
...: y = x
...:
---------------------------------------------------------------------------
NameError: name 'x' is not defined
我不明白为什么这不起作用,但是Python在这个语法上没有问题:
In [1]: x = 1
In [2]: c
在下面的代码中,我定义了两个函数。main和cube.我希望main是我的程序的开始,所以我在main中调用了cube
>>> def main():
number = int(input('Enter a number: '))
cubed_number = cube(number)
print("The number cubed is: ", cubed_number)
>>> def cube(number):
return number * number * number
>
我仍然是Python的初学者。我正在编写一个主脚本,其中函数(b)需要调用其中的另一个函数(a),但是当我运行它时,我会得到一个'NameError‘。这就是我在主要脚本中所做的:
# main script
from __file_a import a
from __file_b import b
par = 'some parameters'
x = b(par)
这就是我在b函数中所写的
def b(some parameters):
from __file_a import a
out = a(par2)
return o
我在我的robotframework脚本所在的目录上有一个Python脚本。我想在控制台上记录来自python脚本中使用robotframework的函数的结果。
我的Python脚本(称为raw_data.py):
from random import random
class raw_data:
def return_random_number(self):
result = random()
return result
raw_data = raw_data()
number = raw_data.return_random_number()
我有一个很简单的例子:
#!/usr/bin/env python
#a() # 1: NameError: name 'a' is not defined
#b() # 1: NameError: name 'b' is not defined
#c() # 1: NameError: name 'c' is not defined
def a():
c() # note the forward use here...
#a() #2: NameError: global name 'c' is not
我正在努力学习Python3,我想我把自己和代码搞混了。假设我有一个使用不同变量的函数。 我怎么能只从其中调用一个变量呢?如何才能在函数外部打印x值? 基本上,我想在函数中得到X的值,特别是当函数中有很多变量的时候。 提前谢谢你。 def calculation():
x = 0
y = 1
a = x * y
b = x + y + y
print(a)
print(b)
return x, y
print("testing")
print(calculation(x)) 当我尝试运行这段代码时,它返回了一个名称错误。 >Tracebac
我有以下脚本
test.py
a=1
def f() :
global a
a=2
当我在ipython3中运行这个脚本时,我得到以下结果:
In [1]: a
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-3f786850e387> in <module>
---
在我的(简化)脚本中,我有以下内容:
def predict(new_data):
.
.
.
model_list = fetch_needed_models() # ["f1","f2","f3"]
for m in model_list:
with open(f"./{m}.pkl","rb") as file:
exec(f"{m}=pickle.load(file)")
print(dir()) # [...,'f1',
我正在尝试理解Python中嵌套类的作用域。下面是我的示例代码:
class OuterClass:
outer_var = 1
class InnerClass:
inner_var = outer_var
类的创建没有完成,我得到了错误:
<type 'exceptions.NameError'>: name 'outer_var' is not defined
尝试inner_var = Outerclass.outer_var不起作用。我得到了:
<type 'exceptions.NameErr
为什么会发生这种NameError?
(插入提示以提高可读性之前的空行。)
$ python3
Python 3.4.10 (default, Oct 4 2019, 19:39:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> def blah():
..
def f1():
X = 88
def f2():
print(X)
return f2
action = f1()
action()
由于f1正在返回f2,所以当我将f2称为(f1())()时,似乎很好。
但是,当我以f2()的形式直接调用f2()时,它会产生错误。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'f2' is not defined
有人能用以上
我一直试图从Python内部评估一个简单的"integrate(x,x)“语句,方法是按照Sage指南将Sage导入Python。这是我的整个剧本:
#!/usr/bin/env sage -python
from sage.all import *
def main():
integrate(x,x)
pass
main()
当我试图从命令行运行它时,会引发以下错误:
NameError: global name 'x' is not defined
我尝试将var(x)添加到脚本中,global x,尝试用sage.integrate(x,
我知道存在许多这样的问题,但我似乎找不到一个可以解决我所遇到的问题的方法。
我有以下几点:
def function():
try:
# function to create a table on hbase
except AlreadyExists, ae:
print "WARN: " + ae.message
当我从另一个python脚本调用它时,它会给我
NameError: global name 'AlreadyExists' is not defined
...but如果我删除def function()并自己运行它,它
我在编程方面非常新手,所以在Python方面也是如此。随后,我遇到了一个我不知道如何解决的问题。
我正在使用一个设计用于BioPython的脚本在Mac上工作。我想,经过大量的工作,在尝试了很多次来安装BioPython之后,我终于正确地安装了NumPY,但当我尝试运行我的脚本时,我得到了这个错误:
ImportError: No module named Bio
我的脚本是这样开始的:
#!/usr/bin/python
from Bio import SeqIO
secuencias=SeqIO.parse("/Users/imac/Documents/Gradiente
glac
当我尝试运行这个简单的python脚本时,我得到了一个错误: def ask_x():
x = int(input('What is X?'))
def ask_y():
y = int(input('What is Y?'))
def result():
print(z)
def count():
if (x>10):
z = x + y
else:
z = 0
print('nono')
#start of program
ask_x()
main.py:
import subone
import subtwo
subone.py:
a = 'abc'
subtwo.py:
print subone.a
运行python main.py会引发一个NameError: name 'subone' is not defined。我希望它能印上“abc”。
将其重构为使用from import和类无助于:
main.py:
from subone import * # Only using from X import * for example purposes.
from subtwo impor
我正在使用这个代码
dof = {i: dframe.apply(lambda x: x.str[i - 1] if type(x.iat[0]) == list else x) for i in range(1, 7)}
将列表的数据转换为数据片段,如本中所示。当我在控制台中使用它时,行可以工作,但是当我将它包含在脚本中时,我会得到以下消息:
NameError: ("free variable 'type' referenced before assignment in enclosing scope", 'occurred at index dur
我的应用程序有一个按钮可以使用execfile动态执行python脚本。如果我在脚本中定义一个函数(例如。并尝试在另一个函数中使用该函数(例如。鸡蛋(),我得到这个错误:
NameError: global name 'spam' is not defined
从蛋清()内部调用垃圾邮件()函数的正确方法是什么?
#mainprogram.py
class mainprogram():
def runme(self):
execfile("myscript.py")
>>> this = mainprogram()
&
我使用cxfreeze从planrequest.py创建了一个Windows可执行文件。它似乎工作得很好,但当我运行exe文件时,我得到NameError: name 'exit' is not defined
声明修复方法是使用import sys。但是,我使用import sys。代码作为python脚本运行良好(如中所示,在编译为可执行文件之前,我对命令行参数进行了广泛的测试)。
import socket
import sys
if len(sys.argv) == 1:
print("Usage:")
print("PlanR
我有一个python脚本,并且收到以下错误:
Traceback (most recent call last):
File "C:\Users\Tim\Desktop\pop-erp\test.py", line 1, in <module>
s = Something()
NameError: name 'Something' is not defined
以下是导致问题的代码:
s = Something()
s.out()
class Something:
def out():
print("