我正在尝试为 RethinkDB API 制作一个包装模块,但在导入我的类(称为 rethinkdb.py)时遇到了 AttributeError。我在具有共享文件夹“Github”的虚拟机中工作。
我在 IPython 控制台中执行此操作:
import library.api.rethinkdb as re
Run Code Online (Sandbox Code Playgroud)
这是错误:
回溯(最近一次调用最后一次):
文件“”,第 1 行,在 import library.api.rethinkdb as re
文件“/media/sf_GitHub/library/api/rethinkdb.py”,第 51 行,在 conn = Connection().connect_to_database() 中
文件“/media/sf_GitHub/library/api/rethinkdb.py”,第48行,在connect_to_database raise e
AttributeError: 'module' 对象没有属性 'connect'
这是代码:
import rethinkdb as r #The downloaded RethinkDB module from http://rethinkdb.com/
class Connection(object):
def __init__(self, host='127.0.0.1', port=28015, database=None, authentication_key=''):
self.host = host
self.port = port
if database is None:
self.db = 'test'
self.auth_key = authentication_key
def connect_to_database(self):
try:
conn = r.connect(self.host, self.port, self.db, self.auth_key) …Run Code Online (Sandbox Code Playgroud) 我有这个错误。为什么?
File "/k.py", line 257, in deskJ
eremuak = aFileLine.strip().split('\t')
AttributeError: '_io.BufferedReader' object has no attribute 'strip'
Run Code Online (Sandbox Code Playgroud)
代码
def deskribapenaJaso(self, aFileLine):
eremuak = aFileLine.strip().split('\t')
print(eremuak) #printNothing
Run Code Online (Sandbox Code Playgroud)
aFileLine = 它是文件的 X 行
下面是我在 spyder 中运行的非常基本的代码,python 很卡,我做错了什么?
import csv,os,sys
path = os.getcwd()
print (path)
os.chroot(path)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
os.chroot(path)
AttributeError: module 'os' has no attribute 'chroot'
Run Code Online (Sandbox Code Playgroud) 我要把这个贴在这里,因为它是一个栗子,让我有些头疼。这可以说是四年前发布的这个问题的副本,但我会再次发布,以防有人遇到我在这里遇到的特定 pandas-numpy 不兼容问题。或者也许有人会想出更好的答案。
代码片段:
#import pdb; pdb.set_trace()
# TODO: This raises AttributeError: 'float' object has no attribute 'sin'
xr = xw + L*np.sin(?r)
Run Code Online (Sandbox Code Playgroud)
输出:
Traceback (most recent call last):
File "MIP_MPC_demo.py", line 561, in <module>
main()
File "MIP_MPC_demo.py", line 557, in main
animation = create_animation(model, data_recorder)
File "MIP_MPC_demo.py", line 358, in create_animation
xr = xw + L*np.sin(?r)
AttributeError: 'float' object has no attribute 'sin'
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试过的:
(Pdb) type(np)
<class 'module'>
(Pdb) np.sin
<ufunc 'sin'>
(Pdb) type(?r)
<class 'pandas.core.series.Series'>
(Pdb) …Run Code Online (Sandbox Code Playgroud) 我正在使用 python 并收到此错误。
当我运行它时,出现:“AttributeError: module 'telegram' has no attribute 'Bot'” 任何想法如何解决这个问题?
在《Hands on machine Learning with scikit-learn and tensorflow 2.0》一书的第 17 章中,他们使用 tf.data.Dataset 和 window() 方法将顺序数据集拆分为多个窗口:
n_steps = 100
window_length = n_steps + 1 # target = input shifted 1 character ahead
dataset = dataset.window(window_length, shift=1, drop_remainder=True)
Run Code Online (Sandbox Code Playgroud)
为了将顺序数据集分割成多个窗口,他们使用了以下方法:
dataset = dataset.flat_map(lambda window: window.batch(window_length))
Run Code Online (Sandbox Code Playgroud)
但是当我执行上面的那一行时,出现以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-18-5b215fb4cb71> in <module>()
----> 1 dataset = dataset.flat_map(lambda window: window.batch(window_length))
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/data/ops/dataset_ops.py in flat_map(self, map_func)
1650 Dataset: A `Dataset`.
1651 """
-> 1652 return FlatMapDataset(self, map_func)
1653
1654 def interleave(self, …Run Code Online (Sandbox Code Playgroud) 我有一个按钮,它应该返回ask_wikipedia函数,所以我使用了CallbackQueryHandler,但是当我想调用ask_wikipedia函数时,我收到一个属性错误!为什么?我该如何修复它?
def Click_Button(update, context) :
query = update.callback_query
if query.data == "Research":
ask_wikipedia(update, context)
query_handler = CallbackQueryHandler(Click_Button)
dispatcher.add_handler(query_handler)
def ask_wikipedia(update, context) :
update.message.reply_text('What do you want to know about ? ')
return About
Run Code Online (Sandbox Code Playgroud)
当我点击按钮时出现此错误
AttributeError: 'NoneType' object has no attribute 'reply_text'
Run Code Online (Sandbox Code Playgroud)
我该如何解决它?
python attributeerror telegram python-telegram-bot telegram-bot
我需要将以下代码从 PyQt5 (它在那里工作)转换为 PyQt6:
self.setWindowFlags(Qt.FramelessWindowHint)
Run Code Online (Sandbox Code Playgroud)
这是错误:
AttributeError: type object 'Qt' has no attribute 'FramelessWindowHint'
Run Code Online (Sandbox Code Playgroud)
我已经尝试过这个:
self.setWindowFlags(Qt.WindowFlags.FramelessWindowHint)
Run Code Online (Sandbox Code Playgroud)
它说:
AttributeError: type object 'Qt' has no attribute 'WindowFlags'
Run Code Online (Sandbox Code Playgroud) 我正在处理加载到 Python 字典中的 JSON 数据。其中很多都有可选字段,其中可能包含字典之类的东西。
dictionary1 =
{"required": {"value1": "one", "value2": "two"},
"optional": {"value1": "one"}}
dictionary2 =
{"required": {"value1": "one", "value2": "two"}}
Run Code Online (Sandbox Code Playgroud)
如果我这样做,
dictionary1.get("required").get("value1")
Run Code Online (Sandbox Code Playgroud)
显然,这是有效的,因为场"required"总是存在的。
但是,当我使用同一行dictionary2(以获取可选字段)时,这将产生一个AttributeError
dictionary2.get("optional").get("value1")
AttributeError: 'NoneType' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为第一个.get()将返回None,而第二个.get()不能调用.get()None 对象。
我可以通过提供默认值来解决这个问题,以防可选字段丢失,但是数据变得越复杂,这就会很烦人,所以我称之为“天真的修复”:
dictionary2.get("optional", {}).get("value1", " ")
Run Code Online (Sandbox Code Playgroud)
因此,第一个.get()将返回一个空字典{},可以在其上调用第二个字典.get(),并且由于它显然不包含任何内容,因此它将返回空字符串,如第二个默认值所定义的那样。
这将不再产生错误,但我想知道是否有更好的解决方案 - 特别是对于更复杂的情况(value1包含数组或另一个字典等......)
我也可以用 try - except 来解决这个问题AttributeError,但这也不是我喜欢的方式。
try:
value1 = dictionary2.get("optional").get("value1")
except AttributeError:
value1 …Run Code Online (Sandbox Code Playgroud) python 3.10运行venv在Windows 10 pro.
我正在尝试按照教程进行Celery集成Flask:https://flask.palletsprojects.com/en/latest/patterns/celery/
# example.py
from celery import Celery, Task
from flask import Flask
def celery_init_app(app: Flask) -> Celery:
class FlaskTask(Task):
def __call__(self, *args: object, **kwargs: object) -> object:
with app.app_context():
return self.run(*args, **kwargs)
celery_app = Celery(app.name, task_cls=FlaskTask)
celery_app.config_from_object(app.config["CELERY"])
celery_app.set_default()
app.extensions["celery"] = celery_app
return celery_app
def create_app() -> Flask:
app = Flask(__name__)
app.config.from_mapping(
CELERY=dict(
# Redis Docker container connection string
broker_url="redis://default:redispw@localhost:55000",
result_backend="redis://default:redispw@localhost:55000",
task_ignore_result=True,
),
)
app.config.from_prefixed_env()
celery_init_app(app)
return …Run Code Online (Sandbox Code Playgroud) attributeerror ×10
python ×9
telegram ×2
api ×1
celery ×1
chroot ×1
connect ×1
dictionary ×1
enums ×1
flask ×1
json ×1
numpy ×1
pandas ×1
pyqt ×1
pyqt6 ×1
python-3.x ×1
redis ×1
rethinkdb ×1
telegram-bot ×1