以下行:
except (IOError, PermissionError, FileNotFoundError) as e:
Run Code Online (Sandbox Code Playgroud)
使用python 2.75运行它时给出以下错误消息:
NameError: global name 'PermissionError' is not defined
Run Code Online (Sandbox Code Playgroud)
但是python 3.3的一切运行良好.
思考/建议吗?
我有一个非常奇怪的错误,我无法解开.
基本上,我在我的lib文件夹中有这个类:
# lib/api/amazon.rb
module API
class Amazon
...
end
end
Run Code Online (Sandbox Code Playgroud)
当我想在某个地方使用它时,我require:
require 'api/amazon'
API::Amazon.do_stuff
Run Code Online (Sandbox Code Playgroud)
这最初是有效的,但过了一段时间它会破坏和提升NameError: uninitialized constant API::Amazon.当我调试它并require在引发错误时再次尝试该文件时,它返回false,表明该文件已经加载.我也可以在$"(这个加载文件列表)中看到它.为什么我不能访问API::Amazon?
注意:我添加了"API"作为首字母缩略词,ActiveSupport::Inflector这就是为什么我不必使用"Api":
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'API'
end
Run Code Online (Sandbox Code Playgroud)
编辑:
我也尝试::API::Amazon.do_stuff过,结果相同.
我试图找出导致我的应用程序出现 SEGFAULT 的原因,并设法将其追踪到某个模块。我在模块的某个函数中设置了跟踪:
def algorithm_wrapper(*args, **kwargs):
_version = version
if "Version" in kwargs:
_version = kwargs["Version"]
del kwargs["Version"]
algm = _framework.createAlgorithm(algorithm, _version)
pdb.set_trace()
_set_logging_option(algm, kwargs)
Run Code Online (Sandbox Code Playgroud)
如果我想检查局部变量_version和algm我收到以下错误:
-> _set_logging_option(algm, kwargs)
(Pdb) _version
NameError: name '_version' is not defined
(Pdb) algm
NameError: name 'algm' is not defined
Run Code Online (Sandbox Code Playgroud)
我很茫然,这是我第一次发现这个错误。请帮忙!
我正在关注Michael Hartl的"Ruby on Rails教程:学习Web开发",并创建检查用户姓名和电子邮件的测试长度的有效性(名称最多50个字符,电子邮件为255个字符).内容test/helpers/application_helper_test.rb是:
require 'test_helper'
class ApplicationHelperTest < ActionView::TestCase
test "full_title_helper" do
assert_equal full_title, FILL_IN
assert_equal full_title("Help"), FILL_IN
end
end
Run Code Online (Sandbox Code Playgroud)
运行时bundle exec rake test,所有测试都通过,但我看到以下消息在结尾处标记为错误:
ERROR["test_full_title_helper", ApplicationHelperTest, 1.820016791]
test_full_title_helper#ApplicationHelperTest (1.82s)
NameError: NameError: uninitialized constant ApplicationHelperTest::FILL_IN
test/helpers/application_helper_test.rb:5:in `block in <class:ApplicationHelperTest>'
test/helpers/application_helper_test.rb:5:in `block in <class:ApplicationHelperTest>'
Run Code Online (Sandbox Code Playgroud)
任何想法如何解决这一问题?
我已经查看了这个主题下的 stackoverflow 问题,并且我将 Trainer 类移到了 Class_Training 类的上方,但是当我输入“manage.py create”时,我仍然收到相同的“name 'Model' is not defined”错误超级用户”在我的命令提示符下。
另外,我很难迁移我的模型。我尝试过“django-admin makemigrations training”,但无法识别 django-admin;和 'manage.py makemigrations training' 但 makemigrations 未被识别。
如何迁移我的模型?
这是我的代码:
#from django.db import models
from django_pg import models
# Create your models here.
TRAINING_TYPE_CHOICES = (
('AC', 'Armed Combat'),
('UC', 'Unarmed Combat'),
('P', 'Piloting'),
('O', 'Other'),
)
GENDER_CHOICES = (
('F', 'Female'),
('M', 'Male'),
('U', 'Unspecified'),
)
OUTCOME_CHOICES = (
('P', 'Pass'),
('F', 'Fail'),
)
class Trainer(models, Model):
first_name = models.CharField(max_length = 25)
surname = models.CharField(max_length …Run Code Online (Sandbox Code Playgroud) 我在lambda函数中遇到了一个重新定位的问题.我可以成功输出foo到stdout但是在使用max()包含a 时出现错误lambda- 请参阅下面的简化代码...
总而言之,我正在尝试budget在未知数量的一阶键中找到嵌套键的最大值.
(Pdb) foo = self.some_method() # some_method() returns a dict, printed in the next step
(Pdb) pp foo
{'1': {'count': 1,
'extra_data': {'activity-count': 1,
'budget': 0,
[...MORE KEY-VALUE PAIRS HERE...]
'version': 1},
[...LOTS MORE KEY-VALUE PAIRS HERE...]
'elements_total': defaultdict(<type 'int'>, {'result': 1, 'another_key': 2}),
'extra_year_data': defaultdict(<function <lambda> at 0x10e05bd70>, {})},
'2': {'count': 1,
'extra_data': {'activity-count': 1,
'budget': 3,
[...MORE KEY-VALUE PAIRS HERE...]
'version': 1},
[...LOTS MORE KEY-VALUE PAIRS HERE...] …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 python3 中编写一个简单的递归函数。在学习 OO Java 时,我也想编写涉及对象的 Python 代码。下面是我的代码。我提示用户输入一个数字,屏幕应该显示每个小于 5 的整数。
class Recursion:
@staticmethod
def recursive(x):
if (x>5):
print (x)
recursive(x - 1)
def main(self):
x = int(input('Enter a number for recursive addition: '))
recursive(x)
Run Code Online (Sandbox Code Playgroud)
但是,当我在终端上运行它时,它会显示:“NameError: name 'recursive' is not defined”。这是错误的样子:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Recursion import *
>>> a = Recursion()
>>> a.main()
Enter a number …Run Code Online (Sandbox Code Playgroud) 我是 ruby on Rails 的新手。我的错误是
NameError in ReviewsController#create
uninitialized constant User::Review
Extracted source:
@review = current_user.reviews.build(review_params)
Run Code Online (Sandbox Code Playgroud)
我在其他堆栈溢出问题上读到,通常是错误名称或忘记belongs_to或has_many的错误,但我相信我已经正确设置了关系。我正在使用 gem 设备来处理用户并登录/注册等
评论.rb
class Reviews < ActiveRecord::Base
belongs_to :user
belongs_to :renters
end
Run Code Online (Sandbox Code Playgroud)
用户.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :reviews
end
Run Code Online (Sandbox Code Playgroud)
评论_Controller.rb
class ReviewsController < ApplicationController
before_action :set_renter
before_action :authenticate_user!
def new
@review = Reviews.new(renters: @renter)
end
def create
@review = current_user.reviews.build(review_params)
@review.renter = @renter
@review.save
redirect_to @renter
end
private
def set_renter
@renter = Renters.find(params[:renter_id])
end
def …Run Code Online (Sandbox Code Playgroud) 我无法理解此Python调试程序会话中引发的以下异常:
(Pdb) p [move for move in move_values if move[0] == max_value]
*** NameError: name 'max_value' is not defined
(Pdb) [move for move in move_values]
[(0.5, (0, 0)), (0.5, (0, 1)), (0.5, (0, 2)), (0.5, (1, 0)), (0.5, (1, 1)), (0.5, (1, 2)), (0.5, (2, 0)), (0.5, (2, 1)), (0.5, (2, 2))]
(Pdb) max_value
0.5
(Pdb) (0.5, (0, 2))[0] == max_value
True
(Pdb) [move for move in move_values if move[0] == 0.5]
[(0.5, (0, 0)), (0.5, (0, 1)), (0.5, (0, …Run Code Online (Sandbox Code Playgroud) 我一直在关注这个我在网上找到的关于深度学习中的语音分析的教程,它一直给我名字错误。我对python很陌生,所以我不确定如何定义它。但是 train_test_split 是默认的一种分割数据的方法,train_test_split 是导入的。
这是代码:
'''
import numpy as np
import pandas as pd
import os
import seaborn as sns
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('fivethirtyeight')
from tqdm import tqdm
print(os.listdir("../input"))
from keras import Sequential
from keras import optimizers
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential,Model
from keras.layers import LSTM, Dense, Bidirectional, Input,Dropout,BatchNormalization,CuDNNLSTM, GRU, CuDNNGRU, Embedding, GlobalMaxPooling1D, GlobalAveragePooling1D, Flatten
from keras import backend as K
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints
from …Run Code Online (Sandbox Code Playgroud)