标签: attributeerror

在 Jython 中访问 Java 类的成员时出现 AttributeError

我正在尝试将我自己的 java 类导入到一些 jython 代码中。我将我的 .java 编译为一个 .class 文件并将 .class 文件放入一个 .jar 中。然后我使用 -Dpython.path="path/to/jar/my.jar" 包含这个 .jar 文件。到目前为止一切顺利,启动我的程序时没有抱怨。

但是,当我到达使用我的 java 类的代码部分时,它似乎在我的 java 类中找不到任何函数。我得到以下信息 AttributeError

AttributeError: 'pos.Test' object has no attribute 'getName'
Run Code Online (Sandbox Code Playgroud)

我们欢迎所有的建议!(下面的代码示例。)

爪哇代码:

package pos;

class Test{

    private String name;

    public Test(){
        name = "TEST";
        System.out.println( "Name = " + name );
    }

    public String getName(){
        return name;
    }   
}
Run Code Online (Sandbox Code Playgroud)

Jython 代码片段:

import pos.Test

...

test = pos.Test()

print 'Name = ', test.getName()
Run Code Online (Sandbox Code Playgroud)

java jython attributeerror

5
推荐指数
1
解决办法
2488
查看次数

Python - AttributeError: 'int' 对象没有属性 'randint'

作为 python 课程的一部分,我正在做的任务之一是生成 1 到 10 100,000 次之间的随机数,并计算每个数字出现的次数。这是我为此任务编写的代码:

    import random

    one = 0
    two = 0
    three = 0
    four = 0
    five = 0
    six = 0
    seven = 0
    eight = 0
    nine = 0
    ten = 0
    count = 0

    while count < 100000:
        random = random.randint(1, 10)

        if random == 1:
            one += 1
        elif random == 2:
            two += 1
        elif random == 3:
            three += 1
        elif random == 4:
            four += 1
        elif …
Run Code Online (Sandbox Code Playgroud)

random attributeerror python-3.x

5
推荐指数
1
解决办法
2万
查看次数

异常 AttributeError:“'NoneType' 对象没有属性 'path'”

我正在调试 python 代码(python2.7.12),因为我的代码可以工作,但是当我将推文流式传输到数据库时,所有变量都为 NULL。

我得到的错误是:

Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0x10068f140> ignored
Run Code Online (Sandbox Code Playgroud)

我假设这个错误来自下面的代码:

def put_tweets_in_database(tweets):
    print "putting tweets in database"
    errors = 0
    count = 0

    for tweet in tweets:
        try:
            commit_tweet_to_database(tweet, count, len(tweets))
            count += 1  
        except Exception as e:
            print e
            session.rollback()
            errors += 1
    print 'there were {} errors'.format(errors)
Run Code Online (Sandbox Code Playgroud)

我不认为这个功能commit_tweet_to_database()有问题...

你有什么主意吗...?我将不胜感激任何帮助!

谢谢。

attributeerror python-2.7 try-except

5
推荐指数
1
解决办法
2万
查看次数

带有 readPlist 和 AttributeError 的 DeprecationWarning

我正在尝试找到一种访问 plist 文件的方法:/Library/Preferences/com.apple.iPod.plist 以访问其中的序列号。

这是我当前的代码--

import os
import plistlib

fileName=os.path.expanduser('/Users/Ryan/Library/Preferences/com.apple.iPod.plist')

pl=plistlib.readPlist(fileName)

for left, right in pl.items(): 
   for values in right.values():
         print(values['Serial Number'])
Run Code Online (Sandbox Code Playgroud)

我不断得到结果,但也会出现一些快速错误。我得到这个:

plist.py:8: DeprecationWarning: The readPlist function is deprecated, use load() instead pl=plistlib.readPlist(fileName)
Run Code Online (Sandbox Code Playgroud)

还有这个:

  File "plist.py", line 16, in <module>
    for values in right.values():
   AttributeError: 'bool' object has no attribute 'values'
Run Code Online (Sandbox Code Playgroud)

我猜使用加载函数相当简单,尽管我很难使用我在网上找到的教程来弄清楚它并根据我的需要进行修改。

关于布尔属性错误,我不知道我做错了什么。

谢谢!

python boolean plist attributeerror

5
推荐指数
1
解决办法
5894
查看次数

AttributeError:“系列”对象没有属性“has_z”

GeoDataFrame我从 CSV 文件中获取了以下内容,并经过一些切片CRSgeometry分配

    ctf_nom         geometry                                    id      
0   Prunus mahaleb  POINT (429125.795043319 4579664.7564311)    2616    
1   Betula pendula  POINT (425079.292045901 4585098.09043407)   940     
2   Betula pendula  POINT (425088.115045896 4585093.66943407)   940     
3   Abelia triflora POINT (429116.661043325 4579685.93743111)   2002    
4   Abies alba      POINT (428219.962044021 4587346.66843531)   797  
Run Code Online (Sandbox Code Playgroud)

我已经将 a 转换geometrystr

from shapely import wkt

df['geometry'] = df['geometry'].apply(wkt.loads)
df_geo = gpd.GeoDataFrame(df, geometry = 'geometry')
Run Code Online (Sandbox Code Playgroud)

并通过以下方式分配了一个crs:

df_geo.crs = {'init' :'epsg:25831'}
df_geo.crs
Run Code Online (Sandbox Code Playgroud)

当我尝试再次按 gdf.to_file()函数保存减少的地理数据帧时,它返回以下属性错误:

AttributeError: 'Series' object has no …

attributeerror python-3.x writetofile geopandas

5
推荐指数
1
解决办法
1461
查看次数

AttributeError:模块“tensorboard”没有属性“lazy”

我有一个使用 Keras 和张量流后端的生成对抗网络。我正在运行 Tensorflow 1.14.0、Python 3.7.4 和 Keras 2.2.4。完整的错误是:

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:/Users/Riley/PycharmProjects/shoeGAN/shoeWGAN.py", line 7, in <module>
    from keras.layers import *
  File "C:\Users\Riley\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import utils
  File "C:\Users\Riley\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
    from . import conv_utils
  File "C:\Users\Riley\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "C:\Users\Riley\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\backend\__init__.py", line 89, in <module>
    from .tensorflow_backend import *
  File "C:\Users\Riley\AppData\Local\Programs\Python\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File …
Run Code Online (Sandbox Code Playgroud)

python attributeerror keras tensorflow tensorboard

5
推荐指数
1
解决办法
1万
查看次数

Tensorflow 2 - AttributeError:“_NestedVariant”对象没有属性“batch”

在《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)

attributeerror tensorflow2.0 tf.data.dataset

5
推荐指数
0
解决办法
746
查看次数

如何解决此错误:AttributeError:'NoneType'对象没有属性'reply_text'?

我有一个按钮,它应该返回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

5
推荐指数
1
解决办法
6096
查看次数

PyQt6:Qt 模块枚举引发 AttributeError

我需要将以下代码从 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 enums pyqt attributeerror pyqt6

5
推荐指数
1
解决办法
5556
查看次数

处理可选的 python 字典字段

我正在处理加载到 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 json dictionary attributeerror

5
推荐指数
2
解决办法
2146
查看次数